001/*
002 * (C) Copyright 2010 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 *     Anahide Tchertchian
018 */
019package org.nuxeo.ecm.platform.forms.layout.demo.service;
020
021import java.util.ArrayList;
022import java.util.HashMap;
023import java.util.List;
024import java.util.Map;
025
026import org.apache.commons.logging.Log;
027import org.apache.commons.logging.LogFactory;
028import org.nuxeo.ecm.platform.forms.layout.demo.descriptors.DemoWidgetTypeDescriptor;
029import org.nuxeo.ecm.platform.forms.layout.service.WebLayoutManager;
030import org.nuxeo.runtime.model.ComponentInstance;
031import org.nuxeo.runtime.model.DefaultComponent;
032
033/**
034 * @author Anahide Tchertchian
035 */
036public class LayoutDemoService extends DefaultComponent implements LayoutDemoManager {
037
038    private static final long serialVersionUID = 1L;
039
040    private static final Log log = LogFactory.getLog(LayoutDemoService.class);
041
042    public static final String WIDGET_TYPES_EP_NAME = "widgettypes";
043
044    private final Map<String, DemoWidgetType> widgetTypeRegistry = new HashMap<String, DemoWidgetType>();
045
046    private final Map<String, List<String>> widgetTypesByCategory = new HashMap<String, List<String>>();
047
048    private final Map<String, String> widgetTypeByViewId = new HashMap<String, String>();
049
050    @Override
051    public void registerContribution(Object contribution, String extensionPoint, ComponentInstance contributor) {
052        if (extensionPoint.equals(WIDGET_TYPES_EP_NAME)) {
053            registerWidgetType(contribution);
054        }
055    }
056
057    @Override
058    public void unregisterContribution(Object contribution, String extensionPoint, ComponentInstance contributor) {
059        if (extensionPoint.equals(WIDGET_TYPES_EP_NAME)) {
060            unregisterWidgetType(contribution);
061        }
062    }
063
064    private void registerWidgetType(Object contribution) {
065        DemoWidgetTypeDescriptor desc = (DemoWidgetTypeDescriptor) contribution;
066        String name = desc.getName();
067        if (widgetTypeRegistry.containsKey(name)) {
068            log.error(String.format("Overriding definition for widget type %s", name));
069            widgetTypeRegistry.remove(name);
070        }
071        String category = desc.getCategory();
072        String wtCat = desc.getWidgetTypeCategory();
073        if (wtCat == null) {
074            wtCat = WebLayoutManager.JSF_CATEGORY;
075        }
076        String viewId = desc.getViewId();
077        // TODO: query the layout service to get more information about this
078        // widget type and use it in the demo
079        DemoWidgetType widgetType = new DemoWidgetTypeImpl(desc.getWidgetTypeName(), desc.getLabel(), viewId, category,
080                wtCat, desc.isPreviewEnabled(), desc.isPreviewHideViewMode(), desc.isPreviewHideEditMode(),
081                desc.getFields(), desc.getDefaultProperties(), desc.getDemoLayouts());
082        widgetTypeRegistry.put(name, widgetType);
083        if (category != null) {
084            List<String> byCat = widgetTypesByCategory.get(category);
085            if (byCat != null && !byCat.contains(name)) {
086                byCat.add(name);
087            } else {
088                byCat = new ArrayList<String>();
089                byCat.add(name);
090            }
091            widgetTypesByCategory.put(category, byCat);
092        }
093        if (widgetTypeByViewId.containsKey(viewId)) {
094            String existingWidget = widgetTypeByViewId.get(viewId);
095            if (!name.equals(existingWidget)) {
096                log.warn(String.format("Changing view id '%s' from widget '%s' to widget '%s'", viewId, existingWidget,
097                        name));
098            }
099        }
100        widgetTypeByViewId.put(viewId, name);
101        log.info("Registered widget type: " + name);
102    }
103
104    private void unregisterWidgetType(Object contribution) {
105        DemoWidgetTypeDescriptor desc = (DemoWidgetTypeDescriptor) contribution;
106        String name = desc.getName();
107        if (widgetTypeRegistry.containsKey(name)) {
108            widgetTypeRegistry.remove(name);
109            log.debug("Unregistered widget type: " + name);
110        }
111        String category = desc.getCategory();
112        if (category != null) {
113            List<String> byCat = widgetTypesByCategory.get(category);
114            if (byCat != null && byCat.contains(name)) {
115                byCat.remove(name);
116                widgetTypesByCategory.put(category, byCat);
117            }
118        }
119    }
120
121    public DemoWidgetType getWidgetType(String widgetTypeName) {
122        return widgetTypeRegistry.get(widgetTypeName);
123    }
124
125    public DemoWidgetType getWidgetTypeByViewId(String viewId) {
126        if (!widgetTypeByViewId.containsKey(viewId)) {
127            return null;
128        }
129        String name = widgetTypeByViewId.get(viewId);
130        return widgetTypeRegistry.get(name);
131    }
132
133    public List<DemoWidgetType> getWidgetTypes(String category) {
134        List<String> wTypes = widgetTypesByCategory.get(category);
135        List<DemoWidgetType> res = new ArrayList<DemoWidgetType>();
136        if (wTypes != null) {
137            for (String wType : wTypes) {
138                res.add(widgetTypeRegistry.get(wType));
139            }
140        }
141        return res;
142    }
143
144}