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.cancellationpolicy.server.command;
018
019import com.echothree.control.user.cancellationpolicy.common.form.CreateCancellationPolicyTranslationForm;
020import com.echothree.model.control.cancellationpolicy.server.control.CancellationPolicyControl;
021import com.echothree.model.control.core.common.MimeTypeUsageTypes;
022import com.echothree.model.control.core.server.logic.MimeTypeLogic;
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.data.user.common.pk.UserVisitPK;
028import com.echothree.util.common.message.ExecutionErrors;
029import com.echothree.util.common.validation.FieldDefinition;
030import com.echothree.util.common.validation.FieldType;
031import com.echothree.util.common.command.BaseResult;
032import com.echothree.util.server.control.BaseSimpleCommand;
033import com.echothree.util.server.control.CommandSecurityDefinition;
034import com.echothree.util.server.control.PartyTypeDefinition;
035import com.echothree.util.server.control.SecurityRoleDefinition;
036import com.echothree.util.server.persistence.Session;
037import java.util.Arrays;
038import java.util.Collections;
039import java.util.List;
040import javax.enterprise.context.RequestScoped;
041
042@RequestScoped
043public class CreateCancellationPolicyTranslationCommand
044        extends BaseSimpleCommand<CreateCancellationPolicyTranslationForm> {
045    
046    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
047    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
048    
049    static {
050        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(Collections.unmodifiableList(Arrays.asList(
051                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
052                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), Collections.unmodifiableList(Arrays.asList(
053                        new SecurityRoleDefinition(SecurityRoleGroups.CancellationPolicy.name(), SecurityRoles.Translation.name())
054                        )))
055                )));
056
057        FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
058                new FieldDefinition("CancellationKindName", FieldType.ENTITY_NAME, true, null, null),
059                new FieldDefinition("CancellationPolicyName", FieldType.ENTITY_NAME, true, null, null),
060                new FieldDefinition("LanguageIsoName", FieldType.ENTITY_NAME, true, null, null),
061                new FieldDefinition("Description", FieldType.STRING, true, 1L, 132L),
062                new FieldDefinition("PolicyMimeTypeName", FieldType.MIME_TYPE, false, null, null),
063                new FieldDefinition("Policy", FieldType.STRING, false, null, null)
064                ));
065    }
066    
067    /** Creates a new instance of CreateCancellationPolicyTranslationCommand */
068    public CreateCancellationPolicyTranslationCommand() {
069        super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false);
070    }
071    
072    @Override
073    protected BaseResult execute() {
074        var cancellationPolicyControl = Session.getModelController(CancellationPolicyControl.class);
075        var cancellationKindName = form.getCancellationKindName();
076        var cancellationKind = cancellationPolicyControl.getCancellationKindByName(cancellationKindName);
077
078        if(cancellationKind != null) {
079            var cancellationPolicyName = form.getCancellationPolicyName();
080            var cancellationPolicy = cancellationPolicyControl.getCancellationPolicyByName(cancellationKind, cancellationPolicyName);
081
082            if(cancellationPolicy != null) {
083                var partyControl = Session.getModelController(PartyControl.class);
084                var languageIsoName = form.getLanguageIsoName();
085                var language = partyControl.getLanguageByIsoName(languageIsoName);
086
087                if(language != null) {
088                    var cancellationPolicyTranslation = cancellationPolicyControl.getCancellationPolicyTranslation(cancellationPolicy, language);
089
090                    if(cancellationPolicyTranslation == null) {
091                        var mimeTypeLogic = MimeTypeLogic.getInstance();
092                        var policyMimeTypeName = form.getPolicyMimeTypeName();
093                        var policy = form.getPolicy();
094
095                        var policyMimeType = mimeTypeLogic.checkMimeType(this, policyMimeTypeName, policy, MimeTypeUsageTypes.TEXT.name(),
096                                ExecutionErrors.MissingRequiredPolicyMimeTypeName.name(), ExecutionErrors.MissingRequiredPolicy.name(),
097                                ExecutionErrors.UnknownPolicyMimeTypeName.name(), ExecutionErrors.UnknownPolicyMimeTypeUsage.name());
098
099                        if(!hasExecutionErrors()) {
100                            var description = form.getDescription();
101
102                            cancellationPolicyControl.createCancellationPolicyTranslation(cancellationPolicy, language, description, policyMimeType, policy,
103                                    getPartyPK());
104                        }
105                    } else {
106                        addExecutionError(ExecutionErrors.DuplicateCancellationPolicyTranslation.name(), cancellationKindName, cancellationPolicyName, languageIsoName);
107                    }
108                } else {
109                    addExecutionError(ExecutionErrors.UnknownLanguageIsoName.name(), languageIsoName);
110                }
111            } else {
112                addExecutionError(ExecutionErrors.UnknownCancellationPolicyName.name(), cancellationKindName, cancellationPolicyName);
113            }
114        } else {
115            addExecutionError(ExecutionErrors.UnknownCancellationKindName.name(), cancellationKindName);
116        }
117
118        return null;
119    }
120    
121}