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.party.server.command;
018
019import com.echothree.control.user.party.common.form.CreatePartyApplicationEditorUseForm;
020import com.echothree.model.control.party.server.control.PartyApplicationEditorUseControl;
021import com.echothree.model.control.core.server.logic.ApplicationLogic;
022import com.echothree.model.control.core.server.logic.EditorLogic;
023import com.echothree.model.control.party.common.PartyTypes;
024import com.echothree.model.control.party.server.logic.PartyLogic;
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.command.BaseResult;
029import com.echothree.util.common.message.ExecutionErrors;
030import com.echothree.util.common.validation.FieldDefinition;
031import com.echothree.util.common.validation.FieldType;
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 CreatePartyApplicationEditorUseCommand
044        extends BaseSimpleCommand<CreatePartyApplicationEditorUseForm> {
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.PartyApplicationEditorUse.name(), SecurityRoles.Create.name())
054                        )))
055                )));
056        
057        FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
058                new FieldDefinition("PartyName", FieldType.ENTITY_NAME, true, null, null),
059                new FieldDefinition("ApplicationName", FieldType.ENTITY_NAME, true, null, null),
060                new FieldDefinition("ApplicationEditorUseName", FieldType.ENTITY_NAME, true, null, null),
061                new FieldDefinition("EditorName", FieldType.ENTITY_NAME, false, null, null),
062                new FieldDefinition("PreferredHeight", FieldType.UNSIGNED_INTEGER, false, null, null),
063                new FieldDefinition("PreferredWidth", FieldType.UNSIGNED_INTEGER, false, null, null)
064                ));
065    }
066    
067    /** Creates a new instance of CreatePartyApplicationEditorUseCommand */
068    public CreatePartyApplicationEditorUseCommand() {
069        super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false);
070    }
071    
072    @Override
073    protected BaseResult execute() {
074        var partyName = form.getPartyName();
075        var party = PartyLogic.getInstance().getPartyByName(this, partyName);
076        
077        if(!hasExecutionErrors()) {
078            var applicationName = form.getApplicationName();
079            var application = ApplicationLogic.getInstance().getApplicationByName(this, applicationName);
080            
081            if(!hasExecutionErrors()) {
082                var applicationEditorUseName = form.getApplicationEditorUseName();
083                var applicationEditorUse = ApplicationLogic.getInstance().getApplicationEditorUseByName(this, application, applicationEditorUseName);
084                
085                if(!hasExecutionErrors()) {
086                    var partyApplicationEditorUseControl = Session.getModelController(PartyApplicationEditorUseControl.class);
087                    var partyApplicationEditorUse = partyApplicationEditorUseControl.getPartyApplicationEditorUse(party, applicationEditorUse);
088                    
089                    if(partyApplicationEditorUse == null) {
090                        var editorName = form.getEditorName();
091                        var editor = editorName == null ? null : EditorLogic.getInstance().getEditorByName(this, editorName);
092                        
093                        if(!hasExecutionErrors()) {
094                            var applicationEditor = editor == null ? null : ApplicationLogic.getInstance().getApplicationEditor(this, application, editor);
095                            
096                            if(!hasExecutionErrors()) {
097                                var strPreferredHeight = form.getPreferredHeight();
098                                var preferredHeight = strPreferredHeight == null ? null : Integer.valueOf(strPreferredHeight);
099                                var strPreferredWidth = form.getPreferredWidth();
100                                var preferredWidth = strPreferredWidth == null ? null : Integer.valueOf(strPreferredWidth);
101
102                                partyApplicationEditorUseControl.createPartyApplicationEditorUse(party, applicationEditorUse, applicationEditor, preferredHeight, preferredWidth,
103                                        getPartyPK());
104                            }
105                        }
106                    } else {
107                        addExecutionError(ExecutionErrors.DuplicatePartyApplicationEditorUse.name(), partyName, applicationName, applicationEditorUseName);
108                    }
109                }
110            }
111        }
112        
113        return null;
114    }
115    
116}