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