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