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.ContentCatalogDescriptionEdit;
020import com.echothree.control.user.content.common.edit.ContentEditFactory;
021import com.echothree.control.user.content.common.form.EditContentCatalogDescriptionForm;
022import com.echothree.control.user.content.common.result.ContentResultFactory;
023import com.echothree.control.user.content.common.result.EditContentCatalogDescriptionResult;
024import com.echothree.control.user.content.common.spec.ContentCatalogDescriptionSpec;
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.ContentCatalogDescription;
032import com.echothree.model.data.content.server.entity.ContentCollection;
033import com.echothree.model.data.content.server.value.ContentCatalogDescriptionValue;
034import com.echothree.model.data.party.server.entity.Language;
035import com.echothree.model.data.user.common.pk.UserVisitPK;
036import com.echothree.util.common.message.ExecutionErrors;
037import com.echothree.util.common.validation.FieldDefinition;
038import com.echothree.util.common.validation.FieldType;
039import com.echothree.util.common.command.EditMode;
040import com.echothree.util.server.control.BaseAbstractEditCommand;
041import com.echothree.util.server.control.CommandSecurityDefinition;
042import com.echothree.util.server.control.PartyTypeDefinition;
043import com.echothree.util.server.control.SecurityRoleDefinition;
044import com.echothree.util.server.persistence.Session;
045import java.util.Arrays;
046import java.util.Collections;
047import java.util.List;
048
049public class EditContentCatalogDescriptionCommand
050        extends BaseAbstractEditCommand<ContentCatalogDescriptionSpec, ContentCatalogDescriptionEdit, EditContentCatalogDescriptionResult, ContentCatalogDescription, ContentCatalog> {
051    
052    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
053    private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS;
054    private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS;
055    
056    static {
057        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(Collections.unmodifiableList(Arrays.asList(
058                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
059                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), Collections.unmodifiableList(Arrays.asList(
060                        new SecurityRoleDefinition(SecurityRoleGroups.ContentCatalog.name(), SecurityRoles.Description.name())
061                        )))
062                )));
063        
064        SPEC_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
065                new FieldDefinition("ContentCollectionName", FieldType.ENTITY_NAME, true, null, null),
066                new FieldDefinition("ContentCatalogName", FieldType.ENTITY_NAME, true, null, null),
067                new FieldDefinition("LanguageIsoName", FieldType.ENTITY_NAME, true, null, null)
068                ));
069        
070        EDIT_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
071                new FieldDefinition("Description", FieldType.STRING, true, 1L, 132L)
072                ));
073    }
074    
075    /** Creates a new instance of EditContentCatalogDescriptionCommand */
076    public EditContentCatalogDescriptionCommand(UserVisitPK userVisitPK, EditContentCatalogDescriptionForm form) {
077        super(userVisitPK, form, COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
078    }
079    
080    @Override
081    public EditContentCatalogDescriptionResult getResult() {
082        return ContentResultFactory.getEditContentCatalogDescriptionResult();
083    }
084    
085    @Override
086    public ContentCatalogDescriptionEdit getEdit() {
087        return ContentEditFactory.getContentCatalogDescriptionEdit();
088    }
089    
090    @Override
091    public ContentCatalogDescription getEntity(EditContentCatalogDescriptionResult result) {
092        var contentControl = Session.getModelController(ContentControl.class);
093        ContentCatalogDescription contentCatalogDescription = null;
094        String contentCollectionName = spec.getContentCollectionName();
095        ContentCollection contentCollection = contentControl.getContentCollectionByName(contentCollectionName);
096        
097        if(contentCollection != null) {
098            String contentCatalogName = spec.getContentCatalogName();
099            ContentCatalog contentCatalog = contentControl.getContentCatalogByName(contentCollection, contentCatalogName);
100            
101            if(contentCatalog != null) {
102                var partyControl = Session.getModelController(PartyControl.class);
103                String languageIsoName = spec.getLanguageIsoName();
104                Language language = partyControl.getLanguageByIsoName(languageIsoName);
105
106                result.setContentCatalog(contentControl.getContentCatalogTransfer(getUserVisit(), contentCatalog));
107
108                if(language != null) {
109                    if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) {
110                        contentCatalogDescription = contentControl.getContentCatalogDescription(contentCatalog, language);
111                    } else { // EditMode.UPDATE
112                        contentCatalogDescription = contentControl.getContentCatalogDescriptionForUpdate(contentCatalog, language);
113                    }
114
115                    if(contentCatalogDescription == null) {
116                        addExecutionError(ExecutionErrors.UnknownContentCatalogDescription.name());
117                    }
118                } else {
119                    addExecutionError(ExecutionErrors.UnknownLanguageIsoName.name(), languageIsoName);
120                }
121            } else {
122                addExecutionError(ExecutionErrors.UnknownContentCatalogName.name(), contentCatalogName);
123            }
124        } else {
125            addExecutionError(ExecutionErrors.UnknownContentCollectionName.name(), contentCollectionName);
126        }
127
128        return contentCatalogDescription;
129    }
130    
131    @Override
132    public ContentCatalog getLockEntity(ContentCatalogDescription contentCatalogDescription) {
133        return contentCatalogDescription.getContentCatalog();
134    }
135    
136    @Override
137    public void fillInResult(EditContentCatalogDescriptionResult result, ContentCatalogDescription contentCatalogDescription) {
138        var contentControl = Session.getModelController(ContentControl.class);
139        
140        result.setContentCatalogDescription(contentControl.getContentCatalogDescriptionTransfer(getUserVisit(), contentCatalogDescription));
141    }
142    
143    @Override
144    public void doLock(ContentCatalogDescriptionEdit edit, ContentCatalogDescription contentCatalogDescription) {
145        edit.setDescription(contentCatalogDescription.getDescription());
146    }
147    
148    @Override
149    public void doUpdate(ContentCatalogDescription contentCatalogDescription) {
150        var contentControl = Session.getModelController(ContentControl.class);
151        ContentCatalogDescriptionValue contentCatalogDescriptionValue = contentControl.getContentCatalogDescriptionValue(contentCatalogDescription);
152        contentCatalogDescriptionValue.setDescription(edit.getDescription());
153
154        contentControl.updateContentCatalogDescriptionFromValue(contentCatalogDescriptionValue, getPartyPK());
155    }
156    
157}