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