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