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.order.server.command; 018 019import com.echothree.control.user.order.common.edit.OrderEditFactory; 020import com.echothree.control.user.order.common.edit.OrderPriorityEdit; 021import com.echothree.control.user.order.common.form.EditOrderPriorityForm; 022import com.echothree.control.user.order.common.result.EditOrderPriorityResult; 023import com.echothree.control.user.order.common.result.OrderResultFactory; 024import com.echothree.control.user.order.common.spec.OrderPriorityUniversalSpec; 025import com.echothree.model.control.order.server.control.OrderPriorityControl; 026import com.echothree.model.control.order.server.control.OrderTypeControl; 027import com.echothree.model.control.order.server.logic.OrderPriorityLogic; 028import com.echothree.model.control.party.common.PartyTypes; 029import com.echothree.model.control.security.common.SecurityRoleGroups; 030import com.echothree.model.control.security.common.SecurityRoles; 031import com.echothree.model.data.order.server.entity.OrderPriority; 032import com.echothree.model.data.user.common.pk.UserVisitPK; 033import com.echothree.util.common.command.EditMode; 034import com.echothree.util.common.message.ExecutionErrors; 035import com.echothree.util.common.validation.FieldDefinition; 036import com.echothree.util.common.validation.FieldType; 037import com.echothree.util.server.control.BaseAbstractEditCommand; 038import com.echothree.util.server.control.CommandSecurityDefinition; 039import com.echothree.util.server.control.PartyTypeDefinition; 040import com.echothree.util.server.control.SecurityRoleDefinition; 041import com.echothree.util.server.persistence.Session; 042import java.util.List; 043import javax.enterprise.context.Dependent; 044 045@Dependent 046public class EditOrderPriorityCommand 047 extends BaseAbstractEditCommand<OrderPriorityUniversalSpec, OrderPriorityEdit, EditOrderPriorityResult, OrderPriority, OrderPriority> { 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(List.of( 055 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 056 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of( 057 new SecurityRoleDefinition(SecurityRoleGroups.OrderPriority.name(), SecurityRoles.Edit.name()) 058 )) 059 )); 060 061 SPEC_FIELD_DEFINITIONS = List.of( 062 new FieldDefinition("OrderTypeName", FieldType.ENTITY_NAME, false, null, null), 063 new FieldDefinition("OrderPriorityName", FieldType.ENTITY_NAME, false, null, null), 064 new FieldDefinition("EntityRef", FieldType.ENTITY_REF, false, null, null), 065 new FieldDefinition("Uuid", FieldType.UUID, false, null, null) 066 ); 067 068 EDIT_FIELD_DEFINITIONS = List.of( 069 new FieldDefinition("OrderPriorityName", FieldType.ENTITY_NAME, true, null, null), 070 new FieldDefinition("Priority", FieldType.UNSIGNED_INTEGER, true, null, null), 071 new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null), 072 new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null), 073 new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L) 074 ); 075 } 076 077 /** Creates a new instance of EditOrderPriorityCommand */ 078 public EditOrderPriorityCommand() { 079 super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 080 } 081 082 @Override 083 public EditOrderPriorityResult getResult() { 084 return OrderResultFactory.getEditOrderPriorityResult(); 085 } 086 087 @Override 088 public OrderPriorityEdit getEdit() { 089 return OrderEditFactory.getOrderPriorityEdit(); 090 } 091 092 @Override 093 public OrderPriority getEntity(EditOrderPriorityResult result) { 094 OrderPriority orderPriority; 095 096 if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) { 097 orderPriority = OrderPriorityLogic.getInstance().getOrderPriorityByUniversalSpec(this, spec, false); 098 } else { // EditMode.UPDATE 099 orderPriority = OrderPriorityLogic.getInstance().getOrderPriorityByUniversalSpecForUpdate(this, spec, false); 100 } 101 102 103 return orderPriority; 104 } 105 106 @Override 107 public OrderPriority getLockEntity(OrderPriority orderPriority) { 108 return orderPriority; 109 } 110 111 @Override 112 public void fillInResult(EditOrderPriorityResult result, OrderPriority orderPriority) { 113 var orderPriorityControl = Session.getModelController(OrderPriorityControl.class); 114 115 result.setOrderPriority(orderPriorityControl.getOrderPriorityTransfer(getUserVisit(), orderPriority)); 116 } 117 118 @Override 119 public void doLock(OrderPriorityEdit edit, OrderPriority orderPriority) { 120 var orderPriorityControl = Session.getModelController(OrderPriorityControl.class); 121 var orderPriorityDescription = orderPriorityControl.getOrderPriorityDescription(orderPriority, getPreferredLanguage()); 122 var orderPriorityDetail = orderPriority.getLastDetail(); 123 124 edit.setOrderPriorityName(orderPriorityDetail.getOrderPriorityName()); 125 edit.setPriority(orderPriorityDetail.getPriority().toString()); 126 edit.setIsDefault(orderPriorityDetail.getIsDefault().toString()); 127 edit.setSortOrder(orderPriorityDetail.getSortOrder().toString()); 128 129 if(orderPriorityDescription != null) { 130 edit.setDescription(orderPriorityDescription.getDescription()); 131 } 132 } 133 134 @Override 135 public void canUpdate(OrderPriority orderPriority) { 136 var orderTypeControl = Session.getModelController(OrderTypeControl.class); 137 var orderTypeName = spec.getOrderTypeName(); 138 var orderType = orderTypeControl.getOrderTypeByName(orderTypeName); 139 140 if(orderType != null) { 141 var orderPriorityControl = Session.getModelController(OrderPriorityControl.class); 142 var orderPriorityName = edit.getOrderPriorityName(); 143 var duplicateOrderPriority = orderPriorityControl.getOrderPriorityByName(orderType, orderPriorityName); 144 145 if(duplicateOrderPriority != null && !orderPriority.equals(duplicateOrderPriority)) { 146 addExecutionError(ExecutionErrors.DuplicateOrderPriorityName.name(), orderTypeName, orderPriorityName); 147 } 148 } else { 149 addExecutionError(ExecutionErrors.UnknownOrderTypeName.name(), orderTypeName); 150 } 151 } 152 153 @Override 154 public void doUpdate(OrderPriority orderPriority) { 155 var orderPriorityControl = Session.getModelController(OrderPriorityControl.class); 156 var partyPK = getPartyPK(); 157 var orderPriorityDetailValue = orderPriorityControl.getOrderPriorityDetailValueForUpdate(orderPriority); 158 var orderPriorityDescription = orderPriorityControl.getOrderPriorityDescriptionForUpdate(orderPriority, getPreferredLanguage()); 159 var description = edit.getDescription(); 160 161 orderPriorityDetailValue.setOrderPriorityName(edit.getOrderPriorityName()); 162 orderPriorityDetailValue.setPriority(Integer.valueOf(edit.getPriority())); 163 orderPriorityDetailValue.setIsDefault(Boolean.valueOf(edit.getIsDefault())); 164 orderPriorityDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder())); 165 166 OrderPriorityLogic.getInstance().updateOrderPriorityFromValue(this, orderPriorityDetailValue, partyPK); 167 168 if(orderPriorityDescription == null && description != null) { 169 orderPriorityControl.createOrderPriorityDescription(orderPriority, getPreferredLanguage(), description, partyPK); 170 } else { 171 if(orderPriorityDescription != null && description == null) { 172 orderPriorityControl.deleteOrderPriorityDescription(orderPriorityDescription, partyPK); 173 } else { 174 if(orderPriorityDescription != null && description != null) { 175 var orderPriorityDescriptionValue = orderPriorityControl.getOrderPriorityDescriptionValue(orderPriorityDescription); 176 177 orderPriorityDescriptionValue.setDescription(description); 178 orderPriorityControl.updateOrderPriorityDescriptionFromValue(orderPriorityDescriptionValue, partyPK); 179 } 180 } 181 } 182 } 183 184}