001// --------------------------------------------------------------------------------
002// Copyright 2002-2024 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.form.CreateTaxClassificationTranslationForm;
020import com.echothree.model.control.core.common.MimeTypeUsageTypes;
021import com.echothree.model.control.core.server.logic.MimeTypeLogic;
022import com.echothree.model.control.geo.server.control.GeoControl;
023import com.echothree.model.control.party.common.PartyTypes;
024import com.echothree.model.control.party.server.control.PartyControl;
025import com.echothree.model.control.security.common.SecurityRoleGroups;
026import com.echothree.model.control.security.common.SecurityRoles;
027import com.echothree.model.control.tax.server.control.TaxControl;
028import com.echothree.model.data.core.server.entity.MimeType;
029import com.echothree.model.data.geo.server.entity.GeoCode;
030import com.echothree.model.data.party.server.entity.Language;
031import com.echothree.model.data.tax.server.entity.TaxClassification;
032import com.echothree.model.data.tax.server.entity.TaxClassificationTranslation;
033import com.echothree.model.data.user.common.pk.UserVisitPK;
034import com.echothree.util.common.message.ExecutionErrors;
035import com.echothree.util.common.validation.FieldDefinition;
036import com.echothree.util.common.validation.FieldType;
037import com.echothree.util.common.command.BaseResult;
038import com.echothree.util.server.control.BaseSimpleCommand;
039import com.echothree.util.server.control.CommandSecurityDefinition;
040import com.echothree.util.server.control.PartyTypeDefinition;
041import com.echothree.util.server.control.SecurityRoleDefinition;
042import com.echothree.util.server.persistence.Session;
043import java.util.Arrays;
044import java.util.Collections;
045import java.util.List;
046
047public class CreateTaxClassificationTranslationCommand
048        extends BaseSimpleCommand<CreateTaxClassificationTranslationForm> {
049    
050    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
051    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
052    
053    static {
054        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(Collections.unmodifiableList(Arrays.asList(
055                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
056                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), Collections.unmodifiableList(Arrays.asList(
057                        new SecurityRoleDefinition(SecurityRoleGroups.TaxClassification.name(), SecurityRoles.Translation.name())
058                        )))
059                )));
060
061        FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
062                new FieldDefinition("CountryName", FieldType.ENTITY_NAME, true, null, null),
063                new FieldDefinition("TaxClassificationName", FieldType.ENTITY_NAME, true, null, null),
064                new FieldDefinition("LanguageIsoName", FieldType.ENTITY_NAME, true, null, null),
065                new FieldDefinition("Description", FieldType.STRING, true, 1L, 132L),
066                new FieldDefinition("OverviewMimeTypeName", FieldType.MIME_TYPE, false, null, null),
067                new FieldDefinition("Overview", FieldType.STRING, false, null, null)
068                ));
069    }
070    
071    /** Creates a new instance of CreateTaxClassificationTranslationCommand */
072    public CreateTaxClassificationTranslationCommand(UserVisitPK userVisitPK, CreateTaxClassificationTranslationForm form) {
073        super(userVisitPK, form, COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false);
074    }
075    
076    @Override
077    protected BaseResult execute() {
078        var geoControl = Session.getModelController(GeoControl.class);
079        String countryName = form.getCountryName();
080        GeoCode geoCode = geoControl.getCountryByAlias(countryName);
081        
082        if(geoCode != null) {
083            var taxControl = Session.getModelController(TaxControl.class);
084            String taxClassificationName = form.getTaxClassificationName();
085            TaxClassification taxClassification = taxControl.getTaxClassificationByName(geoCode, taxClassificationName);
086            
087            if(taxClassification != null) {
088                var partyControl = Session.getModelController(PartyControl.class);
089                String languageIsoName = form.getLanguageIsoName();
090                Language language = partyControl.getLanguageByIsoName(languageIsoName);
091                
092                if(language != null) {
093                    String overview = form.getOverview();
094                    MimeType overviewMimeType = MimeTypeLogic.getInstance().checkMimeType(this, form.getOverviewMimeTypeName(), overview, MimeTypeUsageTypes.TEXT.name(),
095                            ExecutionErrors.MissingRequiredOverviewMimeTypeName.name(), ExecutionErrors.MissingRequiredOverview.name(),
096                            ExecutionErrors.UnknownOverviewMimeTypeName.name(), ExecutionErrors.UnknownOverviewMimeTypeUsage.name());
097
098                    if(!hasExecutionErrors()) {
099                        TaxClassificationTranslation taxClassificationTranslation = taxControl.getTaxClassificationTranslation(taxClassification, language);
100
101                        if(taxClassificationTranslation == null) {
102                            var description = form.getDescription();
103
104                            taxControl.createTaxClassificationTranslation(taxClassification, language, description, overviewMimeType,
105                                    overview, getPartyPK());
106                        } else {
107                            addExecutionError(ExecutionErrors.DuplicateTaxClassificationTranslation.name());
108                        }
109                    }
110                } else {
111                    addExecutionError(ExecutionErrors.UnknownLanguageIsoName.name(), countryName, taxClassificationName, languageIsoName);
112                }
113            } else {
114                addExecutionError(ExecutionErrors.UnknownTaxClassificationName.name(), countryName, taxClassificationName);
115            }
116        } else {
117            addExecutionError(ExecutionErrors.UnknownGeoCodeName.name(), countryName);
118        }
119        
120        return null;
121    }
122    
123}