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