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.search.server.command; 018 019import com.echothree.control.user.search.common.edit.SearchEditFactory; 020import com.echothree.control.user.search.common.edit.SearchSortOrderEdit; 021import com.echothree.control.user.search.common.form.EditSearchSortOrderForm; 022import com.echothree.control.user.search.common.result.EditSearchSortOrderResult; 023import com.echothree.control.user.search.common.result.SearchResultFactory; 024import com.echothree.control.user.search.common.spec.SearchSortOrderSpec; 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.SearchKind; 030import com.echothree.model.data.search.server.entity.SearchSortOrder; 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 EditSearchSortOrderCommand 048 extends BaseAbstractEditCommand<SearchSortOrderSpec, SearchSortOrderEdit, EditSearchSortOrderResult, SearchSortOrder, SearchSortOrder> { 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.SearchSortOrder.name(), SecurityRoles.Edit.name()) 059 ))) 060 ))); 061 062 SPEC_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 063 new FieldDefinition("SearchSortOrderName", FieldType.ENTITY_NAME, true, null, null) 064 )); 065 066 EDIT_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 067 new FieldDefinition("SearchSortOrderName", 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 EditSearchSortOrderCommand */ 075 public EditSearchSortOrderCommand() { 076 super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 077 } 078 079 @Override 080 public EditSearchSortOrderResult getResult() { 081 return SearchResultFactory.getEditSearchSortOrderResult(); 082 } 083 084 @Override 085 public SearchSortOrderEdit getEdit() { 086 return SearchEditFactory.getSearchSortOrderEdit(); 087 } 088 089 SearchKind searchKind; 090 091 @Override 092 public SearchSortOrder getEntity(EditSearchSortOrderResult result) { 093 var searchControl = Session.getModelController(SearchControl.class); 094 SearchSortOrder searchSortOrder = null; 095 var searchKindName = spec.getSearchKindName(); 096 097 searchKind = searchControl.getSearchKindByName(searchKindName); 098 099 if(searchKind != null) { 100 var searchSortOrderName = spec.getSearchSortOrderName(); 101 102 if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) { 103 searchSortOrder = searchControl.getSearchSortOrderByName(searchKind, searchSortOrderName); 104 } else { // EditMode.UPDATE 105 searchSortOrder = searchControl.getSearchSortOrderByNameForUpdate(searchKind, searchSortOrderName); 106 } 107 108 if(searchSortOrder == null) { 109 addExecutionError(ExecutionErrors.UnknownSearchSortOrderName.name(), searchKindName, searchSortOrderName); 110 } 111 } else { 112 addExecutionError(ExecutionErrors.UnknownSearchKindName.name(), searchKindName); 113 } 114 115 return searchSortOrder; 116 } 117 118 @Override 119 public SearchSortOrder getLockEntity(SearchSortOrder searchSortOrder) { 120 return searchSortOrder; 121 } 122 123 @Override 124 public void fillInResult(EditSearchSortOrderResult result, SearchSortOrder searchSortOrder) { 125 var searchControl = Session.getModelController(SearchControl.class); 126 127 result.setSearchSortOrder(searchControl.getSearchSortOrderTransfer(getUserVisit(), searchSortOrder)); 128 } 129 130 @Override 131 public void doLock(SearchSortOrderEdit edit, SearchSortOrder searchSortOrder) { 132 var searchControl = Session.getModelController(SearchControl.class); 133 var searchSortOrderDescription = searchControl.getSearchSortOrderDescription(searchSortOrder, getPreferredLanguage()); 134 var searchSortOrderDetail = searchSortOrder.getLastDetail(); 135 136 edit.setSearchSortOrderName(searchSortOrderDetail.getSearchSortOrderName()); 137 edit.setIsDefault(searchSortOrderDetail.getIsDefault().toString()); 138 edit.setSortOrder(searchSortOrderDetail.getSortOrder().toString()); 139 140 if(searchSortOrderDescription != null) { 141 edit.setDescription(searchSortOrderDescription.getDescription()); 142 } 143 } 144 145 @Override 146 public void canUpdate(SearchSortOrder searchSortOrder) { 147 var searchControl = Session.getModelController(SearchControl.class); 148 var searchKindDetail = searchKind.getLastDetail(); 149 var searchSortOrderName = edit.getSearchSortOrderName(); 150 var duplicateSearchSortOrder = searchControl.getSearchSortOrderByName(searchKind, searchSortOrderName); 151 152 if(duplicateSearchSortOrder != null && !searchSortOrder.equals(duplicateSearchSortOrder)) { 153 addExecutionError(ExecutionErrors.DuplicateSearchSortOrderName.name(), searchKindDetail.getSearchKindName(), searchSortOrderName); 154 } 155 } 156 157 @Override 158 public void doUpdate(SearchSortOrder searchSortOrder) { 159 var searchControl = Session.getModelController(SearchControl.class); 160 var partyPK = getPartyPK(); 161 var searchSortOrderDetailValue = searchControl.getSearchSortOrderDetailValueForUpdate(searchSortOrder); 162 var searchSortOrderDescription = searchControl.getSearchSortOrderDescriptionForUpdate(searchSortOrder, getPreferredLanguage()); 163 var description = edit.getDescription(); 164 165 searchSortOrderDetailValue.setSearchSortOrderName(edit.getSearchSortOrderName()); 166 searchSortOrderDetailValue.setIsDefault(Boolean.valueOf(edit.getIsDefault())); 167 searchSortOrderDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder())); 168 169 searchControl.updateSearchSortOrderFromValue(searchSortOrderDetailValue, partyPK); 170 171 if(searchSortOrderDescription == null && description != null) { 172 searchControl.createSearchSortOrderDescription(searchSortOrder, getPreferredLanguage(), description, partyPK); 173 } else if(searchSortOrderDescription != null && description == null) { 174 searchControl.deleteSearchSortOrderDescription(searchSortOrderDescription, partyPK); 175 } else if(searchSortOrderDescription != null && description != null) { 176 var searchSortOrderDescriptionValue = searchControl.getSearchSortOrderDescriptionValue(searchSortOrderDescription); 177 178 searchSortOrderDescriptionValue.setDescription(description); 179 searchControl.updateSearchSortOrderDescriptionFromValue(searchSortOrderDescriptionValue, partyPK); 180 } 181 } 182 183}