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.ContentCategoryDescriptionEdit;
020import com.echothree.control.user.content.common.edit.ContentEditFactory;
021import com.echothree.control.user.content.common.form.EditContentCategoryDescriptionForm;
022import com.echothree.control.user.content.common.result.ContentResultFactory;
023import com.echothree.control.user.content.common.result.EditContentCategoryDescriptionResult;
024import com.echothree.control.user.content.common.spec.ContentCategoryDescriptionSpec;
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.ContentCatalog;
031import com.echothree.model.data.content.server.entity.ContentCategory;
032import com.echothree.model.data.content.server.entity.ContentCategoryDescription;
033import com.echothree.model.data.content.server.entity.ContentCollection;
034import com.echothree.model.data.content.server.value.ContentCategoryDescriptionValue;
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 EditContentCategoryDescriptionCommand
051        extends BaseAbstractEditCommand<ContentCategoryDescriptionSpec, ContentCategoryDescriptionEdit, EditContentCategoryDescriptionResult, ContentCategoryDescription, ContentCategory> {
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.ContentCategory.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("ContentCatalogName", FieldType.ENTITY_NAME, true, null, null),
068                new FieldDefinition("ContentCategoryName", 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 EditContentCategoryDescriptionCommand */
078    public EditContentCategoryDescriptionCommand(UserVisitPK userVisitPK, EditContentCategoryDescriptionForm form) {
079        super(userVisitPK, form, COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
080    }
081    
082    @Override
083    public EditContentCategoryDescriptionResult getResult() {
084        return ContentResultFactory.getEditContentCategoryDescriptionResult();
085    }
086    
087    @Override
088    public ContentCategoryDescriptionEdit getEdit() {
089        return ContentEditFactory.getContentCategoryDescriptionEdit();
090    }
091    
092    @Override
093    public ContentCategoryDescription getEntity(EditContentCategoryDescriptionResult result) {
094        var contentControl = Session.getModelController(ContentControl.class);
095        ContentCategoryDescription contentCategoryDescription = null;
096        String contentCollectionName = spec.getContentCollectionName();
097        ContentCollection contentCollection = contentControl.getContentCollectionByName(contentCollectionName);
098        
099        if(contentCollection != null) {
100            String contentCatalogName = spec.getContentCatalogName();
101            ContentCatalog contentCatalog = contentControl.getContentCatalogByName(contentCollection, contentCatalogName);
102            
103            if(contentCatalog != null) {
104                String contentCategoryName = spec.getContentCategoryName();
105                ContentCategory contentCategory = contentControl.getContentCategoryByName(contentCatalog, contentCategoryName);
106                
107                if(contentCategory != null) {
108                    var partyControl = Session.getModelController(PartyControl.class);
109                    String languageIsoName = spec.getLanguageIsoName();
110                    Language language = partyControl.getLanguageByIsoName(languageIsoName);
111                    
112                    result.setContentCategory(contentControl.getContentCategoryTransfer(getUserVisit(), contentCategory));
113                    
114                    if(language != null) {
115                        if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) {
116                            contentCategoryDescription = contentControl.getContentCategoryDescription(contentCategory, language);
117                        } else { // EditMode.UPDATE
118                            contentCategoryDescription = contentControl.getContentCategoryDescriptionForUpdate(contentCategory, language);
119                        }
120
121                        if(contentCategoryDescription == null) {
122                            addExecutionError(ExecutionErrors.UnknownContentCategoryDescription.name());
123                        }
124                    } else {
125                        addExecutionError(ExecutionErrors.UnknownLanguageIsoName.name(), languageIsoName);
126                    }
127                } else {
128                    addExecutionError(ExecutionErrors.UnknownContentCategoryName.name(), contentCategoryName);
129                }
130            } else {
131                addExecutionError(ExecutionErrors.UnknownContentCatalogName.name(), contentCatalogName);
132            }
133        } else {
134            addExecutionError(ExecutionErrors.UnknownContentCollectionName.name(), contentCollectionName);
135        }
136
137        return contentCategoryDescription;
138    }
139    
140    @Override
141    public ContentCategory getLockEntity(ContentCategoryDescription contentCategoryDescription) {
142        return contentCategoryDescription.getContentCategory();
143    }
144    
145    @Override
146    public void fillInResult(EditContentCategoryDescriptionResult result, ContentCategoryDescription contentCategoryDescription) {
147        var contentControl = Session.getModelController(ContentControl.class);
148        
149        result.setContentCategoryDescription(contentControl.getContentCategoryDescriptionTransfer(getUserVisit(), contentCategoryDescription));
150    }
151    
152    @Override
153    public void doLock(ContentCategoryDescriptionEdit edit, ContentCategoryDescription contentCategoryDescription) {
154        edit.setDescription(contentCategoryDescription.getDescription());
155    }
156    
157    @Override
158    public void doUpdate(ContentCategoryDescription contentCategoryDescription) {
159        var contentControl = Session.getModelController(ContentControl.class);
160        ContentCategoryDescriptionValue contentCategoryDescriptionValue = contentControl.getContentCategoryDescriptionValue(contentCategoryDescription);
161        contentCategoryDescriptionValue.setDescription(edit.getDescription());
162
163        contentControl.updateContentCategoryDescriptionFromValue(contentCategoryDescriptionValue, getPartyPK());
164    }
165    
166}