001/*
002 * (C) Copyright 2012 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Florent Guillaume
018 */
019package org.nuxeo.ecm.core.work;
020
021import java.util.ArrayList;
022import java.util.HashMap;
023import java.util.List;
024import java.util.Map;
025import java.util.Map.Entry;
026
027import org.apache.commons.logging.Log;
028import org.apache.commons.logging.LogFactory;
029import org.nuxeo.ecm.core.work.api.WorkQueueDescriptor;
030import org.nuxeo.runtime.model.ContributionFragmentRegistry;
031
032/**
033 * Registry for the {@link WorkQueueDescriptor}s.
034 *
035 * @since 5.6
036 */
037public class WorkQueueRegistry extends ContributionFragmentRegistry<WorkQueueDescriptor> {
038
039    private static final Log log = LogFactory.getLog(WorkQueueRegistry.class);
040
041    protected Map<String, WorkQueueDescriptor> registry = new HashMap<String, WorkQueueDescriptor>();
042
043    protected Map<String, String> categoryToQueueId = new HashMap<String, String>();
044
045    /**
046     * Gets the descriptor for a given queue id.
047     *
048     * @param queueId the queue id
049     * @return the queue descriptor, or {@code null}
050     */
051    public WorkQueueDescriptor get(String queueId) {
052        return registry.get(queueId);
053    }
054
055    /**
056     * Gets the list of queue ids.
057     *
058     * @return the list of queue ids
059     */
060    public List<String> getQueueIds() {
061        return new ArrayList<String>(registry.keySet());
062    }
063
064    @Override
065    public String getContributionId(WorkQueueDescriptor contrib) {
066        return contrib.id;
067    }
068
069    @Override
070    public void contributionUpdated(String id, WorkQueueDescriptor contrib, WorkQueueDescriptor newOrigContrib) {
071        registry.put(id, contrib);
072    }
073
074    @Override
075    public void contributionRemoved(String id, WorkQueueDescriptor origContrib) {
076        registry.remove(id);
077    }
078
079    protected void index() {
080        for (Entry<String, WorkQueueDescriptor> es : registry.entrySet()) {
081            String queueId = es.getKey();
082            for (String category : es.getValue().categories) {
083                String old = categoryToQueueId.get("category");
084                if (old != null) {
085                    log.error("Work category '" + category + "' cannot be assigned to work queue '" + queueId
086                            + "' because it is already assigned to work queue '" + old + "'");
087                } else {
088                    categoryToQueueId.put(category, queueId);
089                }
090            }
091        }
092    }
093
094    public String getQueueId(String category) {
095        return categoryToQueueId.get(category);
096    }
097
098    @Override
099    public WorkQueueDescriptor clone(WorkQueueDescriptor orig) {
100        return orig.clone();
101    }
102
103    @Override
104    public void merge(WorkQueueDescriptor src, WorkQueueDescriptor dst) {
105        dst.merge(src);
106    }
107
108}