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.lang.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    public abstract String getDefaultStoreCategory();
044
045    protected String getStoreCategory(String cat) {
046        if (StringUtils.isBlank(cat)) {
047            return getDefaultStoreCategory();
048        }
049        return cat;
050    }
051
052    protected LayoutStore getLayoutStore() {
053        return Framework.getService(LayoutStore.class);
054    }
055
056    protected WidgetDefinition lookupWidget(LayoutDefinition layoutDef, WidgetReference widgetRef) {
057        String widgetName = widgetRef.getName();
058        WidgetDefinition wDef = null;
059        if (layoutDef != null) {
060            wDef = layoutDef.getWidgetDefinition(widgetName);
061            if (wDef != null && wDef.getType() == null) {
062                // consider it's a reference for a global widget
063                wDef = null;
064            }
065        }
066        if (wDef == null) {
067            // try in global registry
068            wDef = lookupWidget(widgetRef);
069        }
070        return wDef;
071    }
072
073    protected WidgetDefinition lookupWidget(WidgetReference widgetRef) {
074        String widgetName = widgetRef.getName();
075        String cat = widgetRef.getCategory();
076        WidgetDefinition wDef;
077        if (StringUtils.isBlank(cat)) {
078            wDef = getWidgetDefinition(widgetName);
079        } else {
080            wDef = getLayoutStore().getWidgetDefinition(cat, widgetName);
081        }
082        if (wDef != null) {
083            wDef.setGlobal(true);
084        }
085        return wDef;
086    }
087
088    @Override
089    public WidgetType getWidgetType(String typeName) {
090        return getLayoutStore().getWidgetType(getDefaultStoreCategory(), typeName);
091    }
092
093    @Override
094    public WidgetTypeDefinition getWidgetTypeDefinition(String typeName) {
095        return getLayoutStore().getWidgetTypeDefinition(getDefaultStoreCategory(), typeName);
096    }
097
098    @Override
099    public List<WidgetTypeDefinition> getWidgetTypeDefinitions() {
100        return getLayoutStore().getWidgetTypeDefinitions(getDefaultStoreCategory());
101    }
102
103    @Override
104    public LayoutTypeDefinition getLayoutTypeDefinition(String typeName) {
105        return getLayoutStore().getLayoutTypeDefinition(getDefaultStoreCategory(), typeName);
106    }
107
108    @Override
109    public List<LayoutTypeDefinition> getLayoutTypeDefinitions() {
110        return getLayoutStore().getLayoutTypeDefinitions(getDefaultStoreCategory());
111    }
112
113    @Override
114    public LayoutDefinition getLayoutDefinition(String layoutName) {
115        return getLayoutStore().getLayoutDefinition(getDefaultStoreCategory(), layoutName);
116    }
117
118    @Override
119    public List<String> getLayoutDefinitionNames() {
120        return getLayoutStore().getLayoutDefinitionNames(getDefaultStoreCategory());
121    }
122
123    @Override
124    public WidgetDefinition getWidgetDefinition(String widgetName) {
125        return getLayoutStore().getWidgetDefinition(getDefaultStoreCategory(), widgetName);
126    }
127
128    // registry helpers
129
130    protected void registerWidgetType(WidgetTypeDefinition desc) {
131        getLayoutStore().registerWidgetType(getDefaultStoreCategory(), desc);
132    }
133
134    protected void unregisterWidgetType(WidgetTypeDefinition desc) {
135        getLayoutStore().unregisterWidgetType(getDefaultStoreCategory(), desc);
136    }
137
138    protected void registerLayoutType(LayoutTypeDefinition desc) {
139        getLayoutStore().registerLayoutType(getDefaultStoreCategory(), desc);
140    }
141
142    protected void unregisterLayoutType(LayoutTypeDefinition desc) {
143        getLayoutStore().unregisterLayoutType(getDefaultStoreCategory(), desc);
144    }
145
146    protected void registerLayout(LayoutDefinition layoutDef) {
147        getLayoutStore().registerLayout(getDefaultStoreCategory(), layoutDef);
148    }
149
150    protected void unregisterLayout(LayoutDefinition layoutDef) {
151        getLayoutStore().unregisterLayout(getDefaultStoreCategory(), layoutDef);
152    }
153
154    protected void registerWidget(WidgetDefinition widgetDef) {
155        getLayoutStore().registerWidget(getDefaultStoreCategory(), widgetDef);
156    }
157
158    protected void unregisterWidget(WidgetDefinition widgetDef) {
159        getLayoutStore().unregisterWidget(getDefaultStoreCategory(), widgetDef);
160    }
161
162}