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.ContentPageDescriptionEdit;
021import com.echothree.control.user.content.common.form.EditContentPageDescriptionForm;
022import com.echothree.control.user.content.common.result.ContentResultFactory;
023import com.echothree.control.user.content.common.result.EditContentPageDescriptionResult;
024import com.echothree.control.user.content.common.spec.ContentPageDescriptionSpec;
025import com.echothree.model.control.content.server.control.ContentControl;
026import com.echothree.model.control.party.common.PartyTypes;
027import com.echothree.model.control.party.server.control.PartyControl;
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.ContentPage;
032import com.echothree.model.data.content.server.entity.ContentPageDescription;
033import com.echothree.model.data.content.server.entity.ContentSection;
034import com.echothree.model.data.content.server.value.ContentPageDescriptionValue;
035import com.echothree.model.data.party.server.entity.Language;
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 EditContentPageDescriptionCommand
051        extends BaseAbstractEditCommand<ContentPageDescriptionSpec, ContentPageDescriptionEdit, EditContentPageDescriptionResult, ContentPageDescription, ContentPage> {
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.ContentPage.name(), SecurityRoles.Description.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                new FieldDefinition("ContentPageName", FieldType.ENTITY_NAME, true, null, null),
069                new FieldDefinition("LanguageIsoName", FieldType.ENTITY_NAME, true, null, null)
070                ));
071        
072        EDIT_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
073                new FieldDefinition("Description", FieldType.STRING, true, 1L, 132L)
074                ));
075    }
076    
077    /** Creates a new instance of EditContentPageDescriptionCommand */
078    public EditContentPageDescriptionCommand(UserVisitPK userVisitPK, EditContentPageDescriptionForm form) {
079        super(userVisitPK, form, COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
080    }
081    
082    @Override
083    public EditContentPageDescriptionResult getResult() {
084        return ContentResultFactory.getEditContentPageDescriptionResult();
085    }
086    
087    @Override
088    public ContentPageDescriptionEdit getEdit() {
089        return ContentEditFactory.getContentPageDescriptionEdit();
090    }
091    
092    @Override
093    public ContentPageDescription getEntity(EditContentPageDescriptionResult result) {
094        var contentControl = Session.getModelController(ContentControl.class);
095        ContentPageDescription contentPageDescription = null;
096        String contentCollectionName = spec.getContentCollectionName();
097        ContentCollection contentCollection = contentControl.getContentCollectionByName(contentCollectionName);
098        
099        if(contentCollection != null) {
100            String contentSectionName = spec.getContentSectionName();
101            ContentSection contentSection = contentControl.getContentSectionByName(contentCollection, contentSectionName);
102            
103            if(contentSection != null) {
104                String contentPageName = spec.getContentPageName();
105                ContentPage contentPage = contentControl.getContentPageByName(contentSection, contentPageName);
106                
107                if(contentPage != null) {
108                    var partyControl = Session.getModelController(PartyControl.class);
109                    String languageIsoName = spec.getLanguageIsoName();
110                    Language language = partyControl.getLanguageByIsoName(languageIsoName);
111                    
112                    result.setContentPage(contentControl.getContentPageTransfer(getUserVisit(), contentPage));
113                    
114                    if(language != null) {
115                        if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) {
116                            contentPageDescription = contentControl.getContentPageDescription(contentPage, language);
117                        } else { // EditMode.UPDATE
118                            contentPageDescription = contentControl.getContentPageDescriptionForUpdate(contentPage, language);
119                        }
120
121                        if(contentPageDescription == null) {
122                            addExecutionError(ExecutionErrors.UnknownContentPageDescription.name());
123                        }
124                    } else {
125                        addExecutionError(ExecutionErrors.UnknownLanguageIsoName.name(), languageIsoName);
126                    }
127                } else {
128                    addExecutionError(ExecutionErrors.UnknownContentPageName.name(), contentPageName);
129                }
130            } else {
131                addExecutionError(ExecutionErrors.UnknownContentSectionName.name(), contentSectionName);
132            }
133        } else {
134            addExecutionError(ExecutionErrors.UnknownContentCollectionName.name(), contentCollectionName);
135        }
136
137        return contentPageDescription;
138    }
139    
140    @Override
141    public ContentPage getLockEntity(ContentPageDescription contentPageDescription) {
142        return contentPageDescription.getContentPage();
143    }
144    
145    @Override
146    public void fillInResult(EditContentPageDescriptionResult result, ContentPageDescription contentPageDescription) {
147        var contentControl = Session.getModelController(ContentControl.class);
148        
149        result.setContentPageDescription(contentControl.getContentPageDescriptionTransfer(getUserVisit(), contentPageDescription));
150    }
151    
152    @Override
153    public void doLock(ContentPageDescriptionEdit edit, ContentPageDescription contentPageDescription) {
154        edit.setDescription(contentPageDescription.getDescription());
155    }
156    
157    @Override
158    public void doUpdate(ContentPageDescription contentPageDescription) {
159        var contentControl = Session.getModelController(ContentControl.class);
160        ContentPageDescriptionValue contentPageDescriptionValue = contentControl.getContentPageDescriptionValue(contentPageDescription);
161        contentPageDescriptionValue.setDescription(edit.getDescription());
162
163        contentControl.updateContentPageDescriptionFromValue(contentPageDescriptionValue, getPartyPK());
164    }
165    
166}