001// --------------------------------------------------------------------------------
002// Copyright 2002-2026 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.IndexType;
031import com.echothree.model.data.user.common.pk.UserVisitPK;
032import com.echothree.util.common.message.ExecutionErrors;
033import com.echothree.util.common.validation.FieldDefinition;
034import com.echothree.util.common.validation.FieldType;
035import com.echothree.util.common.command.EditMode;
036import com.echothree.util.server.control.BaseAbstractEditCommand;
037import com.echothree.util.server.control.CommandSecurityDefinition;
038import com.echothree.util.server.control.PartyTypeDefinition;
039import com.echothree.util.server.control.SecurityRoleDefinition;
040import com.echothree.util.server.persistence.Session;
041import java.util.List;
042import javax.enterprise.context.Dependent;
043
044@Dependent
045public class EditIndexFieldCommand
046        extends BaseAbstractEditCommand<IndexFieldSpec, IndexFieldEdit, EditIndexFieldResult, IndexField, IndexField> {
047
048    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
049    private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS;
050    private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS;
051
052    static {
053        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of(
054                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
055                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of(
056                        new SecurityRoleDefinition(SecurityRoleGroups.IndexField.name(), SecurityRoles.Edit.name())
057                        ))
058                ));
059
060        SPEC_FIELD_DEFINITIONS = List.of(
061                new FieldDefinition("IndexFieldName", FieldType.ENTITY_NAME, true, null, null)
062                );
063
064        EDIT_FIELD_DEFINITIONS = List.of(
065                new FieldDefinition("IndexFieldName", FieldType.ENTITY_NAME, true, null, null),
066                new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null),
067                new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null),
068                new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L)
069                );
070    }
071
072    /** Creates a new instance of EditIndexFieldCommand */
073    public EditIndexFieldCommand() {
074        super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
075    }
076
077    @Override
078    public EditIndexFieldResult getResult() {
079        return IndexResultFactory.getEditIndexFieldResult();
080    }
081
082    @Override
083    public IndexFieldEdit getEdit() {
084        return IndexEditFactory.getIndexFieldEdit();
085    }
086
087    IndexType indexType;
088
089    @Override
090    public IndexField getEntity(EditIndexFieldResult result) {
091        var indexControl = Session.getModelController(IndexControl.class);
092        IndexField indexField = null;
093        var indexTypeName = spec.getIndexTypeName();
094
095        indexType = indexControl.getIndexTypeByName(indexTypeName);
096
097        if(indexType != null) {
098            var indexFieldName = spec.getIndexFieldName();
099
100            if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) {
101                indexField = indexControl.getIndexFieldByName(indexType, indexFieldName);
102            } else { // EditMode.UPDATE
103                indexField = indexControl.getIndexFieldByNameForUpdate(indexType, indexFieldName);
104            }
105
106            if(indexField == null) {
107                addExecutionError(ExecutionErrors.UnknownIndexFieldName.name(), indexTypeName, indexFieldName);
108            }
109        } else {
110            addExecutionError(ExecutionErrors.UnknownIndexTypeName.name(), indexTypeName);
111        }
112
113        return indexField;
114    }
115
116    @Override
117    public IndexField getLockEntity(IndexField indexField) {
118        return indexField;
119    }
120
121    @Override
122    public void fillInResult(EditIndexFieldResult result, IndexField indexField) {
123        var indexControl = Session.getModelController(IndexControl.class);
124
125        result.setIndexField(indexControl.getIndexFieldTransfer(getUserVisit(), indexField));
126    }
127
128    @Override
129    public void doLock(IndexFieldEdit edit, IndexField indexField) {
130        var indexControl = Session.getModelController(IndexControl.class);
131        var indexFieldDescription = indexControl.getIndexFieldDescription(indexField, getPreferredLanguage());
132        var indexFieldDetail = indexField.getLastDetail();
133
134        edit.setIndexFieldName(indexFieldDetail.getIndexFieldName());
135        edit.setIsDefault(indexFieldDetail.getIsDefault().toString());
136        edit.setSortOrder(indexFieldDetail.getSortOrder().toString());
137
138        if(indexFieldDescription != null) {
139            edit.setDescription(indexFieldDescription.getDescription());
140        }
141    }
142
143    @Override
144    public void canUpdate(IndexField indexField) {
145        var indexControl = Session.getModelController(IndexControl.class);
146        var indexTypeDetail = indexType.getLastDetail();
147        var indexFieldName = edit.getIndexFieldName();
148        var duplicateIndexField = indexControl.getIndexFieldByName(indexType, indexFieldName);
149
150        if(duplicateIndexField != null && !indexField.equals(duplicateIndexField)) {
151            addExecutionError(ExecutionErrors.DuplicateIndexFieldName.name(), indexTypeDetail.getIndexTypeName(), indexFieldName);
152        }
153    }
154
155    @Override
156    public void doUpdate(IndexField indexField) {
157        var indexControl = Session.getModelController(IndexControl.class);
158        var partyPK = getPartyPK();
159        var indexFieldDetailValue = indexControl.getIndexFieldDetailValueForUpdate(indexField);
160        var indexFieldDescription = indexControl.getIndexFieldDescriptionForUpdate(indexField, getPreferredLanguage());
161        var description = edit.getDescription();
162
163        indexFieldDetailValue.setIndexFieldName(edit.getIndexFieldName());
164        indexFieldDetailValue.setIsDefault(Boolean.valueOf(edit.getIsDefault()));
165        indexFieldDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder()));
166
167        indexControl.updateIndexFieldFromValue(indexFieldDetailValue, partyPK);
168
169        if(indexFieldDescription == null && description != null) {
170            indexControl.createIndexFieldDescription(indexField, getPreferredLanguage(), description, partyPK);
171        } else if(indexFieldDescription != null && description == null) {
172            indexControl.deleteIndexFieldDescription(indexFieldDescription, partyPK);
173        } else if(indexFieldDescription != null && description != null) {
174            var indexFieldDescriptionValue = indexControl.getIndexFieldDescriptionValue(indexFieldDescription);
175
176            indexFieldDescriptionValue.setDescription(description);
177            indexControl.updateIndexFieldDescriptionFromValue(indexFieldDescriptionValue, partyPK);
178        }
179    }
180
181}