001/*
002 * (C) Copyright 2006-2007 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 *     Nuxeo - initial API and implementation
018 *
019 * $Id: JOOoConvertPluginImpl.java 18651 2007-05-13 20:28:53Z sfermigier $
020 */
021
022package org.nuxeo.ecm.webapp.documentsLists;
023
024import java.util.ArrayList;
025import java.util.HashMap;
026import java.util.List;
027import java.util.Map;
028
029import org.apache.commons.logging.Log;
030import org.apache.commons.logging.LogFactory;
031import org.nuxeo.runtime.model.ComponentContext;
032import org.nuxeo.runtime.model.ComponentInstance;
033import org.nuxeo.runtime.model.DefaultComponent;
034
035public class DocumentsListsService extends DefaultComponent {
036
037    public static final String NAME = "org.nuxeo.ecm.webapp.documentsLists.DocumentsListsService";
038
039    private static final Log log = LogFactory.getLog(DocumentsListsService.class);
040
041    private Map<String, DocumentsListDescriptor> descriptors;
042
043    public DocumentsListDescriptor getDocumentsListDescriptor(String descriptorName) {
044        return descriptors.get(descriptorName);
045    }
046
047    public List<String> getDocumentsListDescriptorsName() {
048        List<String> list = new ArrayList<String>();
049        for (String k : descriptors.keySet()) {
050            if (descriptors.get(k).getEnabled()) {
051                list.add(k);
052            }
053        }
054        return list;
055    }
056
057    @Override
058    public void activate(ComponentContext context) {
059        descriptors = new HashMap<String, DocumentsListDescriptor>();
060    }
061
062    @Override
063    public void deactivate(ComponentContext context) {
064        descriptors = null;
065    }
066
067    private void mergeDescriptors(DocumentsListDescriptor newContrib) {
068        DocumentsListDescriptor oldDescriptor = descriptors.get(newContrib.getName());
069
070        oldDescriptor.setEnabled(newContrib.getEnabled());
071        if (newContrib.getCategory() != null) {
072            oldDescriptor.setCategory(newContrib.getCategory());
073        }
074        oldDescriptor.setSupportAppends(newContrib.getSupportAppends());
075        oldDescriptor.setDefaultInCategory(newContrib.getDefaultInCategory());
076        oldDescriptor.setIsSession(newContrib.getIsSession());
077        oldDescriptor.setPersistent(newContrib.getPersistent());
078        if (newContrib.getImageURL() != null) {
079            oldDescriptor.setImageURL(newContrib.getImageURL());
080        }
081        oldDescriptor.setReadOnly(oldDescriptor.getReadOnly());
082        if (newContrib.getTitle() != null) {
083            oldDescriptor.setTitle(newContrib.getTitle());
084        }
085    }
086
087    @Override
088    public void registerContribution(Object contribution, String extensionPoint, ComponentInstance contributor) {
089
090        DocumentsListDescriptor descriptor = (DocumentsListDescriptor) contribution;
091        if (descriptors.containsKey(descriptor.getName())) {
092            mergeDescriptors(descriptor);
093            log.debug("merged DocumentsListDescriptor: " + descriptor.getName());
094        } else {
095            descriptors.put(descriptor.getName(), descriptor);
096            log.debug("registered DocumentsListDescriptor: " + descriptor.getName());
097        }
098    }
099
100    @Override
101    public void unregisterContribution(Object contribution, String extensionPoint, ComponentInstance contributor) {
102
103        DocumentsListDescriptor descriptor = (DocumentsListDescriptor) contribution;
104        descriptors.remove(descriptor.getName());
105        log.debug("unregistered DocumentsListDescriptor: " + descriptor.getName());
106    }
107
108}