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 java.util.List; 041import javax.enterprise.context.Dependent; 042import javax.inject.Inject; 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 @Inject 073 IndexControl indexControl; 074 075 /** Creates a new instance of EditIndexFieldCommand */ 076 public EditIndexFieldCommand() { 077 super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 078 } 079 080 @Override 081 public EditIndexFieldResult getResult() { 082 return IndexResultFactory.getEditIndexFieldResult(); 083 } 084 085 @Override 086 public IndexFieldEdit getEdit() { 087 return IndexEditFactory.getIndexFieldEdit(); 088 } 089 090 IndexType indexType; 091 092 @Override 093 public IndexField getEntity(EditIndexFieldResult result) { 094 IndexField indexField = null; 095 var indexTypeName = spec.getIndexTypeName(); 096 097 indexType = indexControl.getIndexTypeByName(indexTypeName); 098 099 if(indexType != null) { 100 var indexFieldName = spec.getIndexFieldName(); 101 102 if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) { 103 indexField = indexControl.getIndexFieldByName(indexType, indexFieldName); 104 } else { // EditMode.UPDATE 105 indexField = indexControl.getIndexFieldByNameForUpdate(indexType, indexFieldName); 106 } 107 108 if(indexField == null) { 109 addExecutionError(ExecutionErrors.UnknownIndexFieldName.name(), indexTypeName, indexFieldName); 110 } 111 } else { 112 addExecutionError(ExecutionErrors.UnknownIndexTypeName.name(), indexTypeName); 113 } 114 115 return indexField; 116 } 117 118 @Override 119 public IndexField getLockEntity(IndexField indexField) { 120 return indexField; 121 } 122 123 @Override 124 public void fillInResult(EditIndexFieldResult result, IndexField indexField) { 125 result.setIndexField(indexControl.getIndexFieldTransfer(getUserVisit(), indexField)); 126 } 127 128 @Override 129 public void doLock(IndexFieldEdit edit, IndexField indexField) { 130 var indexFieldDescription = indexControl.getIndexFieldDescription(indexField, getPreferredLanguage()); 131 var indexFieldDetail = indexField.getLastDetail(); 132 133 edit.setIndexFieldName(indexFieldDetail.getIndexFieldName()); 134 edit.setIsDefault(indexFieldDetail.getIsDefault().toString()); 135 edit.setSortOrder(indexFieldDetail.getSortOrder().toString()); 136 137 if(indexFieldDescription != null) { 138 edit.setDescription(indexFieldDescription.getDescription()); 139 } 140 } 141 142 @Override 143 public void canUpdate(IndexField indexField) { 144 var indexTypeDetail = indexType.getLastDetail(); 145 var indexFieldName = edit.getIndexFieldName(); 146 var duplicateIndexField = indexControl.getIndexFieldByName(indexType, indexFieldName); 147 148 if(duplicateIndexField != null && !indexField.equals(duplicateIndexField)) { 149 addExecutionError(ExecutionErrors.DuplicateIndexFieldName.name(), indexTypeDetail.getIndexTypeName(), indexFieldName); 150 } 151 } 152 153 @Override 154 public void doUpdate(IndexField indexField) { 155 var partyPK = getPartyPK(); 156 var indexFieldDetailValue = indexControl.getIndexFieldDetailValueForUpdate(indexField); 157 var indexFieldDescription = indexControl.getIndexFieldDescriptionForUpdate(indexField, getPreferredLanguage()); 158 var description = edit.getDescription(); 159 160 indexFieldDetailValue.setIndexFieldName(edit.getIndexFieldName()); 161 indexFieldDetailValue.setIsDefault(Boolean.valueOf(edit.getIsDefault())); 162 indexFieldDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder())); 163 164 indexControl.updateIndexFieldFromValue(indexFieldDetailValue, partyPK); 165 166 if(indexFieldDescription == null && description != null) { 167 indexControl.createIndexFieldDescription(indexField, getPreferredLanguage(), description, partyPK); 168 } else if(indexFieldDescription != null && description == null) { 169 indexControl.deleteIndexFieldDescription(indexFieldDescription, partyPK); 170 } else if(indexFieldDescription != null && description != null) { 171 var indexFieldDescriptionValue = indexControl.getIndexFieldDescriptionValue(indexFieldDescription); 172 173 indexFieldDescriptionValue.setDescription(description); 174 indexControl.updateIndexFieldDescriptionFromValue(indexFieldDescriptionValue, partyPK); 175 } 176 } 177 178}