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.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 com.echothree.util.server.persistence.Session; 041import java.util.Arrays; 042import java.util.Collections; 043import java.util.List; 044import javax.enterprise.context.RequestScoped; 045 046@RequestScoped 047public class EditIndexFieldCommand 048 extends BaseAbstractEditCommand<IndexFieldSpec, IndexFieldEdit, EditIndexFieldResult, IndexField, IndexField> { 049 050 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 051 private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS; 052 private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS; 053 054 static { 055 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(Collections.unmodifiableList(Arrays.asList( 056 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 057 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), Collections.unmodifiableList(Arrays.asList( 058 new SecurityRoleDefinition(SecurityRoleGroups.IndexField.name(), SecurityRoles.Edit.name()) 059 ))) 060 ))); 061 062 SPEC_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 063 new FieldDefinition("IndexFieldName", FieldType.ENTITY_NAME, true, null, null) 064 )); 065 066 EDIT_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 067 new FieldDefinition("IndexFieldName", FieldType.ENTITY_NAME, true, null, null), 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 EditIndexFieldCommand */ 075 public EditIndexFieldCommand() { 076 super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 077 } 078 079 @Override 080 public EditIndexFieldResult getResult() { 081 return IndexResultFactory.getEditIndexFieldResult(); 082 } 083 084 @Override 085 public IndexFieldEdit getEdit() { 086 return IndexEditFactory.getIndexFieldEdit(); 087 } 088 089 IndexType indexType; 090 091 @Override 092 public IndexField getEntity(EditIndexFieldResult result) { 093 var indexControl = Session.getModelController(IndexControl.class); 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 var indexControl = Session.getModelController(IndexControl.class); 126 127 result.setIndexField(indexControl.getIndexFieldTransfer(getUserVisit(), indexField)); 128 } 129 130 @Override 131 public void doLock(IndexFieldEdit edit, IndexField indexField) { 132 var indexControl = Session.getModelController(IndexControl.class); 133 var indexFieldDescription = indexControl.getIndexFieldDescription(indexField, getPreferredLanguage()); 134 var indexFieldDetail = indexField.getLastDetail(); 135 136 edit.setIndexFieldName(indexFieldDetail.getIndexFieldName()); 137 edit.setIsDefault(indexFieldDetail.getIsDefault().toString()); 138 edit.setSortOrder(indexFieldDetail.getSortOrder().toString()); 139 140 if(indexFieldDescription != null) { 141 edit.setDescription(indexFieldDescription.getDescription()); 142 } 143 } 144 145 @Override 146 public void canUpdate(IndexField indexField) { 147 var indexControl = Session.getModelController(IndexControl.class); 148 var indexTypeDetail = indexType.getLastDetail(); 149 var indexFieldName = edit.getIndexFieldName(); 150 var duplicateIndexField = indexControl.getIndexFieldByName(indexType, indexFieldName); 151 152 if(duplicateIndexField != null && !indexField.equals(duplicateIndexField)) { 153 addExecutionError(ExecutionErrors.DuplicateIndexFieldName.name(), indexTypeDetail.getIndexTypeName(), indexFieldName); 154 } 155 } 156 157 @Override 158 public void doUpdate(IndexField indexField) { 159 var indexControl = Session.getModelController(IndexControl.class); 160 var partyPK = getPartyPK(); 161 var indexFieldDetailValue = indexControl.getIndexFieldDetailValueForUpdate(indexField); 162 var indexFieldDescription = indexControl.getIndexFieldDescriptionForUpdate(indexField, getPreferredLanguage()); 163 var description = edit.getDescription(); 164 165 indexFieldDetailValue.setIndexFieldName(edit.getIndexFieldName()); 166 indexFieldDetailValue.setIsDefault(Boolean.valueOf(edit.getIsDefault())); 167 indexFieldDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder())); 168 169 indexControl.updateIndexFieldFromValue(indexFieldDetailValue, partyPK); 170 171 if(indexFieldDescription == null && description != null) { 172 indexControl.createIndexFieldDescription(indexField, getPreferredLanguage(), description, partyPK); 173 } else if(indexFieldDescription != null && description == null) { 174 indexControl.deleteIndexFieldDescription(indexFieldDescription, partyPK); 175 } else if(indexFieldDescription != null && description != null) { 176 var indexFieldDescriptionValue = indexControl.getIndexFieldDescriptionValue(indexFieldDescription); 177 178 indexFieldDescriptionValue.setDescription(description); 179 indexControl.updateIndexFieldDescriptionFromValue(indexFieldDescriptionValue, partyPK); 180 } 181 } 182 183}