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 WorkQueueDescriptorRegistry extends ContributionFragmentRegistry<WorkQueueDescriptor> {
038
039    private static final Log log = LogFactory.getLog(WorkQueueDescriptorRegistry.class);
040
041    protected final WorkManagerImpl works;
042
043    protected Map<String, WorkQueueDescriptor> registry = new HashMap<String, WorkQueueDescriptor>();
044
045    protected volatile boolean refresh;
046
047    protected Map<String, String> categoryToQueueId = new HashMap<String, String>();
048
049    public WorkQueueDescriptorRegistry(WorkManagerImpl works) {
050        this.works = works;
051    }
052
053    /**
054     * Gets the descriptor for a given queue id.
055     *
056     * @param queueId the queue id
057     * @return the queue descriptor, or {@code null}
058     */
059    public synchronized WorkQueueDescriptor get(String queueId) {
060        return registry.get(queueId);
061    }
062
063    /**
064     * Gets the list of queue ids.
065     *
066     * @return the list of queue ids
067     */
068    public synchronized List<String> getQueueIds() {
069        return new ArrayList<String>(registry.keySet());
070    }
071
072    @Override
073    public String getContributionId(WorkQueueDescriptor contrib) {
074        return contrib.id;
075    }
076
077    @Override
078    public void contributionUpdated(String id, WorkQueueDescriptor contrib, WorkQueueDescriptor newOrigContrib) {
079        registry.put(id, contrib);
080        refresh = true;
081        if (works.started) {
082            works.activateQueue(contrib);
083        }
084    }
085
086    @Override
087    public void contributionRemoved(String id, WorkQueueDescriptor origContrib) {
088        if (works.started) {
089            works.deactivateQueue(origContrib);
090        }
091        registry.remove(id);
092        refresh = true;
093    }
094
095    protected synchronized void refresh() {
096        for (Entry<String, WorkQueueDescriptor> es : registry.entrySet()) {
097            String queueId = es.getKey();
098            for (String category : es.getValue().categories) {
099                String old = categoryToQueueId.get("category");
100                if (old != null) {
101                    log.error("Work category '" + category + "' cannot be assigned to work queue '" + queueId
102                            + "' because it is already assigned to work queue '" + old + "'");
103                } else {
104                    categoryToQueueId.put(category, queueId);
105                }
106            }
107        }
108    }
109
110    public String getQueueId(String category) {
111        if (refresh) {
112            refresh();
113        }
114        return categoryToQueueId.get(category);
115    }
116
117    @Override
118    public WorkQueueDescriptor clone(WorkQueueDescriptor orig) {
119        return orig.clone();
120    }
121
122    @Override
123    public void merge(WorkQueueDescriptor src, WorkQueueDescriptor dst) {
124        dst.merge(src);
125    }
126
127}