001/*
002 * (C) Copyright 2006-2007 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Nuxeo - initial API and implementation
016 *
017 * $Id: JOOoConvertPluginImpl.java 18651 2007-05-13 20:28:53Z sfermigier $
018 */
019
020package org.nuxeo.ecm.webapp.documentsLists;
021
022import java.util.ArrayList;
023import java.util.HashMap;
024import java.util.List;
025import java.util.Map;
026
027import org.apache.commons.logging.Log;
028import org.apache.commons.logging.LogFactory;
029import org.nuxeo.runtime.model.ComponentContext;
030import org.nuxeo.runtime.model.ComponentInstance;
031import org.nuxeo.runtime.model.DefaultComponent;
032
033public class DocumentsListsService extends DefaultComponent {
034
035    public static final String NAME = "org.nuxeo.ecm.webapp.documentsLists.DocumentsListsService";
036
037    private static final Log log = LogFactory.getLog(DocumentsListsService.class);
038
039    private Map<String, DocumentsListDescriptor> descriptors;
040
041    public DocumentsListDescriptor getDocumentsListDescriptor(String descriptorName) {
042        return descriptors.get(descriptorName);
043    }
044
045    public List<String> getDocumentsListDescriptorsName() {
046        List<String> list = new ArrayList<String>();
047        for (String k : descriptors.keySet()) {
048            if (descriptors.get(k).getEnabled()) {
049                list.add(k);
050            }
051        }
052        return list;
053    }
054
055    @Override
056    public void activate(ComponentContext context) {
057        descriptors = new HashMap<String, DocumentsListDescriptor>();
058    }
059
060    @Override
061    public void deactivate(ComponentContext context) {
062        descriptors = null;
063    }
064
065    private void mergeDescriptors(DocumentsListDescriptor newContrib) {
066        DocumentsListDescriptor oldDescriptor = descriptors.get(newContrib.getName());
067
068        oldDescriptor.setEnabled(newContrib.getEnabled());
069        if (newContrib.getCategory() != null) {
070            oldDescriptor.setCategory(newContrib.getCategory());
071        }
072        oldDescriptor.setSupportAppends(newContrib.getSupportAppends());
073        oldDescriptor.setDefaultInCategory(newContrib.getDefaultInCategory());
074        oldDescriptor.setIsSession(newContrib.getIsSession());
075        oldDescriptor.setPersistent(newContrib.getPersistent());
076        if (newContrib.getImageURL() != null) {
077            oldDescriptor.setImageURL(newContrib.getImageURL());
078        }
079        oldDescriptor.setReadOnly(oldDescriptor.getReadOnly());
080        if (newContrib.getTitle() != null) {
081            oldDescriptor.setTitle(newContrib.getTitle());
082        }
083    }
084
085    @Override
086    public void registerContribution(Object contribution, String extensionPoint, ComponentInstance contributor) {
087
088        DocumentsListDescriptor descriptor = (DocumentsListDescriptor) contribution;
089        if (descriptors.containsKey(descriptor.getName())) {
090            mergeDescriptors(descriptor);
091            log.debug("merged DocumentsListDescriptor: " + descriptor.getName());
092        } else {
093            descriptors.put(descriptor.getName(), descriptor);
094            log.debug("registered DocumentsListDescriptor: " + descriptor.getName());
095        }
096    }
097
098    @Override
099    public void unregisterContribution(Object contribution, String extensionPoint, ComponentInstance contributor) {
100
101        DocumentsListDescriptor descriptor = (DocumentsListDescriptor) contribution;
102        descriptors.remove(descriptor.getName());
103        log.debug("unregistered DocumentsListDescriptor: " + descriptor.getName());
104    }
105
106}