001// --------------------------------------------------------------------------------
002// Copyright 2002-2026 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.TaxClassificationEdit;
020import com.echothree.control.user.tax.common.edit.TaxEditFactory;
021import com.echothree.control.user.tax.common.form.EditTaxClassificationForm;
022import com.echothree.control.user.tax.common.result.EditTaxClassificationResult;
023import com.echothree.control.user.tax.common.result.TaxResultFactory;
024import com.echothree.control.user.tax.common.spec.TaxClassificationSpec;
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.security.common.SecurityRoleGroups;
030import com.echothree.model.control.security.common.SecurityRoles;
031import com.echothree.model.control.tax.server.control.TaxControl;
032import com.echothree.model.data.core.server.entity.MimeType;
033import com.echothree.model.data.geo.server.entity.GeoCode;
034import com.echothree.model.data.tax.server.entity.TaxClassification;
035import com.echothree.model.data.user.common.pk.UserVisitPK;
036import com.echothree.util.common.message.ExecutionErrors;
037import com.echothree.util.common.validation.FieldDefinition;
038import com.echothree.util.common.validation.FieldType;
039import com.echothree.util.common.command.EditMode;
040import com.echothree.util.server.control.BaseAbstractEditCommand;
041import com.echothree.util.server.control.CommandSecurityDefinition;
042import com.echothree.util.server.control.PartyTypeDefinition;
043import com.echothree.util.server.control.SecurityRoleDefinition;
044import com.echothree.util.server.persistence.Session;
045import java.util.List;
046import javax.enterprise.context.Dependent;
047
048@Dependent
049public class EditTaxClassificationCommand
050        extends BaseAbstractEditCommand<TaxClassificationSpec, TaxClassificationEdit, EditTaxClassificationResult, TaxClassification, TaxClassification> {
051
052    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
053    private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS;
054    private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS;
055
056    static {
057        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of(
058                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
059                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of(
060                        new SecurityRoleDefinition(SecurityRoleGroups.TaxClassification.name(), SecurityRoles.Edit.name())
061                        ))
062                ));
063
064        SPEC_FIELD_DEFINITIONS = List.of(
065                new FieldDefinition("CountryName", FieldType.ENTITY_NAME, true, null, null),
066                new FieldDefinition("TaxClassificationName", FieldType.ENTITY_NAME, true, null, null)
067                );
068
069        EDIT_FIELD_DEFINITIONS = List.of(
070                new FieldDefinition("TaxClassificationName", FieldType.ENTITY_NAME, true, null, null),
071                new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null),
072                new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null),
073                new FieldDefinition("Description", FieldType.STRING, false, 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 EditTaxClassificationCommand */
080    public EditTaxClassificationCommand() {
081        super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
082    }
083
084    @Override
085    public EditTaxClassificationResult getResult() {
086        return TaxResultFactory.getEditTaxClassificationResult();
087    }
088
089    @Override
090    public TaxClassificationEdit getEdit() {
091        return TaxEditFactory.getTaxClassificationEdit();
092    }
093
094    GeoCode countryGeoCode;
095
096    @Override
097    public TaxClassification getEntity(EditTaxClassificationResult result) {
098        var geoControl = Session.getModelController(GeoControl.class);
099        TaxClassification taxClassification = null;
100        var countryName = spec.getCountryName();
101
102        countryGeoCode = geoControl.getCountryByAlias(countryName);
103
104        if(countryGeoCode != null) {
105            var taxControl = Session.getModelController(TaxControl.class);
106            var taxClassificationName = spec.getTaxClassificationName();
107
108            if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) {
109                taxClassification = taxControl.getTaxClassificationByName(countryGeoCode, taxClassificationName);
110            } else { // EditMode.UPDATE
111                taxClassification = taxControl.getTaxClassificationByNameForUpdate(countryGeoCode, taxClassificationName);
112            }
113
114            if(taxClassification == null) {
115                addExecutionError(ExecutionErrors.UnknownTaxClassificationName.name(), countryName, taxClassificationName);
116            }
117        } else {
118            addExecutionError(ExecutionErrors.UnknownGeoCodeName.name(), countryName);
119        }
120
121        return taxClassification;
122    }
123
124    @Override
125    public TaxClassification getLockEntity(TaxClassification taxClassification) {
126        return taxClassification;
127    }
128
129    @Override
130    public void fillInResult(EditTaxClassificationResult result, TaxClassification taxClassification) {
131        var taxControl = Session.getModelController(TaxControl.class);
132
133        result.setTaxClassification(taxControl.getTaxClassificationTransfer(getUserVisit(), taxClassification));
134    }
135
136    MimeType overviewMimeType;
137    
138    @Override
139    public void doLock(TaxClassificationEdit edit, TaxClassification taxClassification) {
140        var taxControl = Session.getModelController(TaxControl.class);
141        var taxClassificationTranslation = taxControl.getTaxClassificationTranslation(taxClassification, getPreferredLanguage());
142        var taxClassificationDetail = taxClassification.getLastDetail();
143
144        edit.setTaxClassificationName(taxClassificationDetail.getTaxClassificationName());
145        edit.setIsDefault(taxClassificationDetail.getIsDefault().toString());
146        edit.setSortOrder(taxClassificationDetail.getSortOrder().toString());
147
148        if(taxClassificationTranslation != null) {
149            overviewMimeType = taxClassificationTranslation.getOverviewMimeType();
150            
151            edit.setDescription(taxClassificationTranslation.getDescription());
152            edit.setOverviewMimeTypeName(overviewMimeType == null? null: overviewMimeType.getLastDetail().getMimeTypeName());
153            edit.setOverview(taxClassificationTranslation.getOverview());
154        }
155    }
156    
157    @Override
158    public void canUpdate(TaxClassification taxClassification) {
159        var taxControl = Session.getModelController(TaxControl.class);
160        var geoCodeDetail = countryGeoCode.getLastDetail();
161        var taxClassificationName = edit.getTaxClassificationName();
162        var duplicateTaxClassification = taxControl.getTaxClassificationByName(countryGeoCode, taxClassificationName);
163
164        if(duplicateTaxClassification != null && !taxClassification.equals(duplicateTaxClassification)) {
165            addExecutionError(ExecutionErrors.DuplicateTaxClassificationName.name(), geoCodeDetail.getGeoCodeName(), taxClassificationName);
166        } else {
167            var overviewMimeTypeName = edit.getOverviewMimeTypeName();
168            var overview = edit.getOverview();
169
170            overviewMimeType = MimeTypeLogic.getInstance().checkMimeType(this, overviewMimeTypeName, overview, MimeTypeUsageTypes.TEXT.name(),
171                    ExecutionErrors.MissingRequiredOverviewMimeTypeName.name(), ExecutionErrors.MissingRequiredOverview.name(),
172                    ExecutionErrors.UnknownOverviewMimeTypeName.name(), ExecutionErrors.UnknownOverviewMimeTypeUsage.name());
173        }
174    }
175
176    @Override
177    public void doUpdate(TaxClassification taxClassification) {
178        var taxControl = Session.getModelController(TaxControl.class);
179        var partyPK = getPartyPK();
180        var taxClassificationDetailValue = taxControl.getTaxClassificationDetailValueForUpdate(taxClassification);
181        var taxClassificationTranslation = taxControl.getTaxClassificationTranslationForUpdate(taxClassification, getPreferredLanguage());
182        var description = edit.getDescription();
183        var overview = edit.getOverview();
184
185        taxClassificationDetailValue.setTaxClassificationName(edit.getTaxClassificationName());
186        taxClassificationDetailValue.setIsDefault(Boolean.valueOf(edit.getIsDefault()));
187        taxClassificationDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder()));
188
189        taxControl.updateTaxClassificationFromValue(taxClassificationDetailValue, partyPK);
190
191        if(taxClassificationTranslation == null && (description != null || overview != null)) {
192            taxControl.createTaxClassificationTranslation(taxClassification, getPreferredLanguage(), description, overviewMimeType, overview, partyPK);
193        } else if(taxClassificationTranslation != null && (description == null && overview == null)) {
194            taxControl.deleteTaxClassificationTranslation(taxClassificationTranslation, partyPK);
195        } else if(taxClassificationTranslation != null && (description != null || overview != null)) {
196            var taxClassificationTranslationValue = taxControl.getTaxClassificationTranslationValue(taxClassificationTranslation);
197
198            taxClassificationTranslationValue.setDescription(description);
199            taxClassificationTranslationValue.setOverviewMimeTypePK(overviewMimeType == null? null: overviewMimeType.getPrimaryKey());
200            taxClassificationTranslationValue.setOverview(overview);
201            taxControl.updateTaxClassificationTranslationFromValue(taxClassificationTranslationValue, partyPK);
202        }
203    }
204
205}