001// --------------------------------------------------------------------------------
002// Copyright 2002-2026 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.List;
040import javax.enterprise.context.Dependent;
041
042@Dependent
043public class GetPartyApplicationEditorUseCommand
044        extends BaseSimpleCommand<GetPartyApplicationEditorUseForm> {
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(List.of(
051                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
052                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of(
053                        new SecurityRoleDefinition(SecurityRoleGroups.PartyApplicationEditorUse.name(), SecurityRoles.Review.name())
054                        ))
055                ));
056        
057        FORM_FIELD_DEFINITIONS = List.of(
058                new FieldDefinition("PartyName", FieldType.ENTITY_NAME, false, null, null),
059                new FieldDefinition("ApplicationName", FieldType.ENTITY_NAME, true, null, null),
060                new FieldDefinition("ApplicationEditorUseName", FieldType.ENTITY_NAME, true, null, null)
061                );
062    }
063    
064    /** Creates a new instance of GetPartyApplicationEditorUseCommand */
065    public GetPartyApplicationEditorUseCommand() {
066        super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, true);
067    }
068    
069    @Override
070    protected BaseResult execute() {
071        var result = PartyResultFactory.getGetPartyApplicationEditorUseResult();
072        var partyName = form.getPartyName();
073        var party = partyName == null ? getParty() : PartyLogic.getInstance().getPartyByName(this, partyName);
074        
075        if(!hasExecutionErrors()) {
076            var applicationName = form.getApplicationName();
077            var application = ApplicationLogic.getInstance().getApplicationByName(this, applicationName);
078            
079            if(!hasExecutionErrors()) {
080                var applicationEditorUseName = form.getApplicationEditorUseName();
081                var applicationEditorUse = ApplicationLogic.getInstance().getApplicationEditorUseByName(this, application, applicationEditorUseName);
082                
083                if(!hasExecutionErrors()) {
084                    var partyApplicationEditorUseControl = Session.getModelController(PartyApplicationEditorUseControl.class);
085                    var partyApplicationEditorUse = partyApplicationEditorUseControl.getPartyApplicationEditorUse(party, applicationEditorUse);
086                    var partyPK = getPartyPK();
087                    
088                    if(partyApplicationEditorUse == null && partyName == null) {
089                        partyApplicationEditorUse = partyApplicationEditorUseControl.createPartyApplicationEditorUse(party, applicationEditorUse, null, null, null, partyPK);
090                    }
091                    
092                    if(partyApplicationEditorUse != null) {
093                        var userVisit = getUserVisit();
094                        var partyApplicationEditorUseTransfer = partyApplicationEditorUseControl.getPartyApplicationEditorUseTransfer(userVisit, partyApplicationEditorUse);
095                        
096                        // Fill in a few defaults in the TO of the Party is requesting this for themselves.
097                        if(partyName == null) {
098                            var applicationEditorUseDetail = applicationEditorUse.getLastDetail();
099                            var preferredHeight = partyApplicationEditorUseTransfer.getPreferredHeight();
100                            var preferredWidth = partyApplicationEditorUseTransfer.getPreferredWidth();
101                            
102                            if(partyApplicationEditorUseTransfer.getApplicationEditor() == null) {
103                                var applicationControl = Session.getModelController(ApplicationControl.class);
104                                var applicationEditor = applicationEditorUseDetail.getDefaultApplicationEditor();
105                                        
106                                if(applicationEditor == null) {
107                                    applicationEditor = applicationControl.getDefaultApplicationEditor(application);
108                                    
109                                    if(applicationEditor == null) {
110                                        addExecutionError(ExecutionErrors.UnknownDefaultApplicationEditor.name(), applicationName);
111                                    }
112                                }
113                                
114                                if(!hasExecutionErrors()) {
115                                    partyApplicationEditorUseTransfer.setApplicationEditor(applicationControl.getApplicationEditorTransfer(userVisit, applicationEditor));
116                                }
117                            }
118
119                            if(preferredHeight == null || preferredWidth == null) {
120                                if(preferredHeight == null) {
121                                    preferredHeight = applicationEditorUseDetail.getDefaultHeight();
122                                }
123
124                                if(preferredWidth == null) {
125                                    preferredWidth = applicationEditorUseDetail.getDefaultWidth();
126                                }
127
128                                if(preferredHeight == null || preferredWidth == null) {
129                                    var editor = partyApplicationEditorUseTransfer.getApplicationEditor().getEditor();
130
131                                    if(preferredHeight == null) {
132                                        preferredHeight = editor.getDefaultHeight();
133                                    }
134
135                                    if(preferredWidth == null) {
136                                        preferredWidth = editor.getDefaultWidth();
137                                    }
138                                }
139
140                                partyApplicationEditorUseTransfer.setPreferredHeight(preferredHeight);
141                                partyApplicationEditorUseTransfer.setPreferredWidth(preferredWidth);
142                            }
143                        }
144                        
145                        result.setPartyApplicationEditorUse(partyApplicationEditorUseTransfer);
146
147                        sendEvent(partyApplicationEditorUse.getPrimaryKey(), EventTypes.READ, null, null, partyPK);
148                    } else {
149                        addExecutionError(ExecutionErrors.UnknownPartyApplicationEditorUse.name(), partyName, applicationName, applicationEditorUseName);
150                    }
151                }
152            }
153        }
154        
155        return result;
156    }
157    
158}