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.IndexFieldEdit;
021import com.echothree.control.user.index.common.form.EditIndexFieldForm;
022import com.echothree.control.user.index.common.result.EditIndexFieldResult;
023import com.echothree.control.user.index.common.result.IndexResultFactory;
024import com.echothree.control.user.index.common.spec.IndexFieldSpec;
025import com.echothree.model.control.index.server.control.IndexControl;
026import com.echothree.model.control.party.common.PartyTypes;
027import com.echothree.model.control.security.common.SecurityRoleGroups;
028import com.echothree.model.control.security.common.SecurityRoles;
029import com.echothree.model.data.index.server.entity.IndexField;
030import com.echothree.model.data.index.server.entity.IndexFieldDescription;
031import com.echothree.model.data.index.server.entity.IndexFieldDetail;
032import com.echothree.model.data.index.server.entity.IndexType;
033import com.echothree.model.data.index.server.entity.IndexTypeDetail;
034import com.echothree.model.data.index.server.value.IndexFieldDescriptionValue;
035import com.echothree.model.data.index.server.value.IndexFieldDetailValue;
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 EditIndexFieldCommand
051        extends BaseAbstractEditCommand<IndexFieldSpec, IndexFieldEdit, EditIndexFieldResult, IndexField, IndexField> {
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.IndexField.name(), SecurityRoles.Edit.name())
062                        )))
063                )));
064
065        SPEC_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
066                new FieldDefinition("IndexFieldName", FieldType.ENTITY_NAME, true, null, null)
067                ));
068
069        EDIT_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
070                new FieldDefinition("IndexFieldName", FieldType.ENTITY_NAME, true, null, null),
071                new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null),
072                new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null),
073                new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L)
074                ));
075    }
076
077    /** Creates a new instance of EditIndexFieldCommand */
078    public EditIndexFieldCommand(UserVisitPK userVisitPK, EditIndexFieldForm form) {
079        super(userVisitPK, form, COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
080    }
081
082    @Override
083    public EditIndexFieldResult getResult() {
084        return IndexResultFactory.getEditIndexFieldResult();
085    }
086
087    @Override
088    public IndexFieldEdit getEdit() {
089        return IndexEditFactory.getIndexFieldEdit();
090    }
091
092    IndexType indexType;
093
094    @Override
095    public IndexField getEntity(EditIndexFieldResult result) {
096        var indexControl = Session.getModelController(IndexControl.class);
097        IndexField indexField = null;
098        String indexTypeName = spec.getIndexTypeName();
099
100        indexType = indexControl.getIndexTypeByName(indexTypeName);
101
102        if(indexType != null) {
103            String indexFieldName = spec.getIndexFieldName();
104
105            if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) {
106                indexField = indexControl.getIndexFieldByName(indexType, indexFieldName);
107            } else { // EditMode.UPDATE
108                indexField = indexControl.getIndexFieldByNameForUpdate(indexType, indexFieldName);
109            }
110
111            if(indexField == null) {
112                addExecutionError(ExecutionErrors.UnknownIndexFieldName.name(), indexTypeName, indexFieldName);
113            }
114        } else {
115            addExecutionError(ExecutionErrors.UnknownIndexTypeName.name(), indexTypeName);
116        }
117
118        return indexField;
119    }
120
121    @Override
122    public IndexField getLockEntity(IndexField indexField) {
123        return indexField;
124    }
125
126    @Override
127    public void fillInResult(EditIndexFieldResult result, IndexField indexField) {
128        var indexControl = Session.getModelController(IndexControl.class);
129
130        result.setIndexField(indexControl.getIndexFieldTransfer(getUserVisit(), indexField));
131    }
132
133    @Override
134    public void doLock(IndexFieldEdit edit, IndexField indexField) {
135        var indexControl = Session.getModelController(IndexControl.class);
136        IndexFieldDescription indexFieldDescription = indexControl.getIndexFieldDescription(indexField, getPreferredLanguage());
137        IndexFieldDetail indexFieldDetail = indexField.getLastDetail();
138
139        edit.setIndexFieldName(indexFieldDetail.getIndexFieldName());
140        edit.setIsDefault(indexFieldDetail.getIsDefault().toString());
141        edit.setSortOrder(indexFieldDetail.getSortOrder().toString());
142
143        if(indexFieldDescription != null) {
144            edit.setDescription(indexFieldDescription.getDescription());
145        }
146    }
147
148    @Override
149    public void canUpdate(IndexField indexField) {
150        var indexControl = Session.getModelController(IndexControl.class);
151        IndexTypeDetail indexTypeDetail = indexType.getLastDetail();
152        String indexFieldName = edit.getIndexFieldName();
153        IndexField duplicateIndexField = indexControl.getIndexFieldByName(indexType, indexFieldName);
154
155        if(duplicateIndexField != null && !indexField.equals(duplicateIndexField)) {
156            addExecutionError(ExecutionErrors.DuplicateIndexFieldName.name(), indexTypeDetail.getIndexTypeName(), indexFieldName);
157        }
158    }
159
160    @Override
161    public void doUpdate(IndexField indexField) {
162        var indexControl = Session.getModelController(IndexControl.class);
163        var partyPK = getPartyPK();
164        IndexFieldDetailValue indexFieldDetailValue = indexControl.getIndexFieldDetailValueForUpdate(indexField);
165        IndexFieldDescription indexFieldDescription = indexControl.getIndexFieldDescriptionForUpdate(indexField, getPreferredLanguage());
166        String description = edit.getDescription();
167
168        indexFieldDetailValue.setIndexFieldName(edit.getIndexFieldName());
169        indexFieldDetailValue.setIsDefault(Boolean.valueOf(edit.getIsDefault()));
170        indexFieldDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder()));
171
172        indexControl.updateIndexFieldFromValue(indexFieldDetailValue, partyPK);
173
174        if(indexFieldDescription == null && description != null) {
175            indexControl.createIndexFieldDescription(indexField, getPreferredLanguage(), description, partyPK);
176        } else if(indexFieldDescription != null && description == null) {
177            indexControl.deleteIndexFieldDescription(indexFieldDescription, partyPK);
178        } else if(indexFieldDescription != null && description != null) {
179            IndexFieldDescriptionValue indexFieldDescriptionValue = indexControl.getIndexFieldDescriptionValue(indexFieldDescription);
180
181            indexFieldDescriptionValue.setDescription(description);
182            indexControl.updateIndexFieldDescriptionFromValue(indexFieldDescriptionValue, partyPK);
183        }
184    }
185
186}