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