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.IndexEdit;
020import com.echothree.control.user.index.common.edit.IndexEditFactory;
021import com.echothree.control.user.index.common.form.EditIndexForm;
022import com.echothree.control.user.index.common.result.EditIndexResult;
023import com.echothree.control.user.index.common.result.IndexResultFactory;
024import com.echothree.control.user.index.common.spec.IndexSpec;
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.Index;
030import com.echothree.model.data.user.common.pk.UserVisitPK;
031import com.echothree.util.common.message.ExecutionErrors;
032import com.echothree.util.common.validation.FieldDefinition;
033import com.echothree.util.common.validation.FieldType;
034import com.echothree.util.common.command.EditMode;
035import com.echothree.util.server.control.BaseAbstractEditCommand;
036import com.echothree.util.server.control.CommandSecurityDefinition;
037import com.echothree.util.server.control.PartyTypeDefinition;
038import com.echothree.util.server.control.SecurityRoleDefinition;
039import com.echothree.util.server.persistence.Session;
040import java.util.List;
041import javax.enterprise.context.Dependent;
042
043@Dependent
044public class EditIndexCommand
045        extends BaseAbstractEditCommand<IndexSpec, IndexEdit, EditIndexResult, Index, Index> {
046    
047    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
048    private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS;
049    private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS;
050    
051    static {
052        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of(
053                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
054                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of(
055                        new SecurityRoleDefinition(SecurityRoleGroups.Index.name(), SecurityRoles.Edit.name())
056                        ))
057                ));
058        
059        SPEC_FIELD_DEFINITIONS = List.of(
060                new FieldDefinition("IndexName", FieldType.ENTITY_NAME, true, null, null)
061                );
062        
063        EDIT_FIELD_DEFINITIONS = List.of(
064                new FieldDefinition("IndexName", FieldType.ENTITY_NAME, true, null, null),
065                new FieldDefinition("Directory", FieldType.STRING, true, null, 80L),
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 EditIndexCommand */
073    public EditIndexCommand() {
074        super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
075    }
076    
077    @Override
078    public EditIndexResult getResult() {
079        return IndexResultFactory.getEditIndexResult();
080    }
081
082    @Override
083    public IndexEdit getEdit() {
084        return IndexEditFactory.getIndexEdit();
085    }
086
087    @Override
088    public Index getEntity(EditIndexResult result) {
089        var indexControl = Session.getModelController(IndexControl.class);
090        Index index;
091        var indexName = spec.getIndexName();
092
093        if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) {
094            index = indexControl.getIndexByName(indexName);
095        } else { // EditMode.UPDATE
096            index = indexControl.getIndexByNameForUpdate(indexName);
097        }
098
099        if(index == null) {
100            addExecutionError(ExecutionErrors.UnknownIndexName.name(), indexName);
101        }
102
103        return index;
104    }
105
106    @Override
107    public Index getLockEntity(Index index) {
108        return index;
109    }
110
111    @Override
112    public void fillInResult(EditIndexResult result, Index index) {
113        var indexControl = Session.getModelController(IndexControl.class);
114
115        result.setIndex(indexControl.getIndexTransfer(getUserVisit(), index));
116    }
117
118    @Override
119    public void doLock(IndexEdit edit, Index index) {
120        var indexControl = Session.getModelController(IndexControl.class);
121        var indexDescription = indexControl.getIndexDescription(index, getPreferredLanguage());
122        var indexDetail = index.getLastDetail();
123
124        edit.setIndexName(indexDetail.getIndexName());
125        edit.setIsDefault(indexDetail.getIsDefault().toString());
126        edit.setSortOrder(indexDetail.getSortOrder().toString());
127
128        if(indexDescription != null) {
129            edit.setDescription(indexDescription.getDescription());
130        }
131    }
132
133    @Override
134    public void canUpdate(Index index) {
135        var indexControl = Session.getModelController(IndexControl.class);
136        var indexName = edit.getIndexName();
137        var duplicateIndex = indexControl.getIndexByName(indexName);
138
139        if(duplicateIndex != null && !index.equals(duplicateIndex)) {
140            addExecutionError(ExecutionErrors.DuplicateIndexName.name(), indexName);
141        }
142    }
143
144    @Override
145    public void doUpdate(Index index) {
146        var indexControl = Session.getModelController(IndexControl.class);
147        var partyPK = getPartyPK();
148        var indexDetailValue = indexControl.getIndexDetailValueForUpdate(index);
149        var indexDescription = indexControl.getIndexDescriptionForUpdate(index, getPreferredLanguage());
150        var description = edit.getDescription();
151
152        indexDetailValue.setIndexName(edit.getIndexName());
153        indexDetailValue.setDirectory(edit.getDirectory());
154        indexDetailValue.setIsDefault(Boolean.valueOf(edit.getIsDefault()));
155        indexDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder()));
156
157        indexControl.updateIndexFromValue(indexDetailValue, partyPK);
158
159        if(indexDescription == null && description != null) {
160            indexControl.createIndexDescription(index, getPreferredLanguage(), description, partyPK);
161        } else {
162            if(indexDescription != null && description == null) {
163                indexControl.deleteIndexDescription(indexDescription, partyPK);
164            } else {
165                if(indexDescription != null && description != null) {
166                    var indexDescriptionValue = indexControl.getIndexDescriptionValue(indexDescription);
167
168                    indexDescriptionValue.setDescription(description);
169                    indexControl.updateIndexDescriptionFromValue(indexDescriptionValue, partyPK);
170                }
171            }
172        }
173    }
174    
175}