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