001// --------------------------------------------------------------------------------
002// Copyright 2002-2024 Echo Three, LLC
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
017package com.echothree.model.control.content.server.logic;
018
019import com.echothree.control.user.content.common.spec.ContentPageLayoutUniversalSpec;
020import com.echothree.model.control.content.common.exception.DuplicateContentPageLayoutNameException;
021import com.echothree.model.control.content.common.exception.UnknownContentPageLayoutNameException;
022import com.echothree.model.control.content.common.exception.UnknownDefaultContentPageLayoutException;
023import com.echothree.model.control.content.server.control.ContentControl;
024import com.echothree.model.control.core.common.ComponentVendors;
025import com.echothree.model.control.core.common.EntityTypes;
026import com.echothree.model.control.core.common.exception.InvalidParameterCountException;
027import com.echothree.model.control.core.server.logic.EntityInstanceLogic;
028import com.echothree.model.data.content.server.entity.ContentPageLayout;
029import com.echothree.model.data.core.server.entity.EntityInstance;
030import com.echothree.model.data.party.server.entity.Language;
031import com.echothree.util.common.message.ExecutionErrors;
032import com.echothree.util.common.persistence.BasePK;
033import com.echothree.util.server.control.BaseLogic;
034import com.echothree.util.server.message.ExecutionErrorAccumulator;
035import com.echothree.util.server.persistence.EntityPermission;
036import com.echothree.util.server.persistence.Session;
037
038public class ContentPageLayoutLogic
039    extends BaseLogic {
040    
041    private ContentPageLayoutLogic() {
042        super();
043    }
044    
045    private static class ContentPageLayoutLogicHolder {
046        static ContentPageLayoutLogic instance = new ContentPageLayoutLogic();
047    }
048    
049    public static ContentPageLayoutLogic getInstance() {
050        return ContentPageLayoutLogicHolder.instance;
051    }
052
053    public ContentPageLayout createContentPageLayout(final ExecutionErrorAccumulator eea, final String contentPageLayoutName,
054            final Boolean isDefault, final Integer sortOrder, final Language language, final String description,
055            final BasePK createdBy) {
056        var contentControl = Session.getModelController(ContentControl.class);
057        ContentPageLayout contentPageLayout = contentControl.getContentPageLayoutByName(contentPageLayoutName);
058
059        if(contentPageLayout == null) {
060            contentPageLayout = contentControl.createContentPageLayout(contentPageLayoutName, isDefault, sortOrder, createdBy);
061
062            if(description != null) {
063                contentControl.createContentPageLayoutDescription(contentPageLayout, language, description, createdBy);
064            }
065        } else {
066            handleExecutionError(DuplicateContentPageLayoutNameException.class, eea, ExecutionErrors.DuplicateContentPageLayoutName.name(), contentPageLayoutName);
067        }
068
069        return contentPageLayout;
070    }
071
072    public ContentPageLayout getContentPageLayoutByName(final ExecutionErrorAccumulator eea, final String contentPageLayoutName,
073            final EntityPermission entityPermission) {
074        var contentControl = Session.getModelController(ContentControl.class);
075        ContentPageLayout contentPageLayout = contentControl.getContentPageLayoutByName(contentPageLayoutName, entityPermission);
076
077        if(contentPageLayout == null) {
078            handleExecutionError(UnknownContentPageLayoutNameException.class, eea, ExecutionErrors.UnknownContentPageLayoutName.name(), contentPageLayoutName);
079        }
080
081        return contentPageLayout;
082    }
083
084    public ContentPageLayout getContentPageLayoutByName(final ExecutionErrorAccumulator eea, final String contentPageLayoutName) {
085        return getContentPageLayoutByName(eea, contentPageLayoutName, EntityPermission.READ_ONLY);
086    }
087
088    public ContentPageLayout getContentPageLayoutByNameForUpdate(final ExecutionErrorAccumulator eea, final String contentPageLayoutName) {
089        return getContentPageLayoutByName(eea, contentPageLayoutName, EntityPermission.READ_WRITE);
090    }
091
092    public ContentPageLayout getContentPageLayoutByUniversalSpec(final ExecutionErrorAccumulator eea,
093            final ContentPageLayoutUniversalSpec universalSpec, boolean allowDefault, final EntityPermission entityPermission) {
094        ContentPageLayout contentPageLayout = null;
095        var contentControl = Session.getModelController(ContentControl.class);
096        String contentPageLayoutName = universalSpec.getContentPageLayoutName();
097        var parameterCount = (contentPageLayoutName == null ? 0 : 1) + EntityInstanceLogic.getInstance().countPossibleEntitySpecs(universalSpec);
098
099        switch(parameterCount) {
100            case 0:
101                if(allowDefault) {
102                    contentPageLayout = contentControl.getDefaultContentPageLayout(entityPermission);
103
104                    if(contentPageLayout == null) {
105                        handleExecutionError(UnknownDefaultContentPageLayoutException.class, eea, ExecutionErrors.UnknownDefaultContentPageLayout.name());
106                    }
107                } else {
108                    handleExecutionError(InvalidParameterCountException.class, eea, ExecutionErrors.InvalidParameterCount.name());
109                }
110                break;
111            case 1:
112                if(contentPageLayoutName == null) {
113                    var entityInstance = EntityInstanceLogic.getInstance().getEntityInstance(eea, universalSpec,
114                            ComponentVendors.ECHO_THREE.name(), EntityTypes.ContentPageLayout.name());
115
116                    if(!eea.hasExecutionErrors()) {
117                        contentPageLayout = contentControl.getContentPageLayoutByEntityInstance(entityInstance, entityPermission);
118                    }
119                } else {
120                    contentPageLayout = getContentPageLayoutByName(eea, contentPageLayoutName, entityPermission);
121                }
122                break;
123            default:
124                handleExecutionError(InvalidParameterCountException.class, eea, ExecutionErrors.InvalidParameterCount.name());
125                break;
126        }
127
128        return contentPageLayout;
129    }
130
131    public ContentPageLayout getContentPageLayoutByUniversalSpec(final ExecutionErrorAccumulator eea,
132            final ContentPageLayoutUniversalSpec universalSpec, boolean allowDefault) {
133        return getContentPageLayoutByUniversalSpec(eea, universalSpec, allowDefault, EntityPermission.READ_ONLY);
134    }
135
136    public ContentPageLayout getContentPageLayoutByUniversalSpecForUpdate(final ExecutionErrorAccumulator eea,
137            final ContentPageLayoutUniversalSpec universalSpec, boolean allowDefault) {
138        return getContentPageLayoutByUniversalSpec(eea, universalSpec, allowDefault, EntityPermission.READ_WRITE);
139    }
140
141    public void deleteContentPageLayout(final ExecutionErrorAccumulator eea, final ContentPageLayout contentPageLayout,
142            final BasePK deletedBy) {
143        var contentControl = Session.getModelController(ContentControl.class);
144
145        contentControl.deleteContentPageLayout(contentPageLayout, deletedBy);
146    }
147}