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 java.util.List;
046import javax.enterprise.context.Dependent;
047import javax.inject.Inject;
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    @Inject
078    GeoControl geoControl;
079
080    @Inject
081    PartyControl partyControl;
082
083    @Inject
084    TaxControl taxControl;
085
086    @Inject
087    MimeTypeLogic mimeTypeLogic;
088
089    /** Creates a new instance of EditTaxClassificationTranslationCommand */
090    public EditTaxClassificationTranslationCommand() {
091        super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
092    }
093
094    @Override
095    public EditTaxClassificationTranslationResult getResult() {
096        return TaxResultFactory.getEditTaxClassificationTranslationResult();
097    }
098
099    @Override
100    public TaxClassificationTranslationEdit getEdit() {
101        return TaxEditFactory.getTaxClassificationTranslationEdit();
102    }
103
104    @Override
105    public TaxClassificationTranslation getEntity(EditTaxClassificationTranslationResult result) {
106        TaxClassificationTranslation taxClassificationTranslation = null;
107        var countryName = spec.getCountryName();
108        var countryGeoCode = geoControl.getCountryByAlias(countryName);
109
110        if(countryGeoCode != null) {
111            var taxClassificationName = spec.getTaxClassificationName();
112            var taxClassification = taxControl.getTaxClassificationByName(countryGeoCode, taxClassificationName);
113
114            if(taxClassification != null) {
115                var languageIsoName = spec.getLanguageIsoName();
116                var language = partyControl.getLanguageByIsoName(languageIsoName);
117
118                if(language != null) {
119                    if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) {
120                        taxClassificationTranslation = taxControl.getTaxClassificationTranslation(taxClassification, language);
121                    } else { // EditMode.UPDATE
122                        taxClassificationTranslation = taxControl.getTaxClassificationTranslationForUpdate(taxClassification, language);
123                    }
124
125                    if(taxClassificationTranslation == null) {
126                        addExecutionError(ExecutionErrors.UnknownTaxClassificationTranslation.name(), countryName, taxClassificationName, languageIsoName);
127                    }
128                } else {
129                    addExecutionError(ExecutionErrors.UnknownLanguageIsoName.name(), languageIsoName);
130                }
131            } else {
132                addExecutionError(ExecutionErrors.UnknownTaxClassificationName.name(), countryName, taxClassificationName);
133            }
134        } else {
135            addExecutionError(ExecutionErrors.UnknownGeoCodeName.name(), countryName);
136        }
137
138        return taxClassificationTranslation;
139    }
140
141    @Override
142    public TaxClassification getLockEntity(TaxClassificationTranslation taxClassificationTranslation) {
143        return taxClassificationTranslation.getTaxClassification();
144    }
145
146    @Override
147    public void fillInResult(EditTaxClassificationTranslationResult result, TaxClassificationTranslation taxClassificationTranslation) {
148        result.setTaxClassificationTranslation(taxControl.getTaxClassificationTranslationTransfer(getUserVisit(), taxClassificationTranslation));
149    }
150
151    MimeType overviewMimeType;
152    
153    @Override
154    public void doLock(TaxClassificationTranslationEdit edit, TaxClassificationTranslation taxClassificationTranslation) {
155        overviewMimeType = taxClassificationTranslation.getOverviewMimeType();
156        
157        edit.setDescription(taxClassificationTranslation.getDescription());
158        edit.setOverviewMimeTypeName(overviewMimeType == null? null: overviewMimeType.getLastDetail().getMimeTypeName());
159        edit.setOverview(taxClassificationTranslation.getOverview());
160    }
161
162    @Override
163    protected void canUpdate(TaxClassificationTranslation taxClassificationTranslation) {
164        var overviewMimeTypeName = edit.getOverviewMimeTypeName();
165        var overview = edit.getOverview();
166        
167        overviewMimeType = mimeTypeLogic.checkMimeType(this, overviewMimeTypeName, overview, MimeTypeUsageTypes.TEXT.name(),
168                ExecutionErrors.MissingRequiredOverviewMimeTypeName.name(), ExecutionErrors.MissingRequiredOverview.name(),
169                ExecutionErrors.UnknownOverviewMimeTypeName.name(), ExecutionErrors.UnknownOverviewMimeTypeUsage.name());
170    }
171    
172    @Override
173    public void doUpdate(TaxClassificationTranslation taxClassificationTranslation) {
174        var taxClassificationTranslationValue = taxControl.getTaxClassificationTranslationValue(taxClassificationTranslation);
175
176        taxClassificationTranslationValue.setDescription(edit.getDescription());
177        taxClassificationTranslationValue.setOverviewMimeTypePK(overviewMimeType == null ? null : overviewMimeType.getPrimaryKey());
178        taxClassificationTranslationValue.setOverview(edit.getOverview());
179
180        taxControl.updateTaxClassificationTranslationFromValue(taxClassificationTranslationValue, getPartyPK());
181    }
182
183}