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.GetPartyApplicationEditorUseForm;
020import com.echothree.control.user.party.common.result.PartyResultFactory;
021import com.echothree.model.control.core.common.EventTypes;
022import com.echothree.model.control.core.server.control.ApplicationControl;
023import com.echothree.model.control.core.server.logic.ApplicationLogic;
024import com.echothree.model.control.party.common.PartyTypes;
025import com.echothree.model.control.party.server.control.PartyApplicationEditorUseControl;
026import com.echothree.model.control.party.server.logic.PartyLogic;
027import com.echothree.model.control.security.common.SecurityRoleGroups;
028import com.echothree.model.control.security.common.SecurityRoles;
029import com.echothree.model.data.user.common.pk.UserVisitPK;
030import com.echothree.util.common.command.BaseResult;
031import com.echothree.util.common.message.ExecutionErrors;
032import com.echothree.util.common.validation.FieldDefinition;
033import com.echothree.util.common.validation.FieldType;
034import com.echothree.util.server.control.BaseSimpleCommand;
035import com.echothree.util.server.control.CommandSecurityDefinition;
036import com.echothree.util.server.control.PartyTypeDefinition;
037import com.echothree.util.server.control.SecurityRoleDefinition;
038import com.echothree.util.server.persistence.Session;
039import java.util.Arrays;
040import java.util.Collections;
041import java.util.List;
042import javax.enterprise.context.RequestScoped;
043
044@RequestScoped
045public class GetPartyApplicationEditorUseCommand
046        extends BaseSimpleCommand<GetPartyApplicationEditorUseForm> {
047    
048    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
049    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
050    
051    static {
052        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(Collections.unmodifiableList(Arrays.asList(
053                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
054                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), Collections.unmodifiableList(Arrays.asList(
055                        new SecurityRoleDefinition(SecurityRoleGroups.PartyApplicationEditorUse.name(), SecurityRoles.Review.name())
056                        )))
057                )));
058        
059        FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
060                new FieldDefinition("PartyName", FieldType.ENTITY_NAME, false, null, null),
061                new FieldDefinition("ApplicationName", FieldType.ENTITY_NAME, true, null, null),
062                new FieldDefinition("ApplicationEditorUseName", FieldType.ENTITY_NAME, true, null, null)
063                ));
064    }
065    
066    /** Creates a new instance of GetPartyApplicationEditorUseCommand */
067    public GetPartyApplicationEditorUseCommand() {
068        super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, true);
069    }
070    
071    @Override
072    protected BaseResult execute() {
073        var result = PartyResultFactory.getGetPartyApplicationEditorUseResult();
074        var partyName = form.getPartyName();
075        var party = partyName == null ? getParty() : 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                    var partyPK = getPartyPK();
089                    
090                    if(partyApplicationEditorUse == null && partyName == null) {
091                        partyApplicationEditorUse = partyApplicationEditorUseControl.createPartyApplicationEditorUse(party, applicationEditorUse, null, null, null, partyPK);
092                    }
093                    
094                    if(partyApplicationEditorUse != null) {
095                        var userVisit = getUserVisit();
096                        var partyApplicationEditorUseTransfer = partyApplicationEditorUseControl.getPartyApplicationEditorUseTransfer(userVisit, partyApplicationEditorUse);
097                        
098                        // Fill in a few defaults in the TO of the Party is requesting this for themselves.
099                        if(partyName == null) {
100                            var applicationEditorUseDetail = applicationEditorUse.getLastDetail();
101                            var preferredHeight = partyApplicationEditorUseTransfer.getPreferredHeight();
102                            var preferredWidth = partyApplicationEditorUseTransfer.getPreferredWidth();
103                            
104                            if(partyApplicationEditorUseTransfer.getApplicationEditor() == null) {
105                                var applicationControl = Session.getModelController(ApplicationControl.class);
106                                var applicationEditor = applicationEditorUseDetail.getDefaultApplicationEditor();
107                                        
108                                if(applicationEditor == null) {
109                                    applicationEditor = applicationControl.getDefaultApplicationEditor(application);
110                                    
111                                    if(applicationEditor == null) {
112                                        addExecutionError(ExecutionErrors.UnknownDefaultApplicationEditor.name(), applicationName);
113                                    }
114                                }
115                                
116                                if(!hasExecutionErrors()) {
117                                    partyApplicationEditorUseTransfer.setApplicationEditor(applicationControl.getApplicationEditorTransfer(userVisit, applicationEditor));
118                                }
119                            }
120
121                            if(preferredHeight == null || preferredWidth == null) {
122                                if(preferredHeight == null) {
123                                    preferredHeight = applicationEditorUseDetail.getDefaultHeight();
124                                }
125
126                                if(preferredWidth == null) {
127                                    preferredWidth = applicationEditorUseDetail.getDefaultWidth();
128                                }
129
130                                if(preferredHeight == null || preferredWidth == null) {
131                                    var editor = partyApplicationEditorUseTransfer.getApplicationEditor().getEditor();
132
133                                    if(preferredHeight == null) {
134                                        preferredHeight = editor.getDefaultHeight();
135                                    }
136
137                                    if(preferredWidth == null) {
138                                        preferredWidth = editor.getDefaultWidth();
139                                    }
140                                }
141
142                                partyApplicationEditorUseTransfer.setPreferredHeight(preferredHeight);
143                                partyApplicationEditorUseTransfer.setPreferredWidth(preferredWidth);
144                            }
145                        }
146                        
147                        result.setPartyApplicationEditorUse(partyApplicationEditorUseTransfer);
148
149                        sendEvent(partyApplicationEditorUse.getPrimaryKey(), EventTypes.READ, null, null, partyPK);
150                    } else {
151                        addExecutionError(ExecutionErrors.UnknownPartyApplicationEditorUse.name(), partyName, applicationName, applicationEditorUseName);
152                    }
153                }
154            }
155        }
156        
157        return result;
158    }
159    
160}