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