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.item.server.command;
018
019import com.echothree.control.user.item.common.edit.HarmonizedTariffScheduleCodeTranslationEdit;
020import com.echothree.control.user.item.common.edit.ItemEditFactory;
021import com.echothree.control.user.item.common.form.EditHarmonizedTariffScheduleCodeTranslationForm;
022import com.echothree.control.user.item.common.result.EditHarmonizedTariffScheduleCodeTranslationResult;
023import com.echothree.control.user.item.common.result.ItemResultFactory;
024import com.echothree.control.user.item.common.spec.HarmonizedTariffScheduleCodeTranslationSpec;
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.item.server.control.ItemControl;
029import com.echothree.model.control.party.common.PartyTypes;
030import com.echothree.model.control.party.server.control.PartyControl;
031import com.echothree.model.control.security.common.SecurityRoleGroups;
032import com.echothree.model.control.security.common.SecurityRoles;
033import com.echothree.model.data.core.server.entity.MimeType;
034import com.echothree.model.data.geo.server.entity.GeoCode;
035import com.echothree.model.data.item.server.entity.HarmonizedTariffScheduleCode;
036import com.echothree.model.data.item.server.entity.HarmonizedTariffScheduleCodeTranslation;
037import com.echothree.model.data.item.server.value.HarmonizedTariffScheduleCodeTranslationValue;
038import com.echothree.model.data.party.server.entity.Language;
039import com.echothree.model.data.user.common.pk.UserVisitPK;
040import com.echothree.util.common.message.ExecutionErrors;
041import com.echothree.util.common.validation.FieldDefinition;
042import com.echothree.util.common.validation.FieldType;
043import com.echothree.util.common.command.EditMode;
044import com.echothree.util.server.control.BaseAbstractEditCommand;
045import com.echothree.util.server.control.CommandSecurityDefinition;
046import com.echothree.util.server.control.PartyTypeDefinition;
047import com.echothree.util.server.control.SecurityRoleDefinition;
048import com.echothree.util.server.persistence.Session;
049import java.util.Arrays;
050import java.util.Collections;
051import java.util.List;
052
053public class EditHarmonizedTariffScheduleCodeTranslationCommand
054        extends BaseAbstractEditCommand<HarmonizedTariffScheduleCodeTranslationSpec, HarmonizedTariffScheduleCodeTranslationEdit, EditHarmonizedTariffScheduleCodeTranslationResult, HarmonizedTariffScheduleCodeTranslation, HarmonizedTariffScheduleCode> {
055
056    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
057    private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS;
058    private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS;
059
060    static {
061        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(Collections.unmodifiableList(Arrays.asList(
062                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
063                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), Collections.unmodifiableList(Arrays.asList(
064                        new SecurityRoleDefinition(SecurityRoleGroups.HarmonizedTariffScheduleCode.name(), SecurityRoles.Translation.name())
065                        )))
066                )));
067
068        SPEC_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
069                new FieldDefinition("HarmonizedTariffScheduleCodeName", FieldType.ENTITY_NAME, true, null, null),
070                new FieldDefinition("LanguageIsoName", FieldType.ENTITY_NAME, true, null, null)
071                ));
072
073        EDIT_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
074                new FieldDefinition("Description", FieldType.STRING, true, 1L, 132L),
075                new FieldDefinition("OverviewMimeTypeName", FieldType.MIME_TYPE, false, null, null),
076                new FieldDefinition("Overview", FieldType.STRING, false, null, null)
077                ));
078    }
079
080    /** Creates a new instance of EditHarmonizedTariffScheduleCodeTranslationCommand */
081    public EditHarmonizedTariffScheduleCodeTranslationCommand(UserVisitPK userVisitPK, EditHarmonizedTariffScheduleCodeTranslationForm form) {
082        super(userVisitPK, form, COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
083    }
084
085    @Override
086    public EditHarmonizedTariffScheduleCodeTranslationResult getResult() {
087        return ItemResultFactory.getEditHarmonizedTariffScheduleCodeTranslationResult();
088    }
089
090    @Override
091    public HarmonizedTariffScheduleCodeTranslationEdit getEdit() {
092        return ItemEditFactory.getHarmonizedTariffScheduleCodeTranslationEdit();
093    }
094
095    @Override
096    public HarmonizedTariffScheduleCodeTranslation getEntity(EditHarmonizedTariffScheduleCodeTranslationResult result) {
097        var geoControl = Session.getModelController(GeoControl.class);
098        HarmonizedTariffScheduleCodeTranslation harmonizedTariffScheduleCodeTranslation = null;
099        String countryName = spec.getCountryName();
100        GeoCode countryGeoCode = geoControl.getCountryByAlias(countryName);
101
102        if(countryGeoCode != null) {
103            var itemControl = Session.getModelController(ItemControl.class);
104            String harmonizedTariffScheduleCodeName = spec.getHarmonizedTariffScheduleCodeName();
105            HarmonizedTariffScheduleCode harmonizedTariffScheduleCode = itemControl.getHarmonizedTariffScheduleCodeByName(countryGeoCode, harmonizedTariffScheduleCodeName);
106
107            if(harmonizedTariffScheduleCode != null) {
108                var partyControl = Session.getModelController(PartyControl.class);
109                String languageIsoName = spec.getLanguageIsoName();
110                Language language = partyControl.getLanguageByIsoName(languageIsoName);
111
112                if(language != null) {
113                    if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) {
114                        harmonizedTariffScheduleCodeTranslation = itemControl.getHarmonizedTariffScheduleCodeTranslation(harmonizedTariffScheduleCode, language);
115                    } else { // EditMode.UPDATE
116                        harmonizedTariffScheduleCodeTranslation = itemControl.getHarmonizedTariffScheduleCodeTranslationForUpdate(harmonizedTariffScheduleCode, language);
117                    }
118
119                    if(harmonizedTariffScheduleCodeTranslation == null) {
120                        addExecutionError(ExecutionErrors.UnknownHarmonizedTariffScheduleCodeTranslation.name(), countryName, harmonizedTariffScheduleCodeName, languageIsoName);
121                    }
122                } else {
123                    addExecutionError(ExecutionErrors.UnknownLanguageIsoName.name(), languageIsoName);
124                }
125            } else {
126                addExecutionError(ExecutionErrors.UnknownHarmonizedTariffScheduleCodeName.name(), countryName, harmonizedTariffScheduleCodeName);
127            }
128        } else {
129            addExecutionError(ExecutionErrors.UnknownGeoCodeName.name(), countryName);
130        }
131
132        return harmonizedTariffScheduleCodeTranslation;
133    }
134
135    @Override
136    public HarmonizedTariffScheduleCode getLockEntity(HarmonizedTariffScheduleCodeTranslation harmonizedTariffScheduleCodeTranslation) {
137        return harmonizedTariffScheduleCodeTranslation.getHarmonizedTariffScheduleCode();
138    }
139
140    @Override
141    public void fillInResult(EditHarmonizedTariffScheduleCodeTranslationResult result, HarmonizedTariffScheduleCodeTranslation harmonizedTariffScheduleCodeTranslation) {
142        var itemControl = Session.getModelController(ItemControl.class);
143
144        result.setHarmonizedTariffScheduleCodeTranslation(itemControl.getHarmonizedTariffScheduleCodeTranslationTransfer(getUserVisit(), harmonizedTariffScheduleCodeTranslation));
145    }
146
147    MimeType overviewMimeType;
148    
149    @Override
150    public void doLock(HarmonizedTariffScheduleCodeTranslationEdit edit, HarmonizedTariffScheduleCodeTranslation harmonizedTariffScheduleCodeTranslation) {
151        overviewMimeType = harmonizedTariffScheduleCodeTranslation.getOverviewMimeType();
152        
153        edit.setDescription(harmonizedTariffScheduleCodeTranslation.getDescription());
154        edit.setOverviewMimeTypeName(overviewMimeType == null? null: overviewMimeType.getLastDetail().getMimeTypeName());
155        edit.setOverview(harmonizedTariffScheduleCodeTranslation.getOverview());
156    }
157
158    @Override
159    protected void canUpdate(HarmonizedTariffScheduleCodeTranslation harmonizedTariffScheduleCodeTranslation) {
160        String overviewMimeTypeName = edit.getOverviewMimeTypeName();
161        String overview = edit.getOverview();
162        
163        overviewMimeType = MimeTypeLogic.getInstance().checkMimeType(this, overviewMimeTypeName, overview, MimeTypeUsageTypes.TEXT.name(),
164                ExecutionErrors.MissingRequiredOverviewMimeTypeName.name(), ExecutionErrors.MissingRequiredOverview.name(),
165                ExecutionErrors.UnknownOverviewMimeTypeName.name(), ExecutionErrors.UnknownOverviewMimeTypeUsage.name());
166    }
167    
168    @Override
169    public void doUpdate(HarmonizedTariffScheduleCodeTranslation harmonizedTariffScheduleCodeTranslation) {
170        var itemControl = Session.getModelController(ItemControl.class);
171        HarmonizedTariffScheduleCodeTranslationValue harmonizedTariffScheduleCodeTranslationValue = itemControl.getHarmonizedTariffScheduleCodeTranslationValue(harmonizedTariffScheduleCodeTranslation);
172
173        harmonizedTariffScheduleCodeTranslationValue.setDescription(edit.getDescription());
174        harmonizedTariffScheduleCodeTranslationValue.setOverviewMimeTypePK(overviewMimeType == null ? null : overviewMimeType.getPrimaryKey());
175        harmonizedTariffScheduleCodeTranslationValue.setOverview(edit.getOverview());
176
177        itemControl.updateHarmonizedTariffScheduleCodeTranslationFromValue(harmonizedTariffScheduleCodeTranslationValue, getPartyPK());
178    }
179
180}