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.search.server.command; 018 019import com.echothree.control.user.search.common.edit.SearchDefaultOperatorEdit; 020import com.echothree.control.user.search.common.edit.SearchEditFactory; 021import com.echothree.control.user.search.common.form.EditSearchDefaultOperatorForm; 022import com.echothree.control.user.search.common.result.EditSearchDefaultOperatorResult; 023import com.echothree.control.user.search.common.result.SearchResultFactory; 024import com.echothree.control.user.search.common.spec.SearchDefaultOperatorSpec; 025import com.echothree.model.control.party.common.PartyTypes; 026import com.echothree.model.control.search.server.control.SearchControl; 027import com.echothree.model.control.security.common.SecurityRoleGroups; 028import com.echothree.model.control.security.common.SecurityRoles; 029import com.echothree.model.data.search.server.entity.SearchDefaultOperator; 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 EditSearchDefaultOperatorCommand 045 extends BaseAbstractEditCommand<SearchDefaultOperatorSpec, SearchDefaultOperatorEdit, EditSearchDefaultOperatorResult, SearchDefaultOperator, SearchDefaultOperator> { 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.SearchDefaultOperator.name(), SecurityRoles.Edit.name()) 056 )) 057 )); 058 059 SPEC_FIELD_DEFINITIONS = List.of( 060 new FieldDefinition("SearchDefaultOperatorName", FieldType.ENTITY_NAME, true, null, null) 061 ); 062 063 EDIT_FIELD_DEFINITIONS = List.of( 064 new FieldDefinition("SearchDefaultOperatorName", FieldType.ENTITY_NAME, true, null, null), 065 new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null), 066 new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null), 067 new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L) 068 ); 069 } 070 071 @Inject 072 SearchControl searchControl; 073 074 075 /** Creates a new instance of EditSearchDefaultOperatorCommand */ 076 public EditSearchDefaultOperatorCommand() { 077 super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 078 } 079 080 @Override 081 public EditSearchDefaultOperatorResult getResult() { 082 return SearchResultFactory.getEditSearchDefaultOperatorResult(); 083 } 084 085 @Override 086 public SearchDefaultOperatorEdit getEdit() { 087 return SearchEditFactory.getSearchDefaultOperatorEdit(); 088 } 089 090 @Override 091 public SearchDefaultOperator getEntity(EditSearchDefaultOperatorResult result) { 092 SearchDefaultOperator searchDefaultOperator; 093 var searchDefaultOperatorName = spec.getSearchDefaultOperatorName(); 094 095 if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) { 096 searchDefaultOperator = searchControl.getSearchDefaultOperatorByName(searchDefaultOperatorName); 097 } else { // EditMode.UPDATE 098 searchDefaultOperator = searchControl.getSearchDefaultOperatorByNameForUpdate(searchDefaultOperatorName); 099 } 100 101 if(searchDefaultOperator == null) { 102 addExecutionError(ExecutionErrors.UnknownSearchDefaultOperatorName.name(), searchDefaultOperatorName); 103 } 104 105 return searchDefaultOperator; 106 } 107 108 @Override 109 public SearchDefaultOperator getLockEntity(SearchDefaultOperator searchDefaultOperator) { 110 return searchDefaultOperator; 111 } 112 113 @Override 114 public void fillInResult(EditSearchDefaultOperatorResult result, SearchDefaultOperator searchDefaultOperator) { 115 result.setSearchDefaultOperator(searchControl.getSearchDefaultOperatorTransfer(getUserVisit(), searchDefaultOperator)); 116 } 117 118 @Override 119 public void doLock(SearchDefaultOperatorEdit edit, SearchDefaultOperator searchDefaultOperator) { 120 var searchDefaultOperatorDescription = searchControl.getSearchDefaultOperatorDescription(searchDefaultOperator, getPreferredLanguage()); 121 var searchDefaultOperatorDetail = searchDefaultOperator.getLastDetail(); 122 123 edit.setSearchDefaultOperatorName(searchDefaultOperatorDetail.getSearchDefaultOperatorName()); 124 edit.setIsDefault(searchDefaultOperatorDetail.getIsDefault().toString()); 125 edit.setSortOrder(searchDefaultOperatorDetail.getSortOrder().toString()); 126 127 if(searchDefaultOperatorDescription != null) { 128 edit.setDescription(searchDefaultOperatorDescription.getDescription()); 129 } 130 } 131 132 @Override 133 public void canUpdate(SearchDefaultOperator searchDefaultOperator) { 134 var searchDefaultOperatorName = edit.getSearchDefaultOperatorName(); 135 var duplicateSearchDefaultOperator = searchControl.getSearchDefaultOperatorByName(searchDefaultOperatorName); 136 137 if(duplicateSearchDefaultOperator != null && !searchDefaultOperator.equals(duplicateSearchDefaultOperator)) { 138 addExecutionError(ExecutionErrors.DuplicateSearchDefaultOperatorName.name(), searchDefaultOperatorName); 139 } 140 } 141 142 @Override 143 public void doUpdate(SearchDefaultOperator searchDefaultOperator) { 144 var partyPK = getPartyPK(); 145 var searchDefaultOperatorDetailValue = searchControl.getSearchDefaultOperatorDetailValueForUpdate(searchDefaultOperator); 146 var searchDefaultOperatorDescription = searchControl.getSearchDefaultOperatorDescriptionForUpdate(searchDefaultOperator, getPreferredLanguage()); 147 var description = edit.getDescription(); 148 149 searchDefaultOperatorDetailValue.setSearchDefaultOperatorName(edit.getSearchDefaultOperatorName()); 150 searchDefaultOperatorDetailValue.setIsDefault(Boolean.valueOf(edit.getIsDefault())); 151 searchDefaultOperatorDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder())); 152 153 searchControl.updateSearchDefaultOperatorFromValue(searchDefaultOperatorDetailValue, partyPK); 154 155 if(searchDefaultOperatorDescription == null && description != null) { 156 searchControl.createSearchDefaultOperatorDescription(searchDefaultOperator, getPreferredLanguage(), description, partyPK); 157 } else { 158 if(searchDefaultOperatorDescription != null && description == null) { 159 searchControl.deleteSearchDefaultOperatorDescription(searchDefaultOperatorDescription, partyPK); 160 } else { 161 if(searchDefaultOperatorDescription != null && description != null) { 162 var searchDefaultOperatorDescriptionValue = searchControl.getSearchDefaultOperatorDescriptionValue(searchDefaultOperatorDescription); 163 164 searchDefaultOperatorDescriptionValue.setDescription(description); 165 searchControl.updateSearchDefaultOperatorDescriptionFromValue(searchDefaultOperatorDescriptionValue, partyPK); 166 } 167 } 168 } 169 } 170 171}