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