001/*
002 * (C) Copyright 2006-2008 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 *     troger
016 *
017 * $Id$
018 */
019
020package org.nuxeo.ecm.platform.annotations.configuration.service;
021
022import java.util.ArrayList;
023import java.util.HashMap;
024import java.util.HashSet;
025import java.util.List;
026import java.util.Map;
027import java.util.Set;
028
029import org.apache.commons.logging.Log;
030import org.apache.commons.logging.LogFactory;
031import org.nuxeo.ecm.platform.annotations.gwt.server.configuration.UserInfoMapper;
032import org.nuxeo.ecm.platform.annotations.gwt.server.configuration.WebPermission;
033import org.nuxeo.runtime.model.ComponentContext;
034import org.nuxeo.runtime.model.ComponentInstance;
035import org.nuxeo.runtime.model.DefaultComponent;
036
037/**
038 * @author <a href="mailto:troger@nuxeo.com">Thomas Roger</a>
039 * @author <a href="mailto:qlamerand@nuxeo.com">Quentin Lamerand</a>
040 */
041public class WebAnnotationConfigurationServiceImpl extends DefaultComponent implements
042        WebAnnotationConfigurationService {
043
044    private static final Log log = LogFactory.getLog(WebAnnotationConfigurationServiceImpl.class);
045
046    private static final String ANNOTATION_TYPES_EXTENSION_POINT = "types";
047
048    private static final String USER_INFO_EXTENSION_POINT = "userInfo";
049
050    private static final String WEB_PERMISSION_EXTENSION_POINT = "webPermission";
051
052    private static final String FILTERS_EXTENSION_POINT = "filters";
053
054    private static final String DISPLAYED_FIELDS_EXTENSION_POINT = "displayedFields";
055
056    private Map<String, WebAnnotationDefinitionDescriptor> annotationDefinitionsDescriptors;
057
058    private UserInfoMapper userInfoMapper;
059
060    private WebPermission webPermission;
061
062    private Map<String, FilterDescriptor> filterDescriptors;
063
064    private Set<String> displayedFields;
065
066    private Map<String, String> fieldLabels;
067
068    @Override
069    public void activate(ComponentContext context) {
070        annotationDefinitionsDescriptors = new HashMap<String, WebAnnotationDefinitionDescriptor>();
071        filterDescriptors = new HashMap<String, FilterDescriptor>();
072        displayedFields = new HashSet<String>();
073        fieldLabels = new HashMap<String, String>();
074    }
075
076    @Override
077    public void deactivate(ComponentContext context) {
078        annotationDefinitionsDescriptors = null;
079        userInfoMapper = null;
080        filterDescriptors = null;
081        displayedFields = null;
082        fieldLabels = null;
083    }
084
085    @Override
086    public void registerContribution(Object contribution, String extensionPoint, ComponentInstance contributor) {
087        if (ANNOTATION_TYPES_EXTENSION_POINT.equals(extensionPoint)) {
088            WebAnnotationDefinitionDescriptor descriptor = (WebAnnotationDefinitionDescriptor) contribution;
089            if (annotationDefinitionsDescriptors.put(descriptor.getName(), descriptor) != null) {
090                log.info("Already registered annotation type: " + descriptor.getName() + ", storing the new one.");
091            }
092        } else if (USER_INFO_EXTENSION_POINT.equals(extensionPoint)) {
093            UserInfoMapperDescriptor descriptor = (UserInfoMapperDescriptor) contribution;
094            userInfoMapper = newInstance(descriptor.getKlass());
095        } else if (WEB_PERMISSION_EXTENSION_POINT.equals(extensionPoint)) {
096            WebPermissionDescriptor descriptor = (WebPermissionDescriptor) contribution;
097            webPermission = newInstance(descriptor.getKlass());
098        } else if (FILTERS_EXTENSION_POINT.equals(extensionPoint)) {
099            FilterDescriptor descriptor = (FilterDescriptor) contribution;
100            if (filterDescriptors.put(descriptor.getName(), descriptor) != null) {
101                log.info("Already registered annotation filter: " + descriptor.getName() + ", storing the new one.");
102            }
103        } else if (DISPLAYED_FIELDS_EXTENSION_POINT.equals(extensionPoint)) {
104            DisplayedFieldsDescriptor descriptor = (DisplayedFieldsDescriptor) contribution;
105            String fieldName = descriptor.getName();
106            if (descriptor.isDisplayed()) {
107                displayedFields.add(fieldName);
108            } else {
109                displayedFields.remove(fieldName);
110            }
111
112            String fieldLabel = descriptor.getLabel();
113            if (fieldLabel != null) {
114                fieldLabels.put(fieldName, fieldLabel);
115            }
116        }
117    }
118
119    protected <T> T newInstance(Class<T> klass) {
120        try {
121            return klass.newInstance();
122        } catch (ReflectiveOperationException e) {
123            throw new RuntimeException(e);
124        }
125    }
126
127    @Override
128    public void unregisterContribution(Object contribution, String extensionPoint, ComponentInstance contributor) {
129        if (ANNOTATION_TYPES_EXTENSION_POINT.equals(extensionPoint)) {
130            WebAnnotationDefinitionDescriptor descriptor = (WebAnnotationDefinitionDescriptor) contribution;
131            annotationDefinitionsDescriptors.remove(descriptor.getName());
132        } else if (FILTERS_EXTENSION_POINT.equals(extensionPoint)) {
133            FilterDescriptor descriptor = (FilterDescriptor) contribution;
134            filterDescriptors.remove(descriptor.getName());
135        } else if (DISPLAYED_FIELDS_EXTENSION_POINT.equals(extensionPoint)) {
136            DisplayedFieldsDescriptor descriptor = (DisplayedFieldsDescriptor) contribution;
137            String fieldName = descriptor.getName();
138            displayedFields.remove(fieldName);
139            fieldLabels.remove(fieldName);
140        }
141    }
142
143    public List<WebAnnotationDefinitionDescriptor> getAllWebAnnotationDefinitions() {
144        return new ArrayList<WebAnnotationDefinitionDescriptor>(annotationDefinitionsDescriptors.values());
145    }
146
147    public List<WebAnnotationDefinitionDescriptor> getEnabledWebAnnotationDefinitions() {
148        List<WebAnnotationDefinitionDescriptor> definitions = new ArrayList<WebAnnotationDefinitionDescriptor>();
149        for (WebAnnotationDefinitionDescriptor def : annotationDefinitionsDescriptors.values()) {
150            if (def.isEnabled()) {
151                definitions.add(def);
152            }
153        }
154        return definitions;
155    }
156
157    public UserInfoMapper getUserInfoMapper() {
158        return userInfoMapper;
159    }
160
161    public WebPermission getWebPermission() {
162        return webPermission;
163    }
164
165    public Map<String, FilterDescriptor> getFilterDefinitions() {
166        return filterDescriptors;
167    }
168
169    public Set<String> getDisplayedFields() {
170        return displayedFields;
171    }
172
173    public Map<String, String> getFieldLabels() {
174        return fieldLabels;
175    }
176
177}