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