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.index.server.command;
018
019import com.echothree.control.user.index.common.edit.IndexEditFactory;
020import com.echothree.control.user.index.common.edit.IndexFieldDescriptionEdit;
021import com.echothree.control.user.index.common.form.EditIndexFieldDescriptionForm;
022import com.echothree.control.user.index.common.result.EditIndexFieldDescriptionResult;
023import com.echothree.control.user.index.common.result.IndexResultFactory;
024import com.echothree.control.user.index.common.spec.IndexFieldDescriptionSpec;
025import com.echothree.model.control.index.server.control.IndexControl;
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.index.server.entity.IndexField;
031import com.echothree.model.data.index.server.entity.IndexFieldDescription;
032import com.echothree.model.data.index.server.entity.IndexType;
033import com.echothree.model.data.index.server.value.IndexFieldDescriptionValue;
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 EditIndexFieldDescriptionCommand
050        extends BaseAbstractEditCommand<IndexFieldDescriptionSpec, IndexFieldDescriptionEdit, EditIndexFieldDescriptionResult, IndexFieldDescription, IndexField> {
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.IndexField.name(), SecurityRoles.Description.name())
061                        )))
062                )));
063
064        SPEC_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
065                new FieldDefinition("IndexFieldName", FieldType.ENTITY_NAME, true, null, null),
066                new FieldDefinition("LanguageIsoName", FieldType.ENTITY_NAME, true, null, null)
067                ));
068
069        EDIT_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
070                new FieldDefinition("Description", FieldType.STRING, true, 1L, 132L)
071                ));
072    }
073
074    /** Creates a new instance of EditIndexFieldDescriptionCommand */
075    public EditIndexFieldDescriptionCommand(UserVisitPK userVisitPK, EditIndexFieldDescriptionForm form) {
076        super(userVisitPK, form, COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
077    }
078
079    @Override
080    public EditIndexFieldDescriptionResult getResult() {
081        return IndexResultFactory.getEditIndexFieldDescriptionResult();
082    }
083
084    @Override
085    public IndexFieldDescriptionEdit getEdit() {
086        return IndexEditFactory.getIndexFieldDescriptionEdit();
087    }
088
089    @Override
090    public IndexFieldDescription getEntity(EditIndexFieldDescriptionResult result) {
091        var indexControl = Session.getModelController(IndexControl.class);
092        IndexFieldDescription indexFieldDescription = null;
093        String indexTypeName = spec.getIndexTypeName();
094        IndexType indexType = indexControl.getIndexTypeByName(indexTypeName);
095
096        if(indexType != null) {
097            String indexFieldName = spec.getIndexFieldName();
098            IndexField indexField = indexControl.getIndexFieldByName(indexType, indexFieldName);
099
100            if(indexField != null) {
101                var partyControl = Session.getModelController(PartyControl.class);
102                String languageIsoName = spec.getLanguageIsoName();
103                Language language = partyControl.getLanguageByIsoName(languageIsoName);
104
105                if(language != null) {
106                    if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) {
107                        indexFieldDescription = indexControl.getIndexFieldDescription(indexField, language);
108                    } else { // EditMode.UPDATE
109                        indexFieldDescription = indexControl.getIndexFieldDescriptionForUpdate(indexField, language);
110                    }
111
112                    if(indexFieldDescription == null) {
113                        addExecutionError(ExecutionErrors.UnknownIndexFieldDescription.name(), indexTypeName, indexFieldName, languageIsoName);
114                    }
115                } else {
116                    addExecutionError(ExecutionErrors.UnknownLanguageIsoName.name(), languageIsoName);
117                }
118            } else {
119                addExecutionError(ExecutionErrors.UnknownIndexFieldName.name(), indexTypeName, indexFieldName);
120            }
121        } else {
122            addExecutionError(ExecutionErrors.UnknownIndexTypeName.name(), indexTypeName);
123        }
124
125        return indexFieldDescription;
126    }
127
128    @Override
129    public IndexField getLockEntity(IndexFieldDescription indexFieldDescription) {
130        return indexFieldDescription.getIndexField();
131    }
132
133    @Override
134    public void fillInResult(EditIndexFieldDescriptionResult result, IndexFieldDescription indexFieldDescription) {
135        var indexControl = Session.getModelController(IndexControl.class);
136
137        result.setIndexFieldDescription(indexControl.getIndexFieldDescriptionTransfer(getUserVisit(), indexFieldDescription));
138    }
139
140    @Override
141    public void doLock(IndexFieldDescriptionEdit edit, IndexFieldDescription indexFieldDescription) {
142        edit.setDescription(indexFieldDescription.getDescription());
143    }
144
145    @Override
146    public void doUpdate(IndexFieldDescription indexFieldDescription) {
147        var indexControl = Session.getModelController(IndexControl.class);
148        IndexFieldDescriptionValue indexFieldDescriptionValue = indexControl.getIndexFieldDescriptionValue(indexFieldDescription);
149
150        indexFieldDescriptionValue.setDescription(edit.getDescription());
151
152        indexControl.updateIndexFieldDescriptionFromValue(indexFieldDescriptionValue, getPartyPK());
153    }
154
155}