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.core.service;
018
019import java.util.List;
020
021import org.apache.commons.lang.StringUtils;
022import org.nuxeo.ecm.platform.forms.layout.api.LayoutDefinition;
023import org.nuxeo.ecm.platform.forms.layout.api.LayoutTypeDefinition;
024import org.nuxeo.ecm.platform.forms.layout.api.WidgetDefinition;
025import org.nuxeo.ecm.platform.forms.layout.api.WidgetReference;
026import org.nuxeo.ecm.platform.forms.layout.api.WidgetType;
027import org.nuxeo.ecm.platform.forms.layout.api.WidgetTypeDefinition;
028import org.nuxeo.ecm.platform.forms.layout.api.service.LayoutManager;
029import org.nuxeo.ecm.platform.forms.layout.api.service.LayoutStore;
030import org.nuxeo.runtime.api.Framework;
031import org.nuxeo.runtime.model.DefaultComponent;
032
033/**
034 * @author Anahide Tchertchian
035 * @since 5.5
036 */
037public abstract class AbstractLayoutManager extends DefaultComponent implements LayoutManager {
038
039    private static final long serialVersionUID = 1L;
040
041    public abstract String getDefaultStoreCategory();
042
043    protected String getStoreCategory(String cat) {
044        if (StringUtils.isBlank(cat)) {
045            return getDefaultStoreCategory();
046        }
047        return cat;
048    }
049
050    protected LayoutStore getLayoutStore() {
051        return Framework.getService(LayoutStore.class);
052    }
053
054    protected WidgetDefinition lookupWidget(LayoutDefinition layoutDef, WidgetReference widgetRef) {
055        String widgetName = widgetRef.getName();
056        WidgetDefinition wDef = null;
057        if (layoutDef != null) {
058            wDef = layoutDef.getWidgetDefinition(widgetName);
059        }
060        if (wDef == null) {
061            // try in global registry
062            wDef = lookupWidget(widgetRef);
063        }
064        return wDef;
065    }
066
067    protected WidgetDefinition lookupWidget(WidgetReference widgetRef) {
068        String widgetName = widgetRef.getName();
069        String cat = widgetRef.getCategory();
070        WidgetDefinition wDef;
071        if (StringUtils.isBlank(cat)) {
072            wDef = getWidgetDefinition(widgetName);
073        } else {
074            wDef = getLayoutStore().getWidgetDefinition(cat, widgetName);
075        }
076        if (wDef != null) {
077            wDef.setGlobal(true);
078        }
079        return wDef;
080    }
081
082    @Override
083    public WidgetType getWidgetType(String typeName) {
084        return getLayoutStore().getWidgetType(getDefaultStoreCategory(), typeName);
085    }
086
087    @Override
088    public WidgetTypeDefinition getWidgetTypeDefinition(String typeName) {
089        return getLayoutStore().getWidgetTypeDefinition(getDefaultStoreCategory(), typeName);
090    }
091
092    @Override
093    public List<WidgetTypeDefinition> getWidgetTypeDefinitions() {
094        return getLayoutStore().getWidgetTypeDefinitions(getDefaultStoreCategory());
095    }
096
097    @Override
098    public LayoutTypeDefinition getLayoutTypeDefinition(String typeName) {
099        return getLayoutStore().getLayoutTypeDefinition(getDefaultStoreCategory(), typeName);
100    }
101
102    @Override
103    public List<LayoutTypeDefinition> getLayoutTypeDefinitions() {
104        return getLayoutStore().getLayoutTypeDefinitions(getDefaultStoreCategory());
105    }
106
107    @Override
108    public LayoutDefinition getLayoutDefinition(String layoutName) {
109        return getLayoutStore().getLayoutDefinition(getDefaultStoreCategory(), layoutName);
110    }
111
112    @Override
113    public List<String> getLayoutDefinitionNames() {
114        return getLayoutStore().getLayoutDefinitionNames(getDefaultStoreCategory());
115    }
116
117    @Override
118    public WidgetDefinition getWidgetDefinition(String widgetName) {
119        return getLayoutStore().getWidgetDefinition(getDefaultStoreCategory(), widgetName);
120    }
121
122    // registry helpers
123
124    protected void registerWidgetType(WidgetTypeDefinition desc) {
125        getLayoutStore().registerWidgetType(getDefaultStoreCategory(), desc);
126    }
127
128    protected void unregisterWidgetType(WidgetTypeDefinition desc) {
129        getLayoutStore().unregisterWidgetType(getDefaultStoreCategory(), desc);
130    }
131
132    protected void registerLayoutType(LayoutTypeDefinition desc) {
133        getLayoutStore().registerLayoutType(getDefaultStoreCategory(), desc);
134    }
135
136    protected void unregisterLayoutType(LayoutTypeDefinition desc) {
137        getLayoutStore().unregisterLayoutType(getDefaultStoreCategory(), desc);
138    }
139
140    protected void registerLayout(LayoutDefinition layoutDef) {
141        getLayoutStore().registerLayout(getDefaultStoreCategory(), layoutDef);
142    }
143
144    protected void unregisterLayout(LayoutDefinition layoutDef) {
145        getLayoutStore().unregisterLayout(getDefaultStoreCategory(), layoutDef);
146    }
147
148    protected void registerWidget(WidgetDefinition widgetDef) {
149        getLayoutStore().registerWidget(getDefaultStoreCategory(), widgetDef);
150    }
151
152    protected void unregisterWidget(WidgetDefinition widgetDef) {
153        getLayoutStore().unregisterWidget(getDefaultStoreCategory(), widgetDef);
154    }
155
156}