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 java.util.List;
040import javax.enterprise.context.Dependent;
041import javax.inject.Inject;
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    @Inject
073    IndexControl indexControl;
074
075    
076    /** Creates a new instance of EditIndexCommand */
077    public EditIndexCommand() {
078        super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
079    }
080    
081    @Override
082    public EditIndexResult getResult() {
083        return IndexResultFactory.getEditIndexResult();
084    }
085
086    @Override
087    public IndexEdit getEdit() {
088        return IndexEditFactory.getIndexEdit();
089    }
090
091    @Override
092    public Index getEntity(EditIndexResult result) {
093        Index index;
094        var indexName = spec.getIndexName();
095
096        if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) {
097            index = indexControl.getIndexByName(indexName);
098        } else { // EditMode.UPDATE
099            index = indexControl.getIndexByNameForUpdate(indexName);
100        }
101
102        if(index == null) {
103            addExecutionError(ExecutionErrors.UnknownIndexName.name(), indexName);
104        }
105
106        return index;
107    }
108
109    @Override
110    public Index getLockEntity(Index index) {
111        return index;
112    }
113
114    @Override
115    public void fillInResult(EditIndexResult result, Index index) {
116        result.setIndex(indexControl.getIndexTransfer(getUserVisit(), index));
117    }
118
119    @Override
120    public void doLock(IndexEdit edit, Index index) {
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 indexName = edit.getIndexName();
136        var duplicateIndex = indexControl.getIndexByName(indexName);
137
138        if(duplicateIndex != null && !index.equals(duplicateIndex)) {
139            addExecutionError(ExecutionErrors.DuplicateIndexName.name(), indexName);
140        }
141    }
142
143    @Override
144    public void doUpdate(Index index) {
145        var partyPK = getPartyPK();
146        var indexDetailValue = indexControl.getIndexDetailValueForUpdate(index);
147        var indexDescription = indexControl.getIndexDescriptionForUpdate(index, getPreferredLanguage());
148        var description = edit.getDescription();
149
150        indexDetailValue.setIndexName(edit.getIndexName());
151        indexDetailValue.setDirectory(edit.getDirectory());
152        indexDetailValue.setIsDefault(Boolean.valueOf(edit.getIsDefault()));
153        indexDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder()));
154
155        indexControl.updateIndexFromValue(indexDetailValue, partyPK);
156
157        if(indexDescription == null && description != null) {
158            indexControl.createIndexDescription(index, getPreferredLanguage(), description, partyPK);
159        } else {
160            if(indexDescription != null && description == null) {
161                indexControl.deleteIndexDescription(indexDescription, partyPK);
162            } else {
163                if(indexDescription != null && description != null) {
164                    var indexDescriptionValue = indexControl.getIndexDescriptionValue(indexDescription);
165
166                    indexDescriptionValue.setDescription(description);
167                    indexControl.updateIndexDescriptionFromValue(indexDescriptionValue, partyPK);
168                }
169            }
170        }
171    }
172    
173}