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.party.server.command; 018 019import com.echothree.control.user.party.common.form.CreateDivisionForm; 020import com.echothree.control.user.party.common.result.PartyResultFactory; 021import com.echothree.model.control.accounting.server.control.AccountingControl; 022import com.echothree.model.control.party.common.PartyTypes; 023import com.echothree.model.control.party.server.control.PartyControl; 024import com.echothree.model.data.accounting.server.entity.Currency; 025import com.echothree.model.data.user.common.pk.UserVisitPK; 026import com.echothree.util.common.message.ExecutionErrors; 027import com.echothree.util.common.validation.FieldDefinition; 028import com.echothree.util.common.validation.FieldType; 029import com.echothree.util.common.command.BaseResult; 030import com.echothree.util.common.persistence.BasePK; 031import com.echothree.util.server.control.BaseSimpleCommand; 032import com.echothree.util.server.persistence.Session; 033import java.util.Arrays; 034import java.util.Collections; 035import java.util.List; 036import javax.enterprise.context.RequestScoped; 037 038@RequestScoped 039public class CreateDivisionCommand 040 extends BaseSimpleCommand<CreateDivisionForm> { 041 042 private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS; 043 044 static { 045 FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 046 new FieldDefinition("CompanyName", FieldType.ENTITY_NAME, true, null, null), 047 new FieldDefinition("DivisionName", FieldType.ENTITY_NAME, true, null, null), 048 new FieldDefinition("Name", FieldType.STRING, true, 1L, 60L), 049 new FieldDefinition("PreferredLanguageIsoName", FieldType.ENTITY_NAME, false, null, null), 050 new FieldDefinition("PreferredCurrencyIsoName", FieldType.ENTITY_NAME, false, null, null), 051 new FieldDefinition("PreferredJavaTimeZoneName", FieldType.TIME_ZONE_NAME, false, null, null), 052 new FieldDefinition("PreferredDateTimeFormatName", FieldType.ENTITY_NAME, false, null, null), 053 new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null), 054 new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null) 055 )); 056 } 057 058 /** Creates a new instance of CreateDivisionCommand */ 059 public CreateDivisionCommand() { 060 super(null, FORM_FIELD_DEFINITIONS, false); 061 } 062 063 @Override 064 protected BaseResult execute() { 065 var result = PartyResultFactory.getCreateDivisionResult(); 066 var partyControl = Session.getModelController(PartyControl.class); 067 var companyName = form.getCompanyName(); 068 var partyCompany = partyControl.getPartyCompanyByName(companyName); 069 070 if(partyCompany != null) { 071 var divisionName = form.getDivisionName(); 072 var partyCompanyParty = partyCompany.getParty(); 073 var partyDivision = partyControl.getPartyDivisionByName(partyCompanyParty, divisionName); 074 075 if(partyDivision == null) { 076 var preferredLanguageIsoName = form.getPreferredLanguageIsoName(); 077 var preferredLanguage = preferredLanguageIsoName == null? null: partyControl.getLanguageByIsoName(preferredLanguageIsoName); 078 079 if(preferredLanguageIsoName == null || (preferredLanguage != null)) { 080 var preferredJavaTimeZoneName = form.getPreferredJavaTimeZoneName(); 081 var preferredTimeZone = preferredJavaTimeZoneName == null? null: partyControl.getTimeZoneByJavaName(preferredJavaTimeZoneName); 082 083 if(preferredJavaTimeZoneName == null || (preferredTimeZone != null)) { 084 var preferredDateTimeFormatName = form.getPreferredDateTimeFormatName(); 085 var preferredDateTimeFormat = preferredDateTimeFormatName == null? null: partyControl.getDateTimeFormatByName(preferredDateTimeFormatName); 086 087 if(preferredDateTimeFormatName == null || (preferredDateTimeFormat != null)) { 088 var preferredCurrencyIsoName = form.getPreferredCurrencyIsoName(); 089 Currency preferredCurrency; 090 091 if(preferredCurrencyIsoName == null) 092 preferredCurrency = null; 093 else { 094 var accountingControl = Session.getModelController(AccountingControl.class); 095 preferredCurrency = accountingControl.getCurrencyByIsoName(preferredCurrencyIsoName); 096 } 097 098 if(preferredCurrencyIsoName == null || (preferredCurrency != null)) { 099 var partyType = partyControl.getPartyTypeByName(PartyTypes.DIVISION.name()); 100 BasePK createdBy = getPartyPK(); 101 var name = form.getName(); 102 var isDefault = Boolean.valueOf(form.getIsDefault()); 103 var sortOrder = Integer.valueOf(form.getSortOrder()); 104 105 var party = partyControl.createParty(null, partyType, preferredLanguage, preferredCurrency, preferredTimeZone, preferredDateTimeFormat, createdBy); 106 partyControl.createPartyGroup(party, name, createdBy); 107 partyDivision = partyControl.createPartyDivision(party, partyCompanyParty, divisionName, isDefault, sortOrder, createdBy); 108 } else { 109 addExecutionError(ExecutionErrors.UnknownCurrencyIsoName.name(), preferredCurrencyIsoName); 110 } 111 } else { 112 addExecutionError(ExecutionErrors.UnknownDateTimeFormatName.name(), preferredDateTimeFormatName); 113 } 114 } else { 115 addExecutionError(ExecutionErrors.UnknownJavaTimeZoneName.name(), preferredJavaTimeZoneName); 116 } 117 } else { 118 addExecutionError(ExecutionErrors.UnknownLanguageIsoName.name(), preferredLanguageIsoName); 119 } 120 } else { 121 addExecutionError(ExecutionErrors.DuplicateDivisionName.name(), divisionName); 122 } 123 124 if(partyDivision != null) { 125 var party = partyDivision.getParty(); 126 127 result.setEntityRef(party.getPrimaryKey().getEntityRef()); 128 result.setDivisionName(partyDivision.getPartyDivisionName()); 129 result.setPartyName(party.getLastDetail().getPartyName()); 130 } 131 } else { 132 addExecutionError(ExecutionErrors.UnknownCompanyName.name(), companyName); 133 } 134 135 return result; 136 } 137 138}