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