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.tax.server.command; 018 019import com.echothree.control.user.tax.common.edit.TaxClassificationTranslationEdit; 020import com.echothree.control.user.tax.common.edit.TaxEditFactory; 021import com.echothree.control.user.tax.common.form.EditTaxClassificationTranslationForm; 022import com.echothree.control.user.tax.common.result.EditTaxClassificationTranslationResult; 023import com.echothree.control.user.tax.common.result.TaxResultFactory; 024import com.echothree.control.user.tax.common.spec.TaxClassificationTranslationSpec; 025import com.echothree.model.control.core.common.MimeTypeUsageTypes; 026import com.echothree.model.control.core.server.logic.MimeTypeLogic; 027import com.echothree.model.control.geo.server.control.GeoControl; 028import com.echothree.model.control.party.common.PartyTypes; 029import com.echothree.model.control.party.server.control.PartyControl; 030import com.echothree.model.control.security.common.SecurityRoleGroups; 031import com.echothree.model.control.security.common.SecurityRoles; 032import com.echothree.model.control.tax.server.control.TaxControl; 033import com.echothree.model.data.core.server.entity.MimeType; 034import com.echothree.model.data.geo.server.entity.GeoCode; 035import com.echothree.model.data.party.server.entity.Language; 036import com.echothree.model.data.tax.server.entity.TaxClassification; 037import com.echothree.model.data.tax.server.entity.TaxClassificationTranslation; 038import com.echothree.model.data.tax.server.value.TaxClassificationTranslationValue; 039import com.echothree.model.data.user.common.pk.UserVisitPK; 040import com.echothree.util.common.message.ExecutionErrors; 041import com.echothree.util.common.validation.FieldDefinition; 042import com.echothree.util.common.validation.FieldType; 043import com.echothree.util.common.command.EditMode; 044import com.echothree.util.server.control.BaseAbstractEditCommand; 045import com.echothree.util.server.control.CommandSecurityDefinition; 046import com.echothree.util.server.control.PartyTypeDefinition; 047import com.echothree.util.server.control.SecurityRoleDefinition; 048import com.echothree.util.server.persistence.Session; 049import java.util.Arrays; 050import java.util.Collections; 051import java.util.List; 052 053public class EditTaxClassificationTranslationCommand 054 extends BaseAbstractEditCommand<TaxClassificationTranslationSpec, TaxClassificationTranslationEdit, EditTaxClassificationTranslationResult, TaxClassificationTranslation, TaxClassification> { 055 056 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 057 private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS; 058 private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS; 059 060 static { 061 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(Collections.unmodifiableList(Arrays.asList( 062 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 063 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), Collections.unmodifiableList(Arrays.asList( 064 new SecurityRoleDefinition(SecurityRoleGroups.TaxClassification.name(), SecurityRoles.Translation.name()) 065 ))) 066 ))); 067 068 SPEC_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 069 new FieldDefinition("TaxClassificationName", FieldType.ENTITY_NAME, true, null, null), 070 new FieldDefinition("LanguageIsoName", FieldType.ENTITY_NAME, true, null, null) 071 )); 072 073 EDIT_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 074 new FieldDefinition("Description", FieldType.STRING, true, 1L, 132L), 075 new FieldDefinition("OverviewMimeTypeName", FieldType.MIME_TYPE, false, null, null), 076 new FieldDefinition("Overview", FieldType.STRING, false, null, null) 077 )); 078 } 079 080 /** Creates a new instance of EditTaxClassificationTranslationCommand */ 081 public EditTaxClassificationTranslationCommand(UserVisitPK userVisitPK, EditTaxClassificationTranslationForm form) { 082 super(userVisitPK, form, COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 083 } 084 085 @Override 086 public EditTaxClassificationTranslationResult getResult() { 087 return TaxResultFactory.getEditTaxClassificationTranslationResult(); 088 } 089 090 @Override 091 public TaxClassificationTranslationEdit getEdit() { 092 return TaxEditFactory.getTaxClassificationTranslationEdit(); 093 } 094 095 @Override 096 public TaxClassificationTranslation getEntity(EditTaxClassificationTranslationResult result) { 097 var geoControl = Session.getModelController(GeoControl.class); 098 TaxClassificationTranslation taxClassificationTranslation = null; 099 String countryName = spec.getCountryName(); 100 GeoCode countryGeoCode = geoControl.getCountryByAlias(countryName); 101 102 if(countryGeoCode != null) { 103 var taxControl = Session.getModelController(TaxControl.class); 104 String taxClassificationName = spec.getTaxClassificationName(); 105 TaxClassification taxClassification = taxControl.getTaxClassificationByName(countryGeoCode, taxClassificationName); 106 107 if(taxClassification != null) { 108 var partyControl = Session.getModelController(PartyControl.class); 109 String languageIsoName = spec.getLanguageIsoName(); 110 Language language = partyControl.getLanguageByIsoName(languageIsoName); 111 112 if(language != null) { 113 if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) { 114 taxClassificationTranslation = taxControl.getTaxClassificationTranslation(taxClassification, language); 115 } else { // EditMode.UPDATE 116 taxClassificationTranslation = taxControl.getTaxClassificationTranslationForUpdate(taxClassification, language); 117 } 118 119 if(taxClassificationTranslation == null) { 120 addExecutionError(ExecutionErrors.UnknownTaxClassificationTranslation.name(), countryName, taxClassificationName, languageIsoName); 121 } 122 } else { 123 addExecutionError(ExecutionErrors.UnknownLanguageIsoName.name(), languageIsoName); 124 } 125 } else { 126 addExecutionError(ExecutionErrors.UnknownTaxClassificationName.name(), countryName, taxClassificationName); 127 } 128 } else { 129 addExecutionError(ExecutionErrors.UnknownGeoCodeName.name(), countryName); 130 } 131 132 return taxClassificationTranslation; 133 } 134 135 @Override 136 public TaxClassification getLockEntity(TaxClassificationTranslation taxClassificationTranslation) { 137 return taxClassificationTranslation.getTaxClassification(); 138 } 139 140 @Override 141 public void fillInResult(EditTaxClassificationTranslationResult result, TaxClassificationTranslation taxClassificationTranslation) { 142 var taxControl = Session.getModelController(TaxControl.class); 143 144 result.setTaxClassificationTranslation(taxControl.getTaxClassificationTranslationTransfer(getUserVisit(), taxClassificationTranslation)); 145 } 146 147 MimeType overviewMimeType; 148 149 @Override 150 public void doLock(TaxClassificationTranslationEdit edit, TaxClassificationTranslation taxClassificationTranslation) { 151 overviewMimeType = taxClassificationTranslation.getOverviewMimeType(); 152 153 edit.setDescription(taxClassificationTranslation.getDescription()); 154 edit.setOverviewMimeTypeName(overviewMimeType == null? null: overviewMimeType.getLastDetail().getMimeTypeName()); 155 edit.setOverview(taxClassificationTranslation.getOverview()); 156 } 157 158 @Override 159 protected void canUpdate(TaxClassificationTranslation taxClassificationTranslation) { 160 String overviewMimeTypeName = edit.getOverviewMimeTypeName(); 161 String overview = edit.getOverview(); 162 163 overviewMimeType = MimeTypeLogic.getInstance().checkMimeType(this, overviewMimeTypeName, overview, MimeTypeUsageTypes.TEXT.name(), 164 ExecutionErrors.MissingRequiredOverviewMimeTypeName.name(), ExecutionErrors.MissingRequiredOverview.name(), 165 ExecutionErrors.UnknownOverviewMimeTypeName.name(), ExecutionErrors.UnknownOverviewMimeTypeUsage.name()); 166 } 167 168 @Override 169 public void doUpdate(TaxClassificationTranslation taxClassificationTranslation) { 170 var taxControl = Session.getModelController(TaxControl.class); 171 TaxClassificationTranslationValue taxClassificationTranslationValue = taxControl.getTaxClassificationTranslationValue(taxClassificationTranslation); 172 173 taxClassificationTranslationValue.setDescription(edit.getDescription()); 174 taxClassificationTranslationValue.setOverviewMimeTypePK(overviewMimeType == null ? null : overviewMimeType.getPrimaryKey()); 175 taxClassificationTranslationValue.setOverview(edit.getOverview()); 176 177 taxControl.updateTaxClassificationTranslationFromValue(taxClassificationTranslationValue, getPartyPK()); 178 } 179 180}