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.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.Arrays;
047import java.util.Collections;
048import java.util.List;
049import javax.enterprise.context.RequestScoped;
050
051@RequestScoped
052public class EditTaxClassificationTranslationCommand
053        extends BaseAbstractEditCommand<TaxClassificationTranslationSpec, TaxClassificationTranslationEdit, EditTaxClassificationTranslationResult, TaxClassificationTranslation, TaxClassification> {
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.TaxClassification.name(), SecurityRoles.Translation.name())
064                        )))
065                )));
066
067        SPEC_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
068                new FieldDefinition("TaxClassificationName", 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 EditTaxClassificationTranslationCommand */
080    public EditTaxClassificationTranslationCommand() {
081        super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
082    }
083
084    @Override
085    public EditTaxClassificationTranslationResult getResult() {
086        return TaxResultFactory.getEditTaxClassificationTranslationResult();
087    }
088
089    @Override
090    public TaxClassificationTranslationEdit getEdit() {
091        return TaxEditFactory.getTaxClassificationTranslationEdit();
092    }
093
094    @Override
095    public TaxClassificationTranslation getEntity(EditTaxClassificationTranslationResult result) {
096        var geoControl = Session.getModelController(GeoControl.class);
097        TaxClassificationTranslation taxClassificationTranslation = null;
098        var countryName = spec.getCountryName();
099        var countryGeoCode = geoControl.getCountryByAlias(countryName);
100
101        if(countryGeoCode != null) {
102            var taxControl = Session.getModelController(TaxControl.class);
103            var taxClassificationName = spec.getTaxClassificationName();
104            var taxClassification = taxControl.getTaxClassificationByName(countryGeoCode, taxClassificationName);
105
106            if(taxClassification != 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                        taxClassificationTranslation = taxControl.getTaxClassificationTranslation(taxClassification, language);
114                    } else { // EditMode.UPDATE
115                        taxClassificationTranslation = taxControl.getTaxClassificationTranslationForUpdate(taxClassification, language);
116                    }
117
118                    if(taxClassificationTranslation == null) {
119                        addExecutionError(ExecutionErrors.UnknownTaxClassificationTranslation.name(), countryName, taxClassificationName, languageIsoName);
120                    }
121                } else {
122                    addExecutionError(ExecutionErrors.UnknownLanguageIsoName.name(), languageIsoName);
123                }
124            } else {
125                addExecutionError(ExecutionErrors.UnknownTaxClassificationName.name(), countryName, taxClassificationName);
126            }
127        } else {
128            addExecutionError(ExecutionErrors.UnknownGeoCodeName.name(), countryName);
129        }
130
131        return taxClassificationTranslation;
132    }
133
134    @Override
135    public TaxClassification getLockEntity(TaxClassificationTranslation taxClassificationTranslation) {
136        return taxClassificationTranslation.getTaxClassification();
137    }
138
139    @Override
140    public void fillInResult(EditTaxClassificationTranslationResult result, TaxClassificationTranslation taxClassificationTranslation) {
141        var taxControl = Session.getModelController(TaxControl.class);
142
143        result.setTaxClassificationTranslation(taxControl.getTaxClassificationTranslationTransfer(getUserVisit(), taxClassificationTranslation));
144    }
145
146    MimeType overviewMimeType;
147    
148    @Override
149    public void doLock(TaxClassificationTranslationEdit edit, TaxClassificationTranslation taxClassificationTranslation) {
150        overviewMimeType = taxClassificationTranslation.getOverviewMimeType();
151        
152        edit.setDescription(taxClassificationTranslation.getDescription());
153        edit.setOverviewMimeTypeName(overviewMimeType == null? null: overviewMimeType.getLastDetail().getMimeTypeName());
154        edit.setOverview(taxClassificationTranslation.getOverview());
155    }
156
157    @Override
158    protected void canUpdate(TaxClassificationTranslation taxClassificationTranslation) {
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(TaxClassificationTranslation taxClassificationTranslation) {
169        var taxControl = Session.getModelController(TaxControl.class);
170        var taxClassificationTranslationValue = taxControl.getTaxClassificationTranslationValue(taxClassificationTranslation);
171
172        taxClassificationTranslationValue.setDescription(edit.getDescription());
173        taxClassificationTranslationValue.setOverviewMimeTypePK(overviewMimeType == null ? null : overviewMimeType.getPrimaryKey());
174        taxClassificationTranslationValue.setOverview(edit.getOverview());
175
176        taxControl.updateTaxClassificationTranslationFromValue(taxClassificationTranslationValue, getPartyPK());
177    }
178
179}