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