001// --------------------------------------------------------------------------------
002// Copyright 2002-2025 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.ContentSectionEdit;
021import com.echothree.control.user.content.common.form.EditContentSectionForm;
022import com.echothree.control.user.content.common.result.ContentResultFactory;
023import com.echothree.control.user.content.common.result.EditContentSectionResult;
024import com.echothree.control.user.content.common.spec.ContentSectionSpec;
025import com.echothree.model.control.content.common.ContentSections;
026import com.echothree.model.control.content.server.control.ContentControl;
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.ContentCollection;
031import com.echothree.model.data.content.server.entity.ContentSection;
032import com.echothree.model.data.user.common.pk.UserVisitPK;
033import com.echothree.util.common.message.ExecutionErrors;
034import com.echothree.util.common.validation.FieldDefinition;
035import com.echothree.util.common.validation.FieldType;
036import com.echothree.util.common.command.EditMode;
037import com.echothree.util.server.control.BaseAbstractEditCommand;
038import com.echothree.util.server.control.CommandSecurityDefinition;
039import com.echothree.util.server.control.PartyTypeDefinition;
040import com.echothree.util.server.control.SecurityRoleDefinition;
041import com.echothree.util.server.persistence.Session;
042import java.util.Arrays;
043import java.util.Collections;
044import java.util.List;
045import javax.enterprise.context.RequestScoped;
046
047@RequestScoped
048public class EditContentSectionCommand
049        extends BaseAbstractEditCommand<ContentSectionSpec, ContentSectionEdit, EditContentSectionResult, ContentSection, ContentSection> {
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.ContentSection.name(), SecurityRoles.Edit.name())
060                        )))
061                )));
062        
063        SPEC_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
064                new FieldDefinition("ContentCollectionName", FieldType.ENTITY_NAME, true, null, null),
065                new FieldDefinition("ContentSectionName", FieldType.ENTITY_NAME, true, null, null)
066                ));
067        
068        EDIT_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
069                new FieldDefinition("ContentSectionName", FieldType.ENTITY_NAME, true, null, null),
070                new FieldDefinition("ParentContentSectionName", FieldType.ENTITY_NAME, false, null, null),
071                new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null),
072                new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null),
073                new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L)
074                ));
075    }
076    
077    /** Creates a new instance of EditContentSectionCommand */
078    public EditContentSectionCommand() {
079        super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
080    }
081    
082    @Override
083    public EditContentSectionResult getResult() {
084        return ContentResultFactory.getEditContentSectionResult();
085    }
086    
087    @Override
088    public ContentSectionEdit getEdit() {
089        return ContentEditFactory.getContentSectionEdit();
090    }
091    
092    ContentCollection contentCollection = null;
093    
094    @Override
095    public ContentSection getEntity(EditContentSectionResult result) {
096        var contentControl = Session.getModelController(ContentControl.class);
097        ContentSection contentSection = null;
098        var contentCollectionName = spec.getContentCollectionName();
099        
100        contentCollection = contentControl.getContentCollectionByName(contentCollectionName);
101        
102        if(contentCollection != null) {
103            var contentSectionName = spec.getContentSectionName();
104
105            if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) {
106                contentSection = contentControl.getContentSectionByName(contentCollection, contentSectionName);
107            } else { // EditMode.UPDATE
108                contentSection = contentControl.getContentSectionByNameForUpdate(contentCollection, contentSectionName);
109            }
110
111            if(contentSection != null) {
112                result.setContentSection(contentControl.getContentSectionTransfer(getUserVisit(), contentSection));
113            } else {
114                addExecutionError(ExecutionErrors.UnknownContentSectionName.name(), contentSectionName);
115            }
116        } else {
117            addExecutionError(ExecutionErrors.UnknownContentCollectionName.name(), contentCollectionName);
118        }
119
120        return contentSection;
121    }
122    
123    @Override
124    public ContentSection getLockEntity(ContentSection contentSection) {
125        return contentSection;
126    }
127    
128    @Override
129    public void fillInResult(EditContentSectionResult result, ContentSection contentSection) {
130        var contentControl = Session.getModelController(ContentControl.class);
131        
132        result.setContentSection(contentControl.getContentSectionTransfer(getUserVisit(), contentSection));
133    }
134    
135    @Override
136    public void doLock(ContentSectionEdit edit, ContentSection contentSection) {
137        var contentControl = Session.getModelController(ContentControl.class);
138        var contentSectionDescription = contentControl.getContentSectionDescription(contentSection, getPreferredLanguage());
139        var contentSectionDetail = contentSection.getLastDetail();
140        var parentContentSection = contentSectionDetail.getParentContentSection();
141
142        edit.setContentSectionName(contentSectionDetail.getContentSectionName());
143        edit.setParentContentSectionName(parentContentSection == null? null: parentContentSection.getLastDetail().getContentSectionName());
144        edit.setIsDefault(contentSectionDetail.getIsDefault().toString());
145        edit.setSortOrder(contentSectionDetail.getSortOrder().toString());
146
147        if(contentSectionDescription != null) {
148            edit.setDescription(contentSectionDescription.getDescription());
149        }
150    }
151    
152    ContentSection parentContentSection = null;
153    
154    @Override
155    public void canUpdate(ContentSection contentSection) {
156        var contentControl = Session.getModelController(ContentControl.class);
157        var contentSectionName = edit.getContentSectionName();
158        var duplicateContentSection = contentControl.getContentSectionByName(contentCollection, contentSectionName);
159
160        if(duplicateContentSection == null || contentSection.equals(duplicateContentSection)) {
161            var parentContentSectionName = edit.getParentContentSectionName();
162            
163            parentContentSection = contentControl.getContentSectionByName(contentCollection, parentContentSectionName == null ?
164                    ContentSections.ROOT.toString() : parentContentSectionName);
165
166            if(parentContentSection != null) {
167                if(!contentControl.isParentContentSectionSafe(contentSection, parentContentSection)) {
168                    addExecutionError(ExecutionErrors.InvalidParentContentSection.name());
169                }
170            } else {
171                addExecutionError(ExecutionErrors.UnknownParentContentSectionName.name(), parentContentSectionName);
172            }
173        } else {
174            addExecutionError(ExecutionErrors.DuplicateContentSectionName.name(), contentSectionName);
175        }
176    }
177    
178    @Override
179    public void doUpdate(ContentSection contentSection) {
180        var contentControl = Session.getModelController(ContentControl.class);
181        var partyPK = getPartyPK();
182        var contentSectionDetailValue = contentControl.getContentSectionDetailValueForUpdate(contentSection);
183        var contentSectionDescription = contentControl.getContentSectionDescriptionForUpdate(contentSection, getPreferredLanguage());
184        var description = edit.getDescription();
185
186        contentSectionDetailValue.setContentSectionName(edit.getContentSectionName());
187        contentSectionDetailValue.setParentContentSectionPK(parentContentSection == null? null: parentContentSection.getPrimaryKey());
188        contentSectionDetailValue.setIsDefault(Boolean.valueOf(edit.getIsDefault()));
189        contentSectionDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder()));
190
191        contentControl.updateContentSectionFromValue(contentSectionDetailValue, partyPK);
192
193        if(contentSectionDescription == null && description != null) {
194            contentControl.createContentSectionDescription(contentSection, getPreferredLanguage(), description, partyPK);
195        } else if(contentSectionDescription != null && description == null) {
196            contentControl.deleteContentSectionDescription(contentSectionDescription, partyPK);
197        } else if(contentSectionDescription != null && description != null) {
198            var contentSectionDescriptionValue = contentControl.getContentSectionDescriptionValue(contentSectionDescription);
199
200            contentSectionDescriptionValue.setDescription(description);
201            contentControl.updateContentSectionDescriptionFromValue(contentSectionDescriptionValue, partyPK);
202        }
203    }
204    
205}