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.sales.server.command; 018 019import com.echothree.control.user.sales.common.edit.SalesEditFactory; 020import com.echothree.control.user.sales.common.edit.SalesOrderTimeEdit; 021import com.echothree.control.user.sales.common.form.EditSalesOrderTimeForm; 022import com.echothree.control.user.sales.common.result.EditSalesOrderTimeResult; 023import com.echothree.control.user.sales.common.result.SalesResultFactory; 024import com.echothree.control.user.sales.common.spec.SalesOrderTimeSpec; 025import com.echothree.model.control.order.server.control.OrderTimeControl; 026import com.echothree.model.control.sales.server.logic.SalesOrderLogic; 027import com.echothree.model.data.order.server.entity.Order; 028import com.echothree.model.data.order.server.entity.OrderTime; 029import com.echothree.model.data.user.common.pk.UserVisitPK; 030import com.echothree.util.common.command.EditMode; 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.server.control.BaseAbstractEditCommand; 035import com.echothree.util.server.persistence.Session; 036import com.echothree.util.server.string.DateUtils; 037import java.util.List; 038import javax.enterprise.context.Dependent; 039 040@Dependent 041public class EditSalesOrderTimeCommand 042 extends BaseAbstractEditCommand<SalesOrderTimeSpec, SalesOrderTimeEdit, EditSalesOrderTimeResult, OrderTime, Order> { 043 044 private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS; 045 private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS; 046 047 static { 048 SPEC_FIELD_DEFINITIONS = List.of( 049 new FieldDefinition("OrderName", FieldType.ENTITY_NAME, true, null, null), 050 new FieldDefinition("OrderTimeTypeName", FieldType.ENTITY_NAME, true, null, null) 051 ); 052 053 EDIT_FIELD_DEFINITIONS = List.of( 054 new FieldDefinition("Time", FieldType.DATE_TIME, true, null, null) 055 ); 056 } 057 058 /** Creates a new instance of EditSalesOrderTimeCommand */ 059 public EditSalesOrderTimeCommand() { 060 super(null, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 061 } 062 063 @Override 064 public EditSalesOrderTimeResult getResult() { 065 return SalesResultFactory.getEditSalesOrderTimeResult(); 066 } 067 068 @Override 069 public SalesOrderTimeEdit getEdit() { 070 return SalesEditFactory.getSalesOrderTimeEdit(); 071 } 072 073 @Override 074 public OrderTime getEntity(EditSalesOrderTimeResult result) { 075 var orderName = spec.getOrderName(); 076 var order = SalesOrderLogic.getInstance().getOrderByName(this, orderName); 077 OrderTime orderTime = null; 078 079 if(!hasExecutionErrors()) { 080 var orderTimeControl = Session.getModelController(OrderTimeControl.class); 081 var orderType = order.getLastDetail().getOrderType(); 082 var orderTimeTypeName = spec.getOrderTimeTypeName(); 083 var orderTimeType = orderTimeControl.getOrderTimeTypeByName(orderType, orderTimeTypeName); 084 085 if(orderTimeType != null) { 086 if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) { 087 orderTime = orderTimeControl.getOrderTime(order, orderTimeType); 088 } else { // EditMode.UPDATE 089 orderTime = orderTimeControl.getOrderTimeForUpdate(order, orderTimeType); 090 } 091 092 if(orderTime == null) { 093 addExecutionError(ExecutionErrors.UnknownOrderTime.name(), orderType.getLastDetail().getOrderTypeName(), orderName, orderTimeTypeName); 094 } 095 } 096 } 097 098 return orderTime; 099 } 100 101 @Override 102 public Order getLockEntity(OrderTime orderTime) { 103 return orderTime.getOrder(); 104 } 105 106 @Override 107 public void fillInResult(EditSalesOrderTimeResult result, OrderTime orderTime) { 108 var orderTimeControl = Session.getModelController(OrderTimeControl.class); 109 110 result.setOrderTime(orderTimeControl.getOrderTimeTransfer(getUserVisit(), orderTime)); 111 } 112 113 @Override 114 public void doLock(SalesOrderTimeEdit edit, OrderTime orderTime) { 115 edit.setTime(DateUtils.getInstance().formatTypicalDateTime(getUserVisit(), getPreferredDateTimeFormat(), orderTime.getTime())); 116 } 117 118 @Override 119 public void doUpdate(OrderTime orderTime) { 120 var orderTimeControl = Session.getModelController(OrderTimeControl.class); 121 var orderTimeValue = orderTimeControl.getOrderTimeValue(orderTime); 122 var time = Long.valueOf(edit.getTime()); 123 124 orderTimeValue.setTime(time); 125 126 orderTimeControl.updateOrderTimeFromValue(orderTimeValue, getPartyPK()); 127 } 128 129}