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.HarmonizedTariffScheduleCodeTranslationEdit; 020import com.echothree.control.user.item.common.edit.ItemEditFactory; 021import com.echothree.control.user.item.common.form.EditHarmonizedTariffScheduleCodeTranslationForm; 022import com.echothree.control.user.item.common.result.EditHarmonizedTariffScheduleCodeTranslationResult; 023import com.echothree.control.user.item.common.result.ItemResultFactory; 024import com.echothree.control.user.item.common.spec.HarmonizedTariffScheduleCodeTranslationSpec; 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.item.server.control.ItemControl; 029import com.echothree.model.control.party.common.PartyTypes; 030import com.echothree.model.control.party.server.control.PartyControl; 031import com.echothree.model.control.security.common.SecurityRoleGroups; 032import com.echothree.model.control.security.common.SecurityRoles; 033import com.echothree.model.data.core.server.entity.MimeType; 034import com.echothree.model.data.item.server.entity.HarmonizedTariffScheduleCode; 035import com.echothree.model.data.item.server.entity.HarmonizedTariffScheduleCodeTranslation; 036import com.echothree.model.data.user.common.pk.UserVisitPK; 037import com.echothree.util.common.message.ExecutionErrors; 038import com.echothree.util.common.validation.FieldDefinition; 039import com.echothree.util.common.validation.FieldType; 040import com.echothree.util.common.command.EditMode; 041import com.echothree.util.server.control.BaseAbstractEditCommand; 042import com.echothree.util.server.control.CommandSecurityDefinition; 043import com.echothree.util.server.control.PartyTypeDefinition; 044import com.echothree.util.server.control.SecurityRoleDefinition; 045import com.echothree.util.server.persistence.Session; 046import java.util.Arrays; 047import java.util.Collections; 048import java.util.List; 049import javax.enterprise.context.RequestScoped; 050 051@RequestScoped 052public class EditHarmonizedTariffScheduleCodeTranslationCommand 053 extends BaseAbstractEditCommand<HarmonizedTariffScheduleCodeTranslationSpec, HarmonizedTariffScheduleCodeTranslationEdit, EditHarmonizedTariffScheduleCodeTranslationResult, HarmonizedTariffScheduleCodeTranslation, HarmonizedTariffScheduleCode> { 054 055 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 056 private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS; 057 private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS; 058 059 static { 060 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(Collections.unmodifiableList(Arrays.asList( 061 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 062 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), Collections.unmodifiableList(Arrays.asList( 063 new SecurityRoleDefinition(SecurityRoleGroups.HarmonizedTariffScheduleCode.name(), SecurityRoles.Translation.name()) 064 ))) 065 ))); 066 067 SPEC_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 068 new FieldDefinition("HarmonizedTariffScheduleCodeName", FieldType.ENTITY_NAME, true, null, null), 069 new FieldDefinition("LanguageIsoName", FieldType.ENTITY_NAME, true, null, null) 070 )); 071 072 EDIT_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 073 new FieldDefinition("Description", FieldType.STRING, true, 1L, 132L), 074 new FieldDefinition("OverviewMimeTypeName", FieldType.MIME_TYPE, false, null, null), 075 new FieldDefinition("Overview", FieldType.STRING, false, null, null) 076 )); 077 } 078 079 /** Creates a new instance of EditHarmonizedTariffScheduleCodeTranslationCommand */ 080 public EditHarmonizedTariffScheduleCodeTranslationCommand() { 081 super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 082 } 083 084 @Override 085 public EditHarmonizedTariffScheduleCodeTranslationResult getResult() { 086 return ItemResultFactory.getEditHarmonizedTariffScheduleCodeTranslationResult(); 087 } 088 089 @Override 090 public HarmonizedTariffScheduleCodeTranslationEdit getEdit() { 091 return ItemEditFactory.getHarmonizedTariffScheduleCodeTranslationEdit(); 092 } 093 094 @Override 095 public HarmonizedTariffScheduleCodeTranslation getEntity(EditHarmonizedTariffScheduleCodeTranslationResult result) { 096 var geoControl = Session.getModelController(GeoControl.class); 097 HarmonizedTariffScheduleCodeTranslation harmonizedTariffScheduleCodeTranslation = null; 098 var countryName = spec.getCountryName(); 099 var countryGeoCode = geoControl.getCountryByAlias(countryName); 100 101 if(countryGeoCode != null) { 102 var itemControl = Session.getModelController(ItemControl.class); 103 var harmonizedTariffScheduleCodeName = spec.getHarmonizedTariffScheduleCodeName(); 104 var harmonizedTariffScheduleCode = itemControl.getHarmonizedTariffScheduleCodeByName(countryGeoCode, harmonizedTariffScheduleCodeName); 105 106 if(harmonizedTariffScheduleCode != null) { 107 var partyControl = Session.getModelController(PartyControl.class); 108 var languageIsoName = spec.getLanguageIsoName(); 109 var language = partyControl.getLanguageByIsoName(languageIsoName); 110 111 if(language != null) { 112 if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) { 113 harmonizedTariffScheduleCodeTranslation = itemControl.getHarmonizedTariffScheduleCodeTranslation(harmonizedTariffScheduleCode, language); 114 } else { // EditMode.UPDATE 115 harmonizedTariffScheduleCodeTranslation = itemControl.getHarmonizedTariffScheduleCodeTranslationForUpdate(harmonizedTariffScheduleCode, language); 116 } 117 118 if(harmonizedTariffScheduleCodeTranslation == null) { 119 addExecutionError(ExecutionErrors.UnknownHarmonizedTariffScheduleCodeTranslation.name(), countryName, harmonizedTariffScheduleCodeName, languageIsoName); 120 } 121 } else { 122 addExecutionError(ExecutionErrors.UnknownLanguageIsoName.name(), languageIsoName); 123 } 124 } else { 125 addExecutionError(ExecutionErrors.UnknownHarmonizedTariffScheduleCodeName.name(), countryName, harmonizedTariffScheduleCodeName); 126 } 127 } else { 128 addExecutionError(ExecutionErrors.UnknownGeoCodeName.name(), countryName); 129 } 130 131 return harmonizedTariffScheduleCodeTranslation; 132 } 133 134 @Override 135 public HarmonizedTariffScheduleCode getLockEntity(HarmonizedTariffScheduleCodeTranslation harmonizedTariffScheduleCodeTranslation) { 136 return harmonizedTariffScheduleCodeTranslation.getHarmonizedTariffScheduleCode(); 137 } 138 139 @Override 140 public void fillInResult(EditHarmonizedTariffScheduleCodeTranslationResult result, HarmonizedTariffScheduleCodeTranslation harmonizedTariffScheduleCodeTranslation) { 141 var itemControl = Session.getModelController(ItemControl.class); 142 143 result.setHarmonizedTariffScheduleCodeTranslation(itemControl.getHarmonizedTariffScheduleCodeTranslationTransfer(getUserVisit(), harmonizedTariffScheduleCodeTranslation)); 144 } 145 146 MimeType overviewMimeType; 147 148 @Override 149 public void doLock(HarmonizedTariffScheduleCodeTranslationEdit edit, HarmonizedTariffScheduleCodeTranslation harmonizedTariffScheduleCodeTranslation) { 150 overviewMimeType = harmonizedTariffScheduleCodeTranslation.getOverviewMimeType(); 151 152 edit.setDescription(harmonizedTariffScheduleCodeTranslation.getDescription()); 153 edit.setOverviewMimeTypeName(overviewMimeType == null? null: overviewMimeType.getLastDetail().getMimeTypeName()); 154 edit.setOverview(harmonizedTariffScheduleCodeTranslation.getOverview()); 155 } 156 157 @Override 158 protected void canUpdate(HarmonizedTariffScheduleCodeTranslation harmonizedTariffScheduleCodeTranslation) { 159 var overviewMimeTypeName = edit.getOverviewMimeTypeName(); 160 var overview = edit.getOverview(); 161 162 overviewMimeType = MimeTypeLogic.getInstance().checkMimeType(this, overviewMimeTypeName, overview, MimeTypeUsageTypes.TEXT.name(), 163 ExecutionErrors.MissingRequiredOverviewMimeTypeName.name(), ExecutionErrors.MissingRequiredOverview.name(), 164 ExecutionErrors.UnknownOverviewMimeTypeName.name(), ExecutionErrors.UnknownOverviewMimeTypeUsage.name()); 165 } 166 167 @Override 168 public void doUpdate(HarmonizedTariffScheduleCodeTranslation harmonizedTariffScheduleCodeTranslation) { 169 var itemControl = Session.getModelController(ItemControl.class); 170 var harmonizedTariffScheduleCodeTranslationValue = itemControl.getHarmonizedTariffScheduleCodeTranslationValue(harmonizedTariffScheduleCodeTranslation); 171 172 harmonizedTariffScheduleCodeTranslationValue.setDescription(edit.getDescription()); 173 harmonizedTariffScheduleCodeTranslationValue.setOverviewMimeTypePK(overviewMimeType == null ? null : overviewMimeType.getPrimaryKey()); 174 harmonizedTariffScheduleCodeTranslationValue.setOverview(edit.getOverview()); 175 176 itemControl.updateHarmonizedTariffScheduleCodeTranslationFromValue(harmonizedTariffScheduleCodeTranslationValue, getPartyPK()); 177 } 178 179}