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