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.control.user.content.server.command;
018
019import com.echothree.control.user.content.common.edit.ContentEditFactory;
020import com.echothree.control.user.content.common.edit.ContentPageLayoutEdit;
021import com.echothree.control.user.content.common.form.EditContentPageLayoutForm;
022import com.echothree.control.user.content.common.result.ContentResultFactory;
023import com.echothree.control.user.content.common.result.EditContentPageLayoutResult;
024import com.echothree.control.user.content.common.spec.ContentPageLayoutUniversalSpec;
025import com.echothree.model.control.content.server.control.ContentControl;
026import com.echothree.model.control.content.server.logic.ContentPageLayoutLogic;
027import com.echothree.model.control.party.common.PartyTypes;
028import com.echothree.model.control.security.common.SecurityRoleGroups;
029import com.echothree.model.control.security.common.SecurityRoles;
030import com.echothree.model.data.content.server.entity.ContentPageLayout;
031import com.echothree.model.data.content.server.entity.ContentPageLayoutDescription;
032import com.echothree.model.data.content.server.entity.ContentPageLayoutDetail;
033import com.echothree.model.data.content.server.value.ContentPageLayoutDescriptionValue;
034import com.echothree.model.data.content.server.value.ContentPageLayoutDetailValue;
035import com.echothree.model.data.user.common.pk.UserVisitPK;
036import com.echothree.util.common.message.ExecutionErrors;
037import com.echothree.util.common.validation.FieldDefinition;
038import com.echothree.util.common.validation.FieldType;
039import com.echothree.util.server.control.BaseAbstractEditCommand;
040import com.echothree.util.server.control.CommandSecurityDefinition;
041import com.echothree.util.server.control.PartyTypeDefinition;
042import com.echothree.util.server.control.SecurityRoleDefinition;
043import com.echothree.util.server.persistence.Session;
044import java.util.Arrays;
045import java.util.Collections;
046import java.util.List;
047
048public class EditContentPageLayoutCommand
049        extends BaseAbstractEditCommand<ContentPageLayoutUniversalSpec, ContentPageLayoutEdit, EditContentPageLayoutResult, ContentPageLayout, ContentPageLayout> {
050    
051    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
052    private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS;
053    private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS;
054    
055    static {
056        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(Collections.unmodifiableList(Arrays.asList(
057                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
058                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), Collections.unmodifiableList(Arrays.asList(
059                        new SecurityRoleDefinition(SecurityRoleGroups.ContentPageLayout.name(), SecurityRoles.Edit.name())
060                        )))
061                )));
062        
063        SPEC_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
064                new FieldDefinition("ContentPageLayoutName", FieldType.ENTITY_NAME, false, null, null),
065                new FieldDefinition("EntityRef", FieldType.ENTITY_REF, false, null, null),
066                new FieldDefinition("Key", FieldType.KEY, false, null, null),
067                new FieldDefinition("Guid", FieldType.GUID, false, null, null),
068                new FieldDefinition("Ulid", FieldType.ULID, false, null, null)
069                ));
070        
071        EDIT_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
072                new FieldDefinition("ContentPageLayoutName", FieldType.ENTITY_NAME, true, null, null),
073                new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null),
074                new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null),
075                new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L)
076                ));
077    }
078    
079    /** Creates a new instance of EditContentPageLayoutCommand */
080    public EditContentPageLayoutCommand(UserVisitPK userVisitPK, EditContentPageLayoutForm form) {
081        super(userVisitPK, form, COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
082    }
083    
084    @Override
085    public EditContentPageLayoutResult getResult() {
086        return ContentResultFactory.getEditContentPageLayoutResult();
087    }
088    
089    @Override
090    public ContentPageLayoutEdit getEdit() {
091        return ContentEditFactory.getContentPageLayoutEdit();
092    }
093    
094    @Override
095    public ContentPageLayout getEntity(EditContentPageLayoutResult result) {
096        return ContentPageLayoutLogic.getInstance().getContentPageLayoutByUniversalSpec(this, spec, false, editModeToEntityPermission(editMode));
097    }
098    
099    @Override
100    public ContentPageLayout getLockEntity(ContentPageLayout contentPageLayout) {
101        return contentPageLayout;
102    }
103    
104    @Override
105    public void fillInResult(EditContentPageLayoutResult result, ContentPageLayout contentPageLayout) {
106        var contentControl = Session.getModelController(ContentControl.class);
107        
108        result.setContentPageLayout(contentControl.getContentPageLayoutTransfer(getUserVisit(), contentPageLayout));
109    }
110    
111    @Override
112    public void doLock(ContentPageLayoutEdit edit, ContentPageLayout contentPageLayout) {
113        var contentControl = Session.getModelController(ContentControl.class);
114        ContentPageLayoutDescription contentPageLayoutDescription = contentControl.getContentPageLayoutDescription(contentPageLayout, getPreferredLanguage());
115        ContentPageLayoutDetail contentPageLayoutDetail = contentPageLayout.getLastDetail();
116        
117        edit.setContentPageLayoutName(contentPageLayoutDetail.getContentPageLayoutName());
118        edit.setIsDefault(contentPageLayoutDetail.getIsDefault().toString());
119        edit.setSortOrder(contentPageLayoutDetail.getSortOrder().toString());
120
121        if(contentPageLayoutDescription != null) {
122            edit.setDescription(contentPageLayoutDescription.getDescription());
123        }
124    }
125        
126    @Override
127    public void canUpdate(ContentPageLayout contentPageLayout) {
128        var contentControl = Session.getModelController(ContentControl.class);
129        String contentPageLayoutName = edit.getContentPageLayoutName();
130        ContentPageLayout duplicateContentPageLayout = contentControl.getContentPageLayoutByName(contentPageLayoutName);
131
132        if(duplicateContentPageLayout != null && !contentPageLayout.equals(duplicateContentPageLayout)) {
133            addExecutionError(ExecutionErrors.DuplicateContentPageLayoutName.name(), contentPageLayoutName);
134        }
135    }
136    
137    @Override
138    public void doUpdate(ContentPageLayout contentPageLayout) {
139        var contentControl = Session.getModelController(ContentControl.class);
140        var partyPK = getPartyPK();
141        ContentPageLayoutDetailValue contentPageLayoutDetailValue = contentControl.getContentPageLayoutDetailValueForUpdate(contentPageLayout);
142        ContentPageLayoutDescription contentPageLayoutDescription = contentControl.getContentPageLayoutDescriptionForUpdate(contentPageLayout, getPreferredLanguage());
143        String description = edit.getDescription();
144
145        contentPageLayoutDetailValue.setContentPageLayoutName(edit.getContentPageLayoutName());
146        contentPageLayoutDetailValue.setIsDefault(Boolean.valueOf(edit.getIsDefault()));
147        contentPageLayoutDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder()));
148
149        contentControl.updateContentPageLayoutFromValue(contentPageLayoutDetailValue, partyPK);
150
151        if(contentPageLayoutDescription == null && description != null) {
152            contentControl.createContentPageLayoutDescription(contentPageLayout, getPreferredLanguage(), description, partyPK);
153        } else if(contentPageLayoutDescription != null && description == null) {
154            contentControl.deleteContentPageLayoutDescription(contentPageLayoutDescription, partyPK);
155        } else if(contentPageLayoutDescription != null && description != null) {
156            ContentPageLayoutDescriptionValue contentPageLayoutDescriptionValue = contentControl.getContentPageLayoutDescriptionValue(contentPageLayoutDescription);
157
158            contentPageLayoutDescriptionValue.setDescription(description);
159            contentControl.updateContentPageLayoutDescriptionFromValue(contentPageLayoutDescriptionValue, partyPK);
160        }
161    }
162    
163}