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.item.server.command; 018 019import com.echothree.control.user.item.common.edit.ItemEditFactory; 020import com.echothree.control.user.item.common.edit.ItemShippingTimeEdit; 021import com.echothree.control.user.item.common.form.EditItemShippingTimeForm; 022import com.echothree.control.user.item.common.result.EditItemShippingTimeResult; 023import com.echothree.control.user.item.common.result.ItemResultFactory; 024import com.echothree.control.user.item.common.spec.ItemShippingTimeSpec; 025import com.echothree.model.control.customer.server.control.CustomerControl; 026import com.echothree.model.control.item.server.control.ItemControl; 027import com.echothree.model.data.item.server.entity.Item; 028import com.echothree.model.data.item.server.entity.ItemShippingTime; 029import com.echothree.model.data.user.common.pk.UserVisitPK; 030import com.echothree.util.common.message.ExecutionErrors; 031import com.echothree.util.common.validation.FieldDefinition; 032import com.echothree.util.common.validation.FieldType; 033import com.echothree.util.common.command.EditMode; 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 EditItemShippingTimeCommand 044 extends BaseAbstractEditCommand<ItemShippingTimeSpec, ItemShippingTimeEdit, EditItemShippingTimeResult, ItemShippingTime, Item> { 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("ItemName", FieldType.ENTITY_NAME, true, null, null), 052 new FieldDefinition("CustomerTypeName", FieldType.ENTITY_NAME, true, null, null) 053 )); 054 055 EDIT_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 056 new FieldDefinition("ShippingStartTime", FieldType.DATE_TIME, true, null, null), 057 new FieldDefinition("ShippingEndTime", FieldType.DATE_TIME, false, null, null) 058 )); 059 } 060 061 /** Creates a new instance of EditItemShippingTimeCommand */ 062 public EditItemShippingTimeCommand() { 063 super(null, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 064 } 065 066 @Override 067 public EditItemShippingTimeResult getResult() { 068 return ItemResultFactory.getEditItemShippingTimeResult(); 069 } 070 071 @Override 072 public ItemShippingTimeEdit getEdit() { 073 return ItemEditFactory.getItemShippingTimeEdit(); 074 } 075 076 @Override 077 public ItemShippingTime getEntity(EditItemShippingTimeResult result) { 078 var itemControl = Session.getModelController(ItemControl.class); 079 ItemShippingTime itemShippingTime = null; 080 var itemName = spec.getItemName(); 081 var item = itemControl.getItemByName(itemName); 082 083 if(item != null) { 084 var customerControl = Session.getModelController(CustomerControl.class); 085 var customerTypeName = spec.getCustomerTypeName(); 086 var customerType = customerControl.getCustomerTypeByName(customerTypeName); 087 088 if(customerType != null) { 089 if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) { 090 itemShippingTime = itemControl.getItemShippingTime(item, customerType); 091 } else { // EditMode.UPDATE 092 itemShippingTime = itemControl.getItemShippingTimeForUpdate(item, customerType); 093 } 094 095 if(itemShippingTime == null) { 096 addExecutionError(ExecutionErrors.UnknownItemShippingTime.name(), itemName, customerTypeName); 097 } 098 } else { 099 addExecutionError(ExecutionErrors.UnknownCustomerTypeName.name(), customerTypeName); 100 } 101 } else { 102 addExecutionError(ExecutionErrors.UnknownItemName.name(), itemName); 103 } 104 105 return itemShippingTime; 106 } 107 108 @Override 109 public Item getLockEntity(ItemShippingTime itemShippingTime) { 110 return itemShippingTime.getItem(); 111 } 112 113 @Override 114 public void fillInResult(EditItemShippingTimeResult result, ItemShippingTime itemShippingTime) { 115 var itemControl = Session.getModelController(ItemControl.class); 116 117 result.setItemShippingTime(itemControl.getItemShippingTimeTransfer(getUserVisit(), itemShippingTime)); 118 } 119 120 @Override 121 public void doLock(ItemShippingTimeEdit edit, ItemShippingTime itemShippingTime) { 122 var dateUtils = DateUtils.getInstance(); 123 var shippingEndTime = itemShippingTime.getShippingEndTime(); 124 var preferredDateTimeFormat = getPreferredDateTimeFormat(); 125 var userVisit = getUserVisit(); 126 127 edit.setShippingStartTime(dateUtils.formatTypicalDateTime(userVisit, preferredDateTimeFormat, itemShippingTime.getShippingStartTime())); 128 edit.setShippingEndTime(shippingEndTime == null? null: dateUtils.formatTypicalDateTime(userVisit, preferredDateTimeFormat, shippingEndTime)); 129 } 130 131 @Override 132 public void doUpdate(ItemShippingTime itemShippingTime) { 133 var itemControl = Session.getModelController(ItemControl.class); 134 var itemShippingTimeValue = itemControl.getItemShippingTimeValue(itemShippingTime); 135 var strShippingEndTime = edit.getShippingEndTime(); 136 var shippingEndTime = strShippingEndTime == null? null: Long.valueOf(strShippingEndTime); 137 138 itemShippingTimeValue.setShippingStartTime(Long.valueOf(edit.getShippingStartTime())); 139 itemShippingTimeValue.setShippingEndTime(shippingEndTime); 140 141 itemControl.updateItemShippingTimeFromValue(itemShippingTimeValue, getPartyPK()); 142 } 143 144}