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.edit.PartyApplicationEditorUseEdit;
020import com.echothree.control.user.party.common.edit.PartyEditFactory;
021import com.echothree.control.user.party.common.form.EditPartyApplicationEditorUseForm;
022import com.echothree.control.user.party.common.result.EditPartyApplicationEditorUseResult;
023import com.echothree.control.user.party.common.result.PartyResultFactory;
024import com.echothree.control.user.party.common.spec.PartyApplicationEditorUseSpec;
025import com.echothree.model.control.core.server.logic.ApplicationLogic;
026import com.echothree.model.control.core.server.logic.EditorLogic;
027import com.echothree.model.control.party.common.PartyTypes;
028import com.echothree.model.control.party.server.control.PartyApplicationEditorUseControl;
029import com.echothree.model.control.party.server.logic.PartyLogic;
030import com.echothree.model.control.security.common.SecurityRoleGroups;
031import com.echothree.model.control.security.common.SecurityRoles;
032import com.echothree.model.data.core.server.entity.Application;
033import com.echothree.model.data.core.server.entity.ApplicationEditor;
034import com.echothree.model.data.party.server.entity.PartyApplicationEditorUse;
035import com.echothree.model.data.user.common.pk.UserVisitPK;
036import com.echothree.util.common.command.EditMode;
037import com.echothree.util.common.message.ExecutionErrors;
038import com.echothree.util.common.validation.FieldDefinition;
039import com.echothree.util.common.validation.FieldType;
040import com.echothree.util.server.control.BaseAbstractEditCommand;
041import com.echothree.util.server.control.CommandSecurityDefinition;
042import com.echothree.util.server.control.PartyTypeDefinition;
043import com.echothree.util.server.control.SecurityRoleDefinition;
044import com.echothree.util.server.persistence.Session;
045import java.util.List;
046import javax.enterprise.context.Dependent;
047
048@Dependent
049public class EditPartyApplicationEditorUseCommand
050        extends BaseAbstractEditCommand<PartyApplicationEditorUseSpec, PartyApplicationEditorUseEdit, EditPartyApplicationEditorUseResult, PartyApplicationEditorUse, PartyApplicationEditorUse> {
051    
052    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
053    private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS;
054    private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS;
055    
056    static {
057        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of(
058                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
059                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of(
060                        new SecurityRoleDefinition(SecurityRoleGroups.PartyApplicationEditorUse.name(), SecurityRoles.Edit.name())
061                        ))
062                ));
063        
064        SPEC_FIELD_DEFINITIONS = List.of(
065                new FieldDefinition("PartyName", FieldType.ENTITY_NAME, false, null, null),
066                new FieldDefinition("ApplicationName", FieldType.ENTITY_NAME, true, null, null),
067                new FieldDefinition("ApplicationEditorUseName", FieldType.ENTITY_NAME, true, null, null)
068                );
069        
070        EDIT_FIELD_DEFINITIONS = List.of(
071                new FieldDefinition("EditorName", FieldType.ENTITY_NAME, false, null, null),
072                new FieldDefinition("PreferredHeight", FieldType.UNSIGNED_INTEGER, false, null, null),
073                new FieldDefinition("PreferredWidth", FieldType.UNSIGNED_INTEGER, false, null, null)
074                );
075    }
076    
077    /** Creates a new instance of EditPartyApplicationEditorUseCommand */
078    public EditPartyApplicationEditorUseCommand() {
079        super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
080    }
081    
082    @Override
083    public EditPartyApplicationEditorUseResult getResult() {
084        return PartyResultFactory.getEditPartyApplicationEditorUseResult();
085    }
086
087    @Override
088    public PartyApplicationEditorUseEdit getEdit() {
089        return PartyEditFactory.getPartyApplicationEditorUseEdit();
090    }
091
092    Application application;
093    
094    @Override
095    public PartyApplicationEditorUse getEntity(EditPartyApplicationEditorUseResult result) {
096        PartyApplicationEditorUse partyApplicationEditorUse = null;
097        var partyName = spec.getPartyName();
098        var party = partyName == null ? getParty() : PartyLogic.getInstance().getPartyByName(this, partyName);
099        
100        if(!hasExecutionErrors()) {
101            var applicationName = spec.getApplicationName();
102            
103            application = ApplicationLogic.getInstance().getApplicationByName(this, applicationName);
104            
105            if(!hasExecutionErrors()) {
106                var applicationEditorUseName = spec.getApplicationEditorUseName();
107                var applicationEditorUse = ApplicationLogic.getInstance().getApplicationEditorUseByName(this, application, applicationEditorUseName);
108                
109                if(!hasExecutionErrors()) {
110                    var partyApplicationEditorUseControl = Session.getModelController(PartyApplicationEditorUseControl.class);
111                    
112                    if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) {
113                        partyApplicationEditorUse = partyApplicationEditorUseControl.getPartyApplicationEditorUse(party, applicationEditorUse);
114                    } else { // EditMode.UPDATE
115                        partyApplicationEditorUse = partyApplicationEditorUseControl.getPartyApplicationEditorUseForUpdate(party, applicationEditorUse);
116                    }
117                    
118                    if(partyApplicationEditorUse == null) {
119                        addExecutionError(ExecutionErrors.DuplicatePartyApplicationEditorUse.name(), partyName, applicationName, applicationEditorUseName);
120                    }
121                }
122            }
123        }
124
125        return partyApplicationEditorUse;
126    }
127
128    @Override
129    public PartyApplicationEditorUse getLockEntity(PartyApplicationEditorUse partyApplicationEditorUse) {
130        return partyApplicationEditorUse;
131    }
132
133    @Override
134    public void fillInResult(EditPartyApplicationEditorUseResult result, PartyApplicationEditorUse partyApplicationEditorUse) {
135        var partyApplicationEditorUseControl = Session.getModelController(PartyApplicationEditorUseControl.class);
136
137        result.setPartyApplicationEditorUse(partyApplicationEditorUseControl.getPartyApplicationEditorUseTransfer(getUserVisit(), partyApplicationEditorUse));
138    }
139
140    ApplicationEditor applicationEditor;
141    
142    @Override
143    public void doLock(PartyApplicationEditorUseEdit edit, PartyApplicationEditorUse partyApplicationEditorUse) {
144        var partyApplicationEditorUseDetail = partyApplicationEditorUse.getLastDetail();
145        var preferredHeight = partyApplicationEditorUseDetail.getPreferredHeight();
146        var preferredWidth = partyApplicationEditorUseDetail.getPreferredWidth();
147
148        applicationEditor = partyApplicationEditorUseDetail.getApplicationEditor();
149       
150        edit.setEditorName(applicationEditor == null ? null : applicationEditor.getLastDetail().getEditor().getLastDetail().getEditorName());
151        edit.setPreferredHeight(preferredHeight == null ? null : preferredHeight.toString());
152        edit.setPreferredWidth(preferredWidth == null ? null : preferredWidth.toString());
153    }
154
155    @Override
156    public void canUpdate(PartyApplicationEditorUse partyApplicationEditorUse) {
157        var editorName = edit.getEditorName();
158        var editor = editorName == null ? null : EditorLogic.getInstance().getEditorByName(this, editorName);
159
160        if(!hasExecutionErrors()) {
161            applicationEditor = editor == null ? null : ApplicationLogic.getInstance().getApplicationEditor(this, application, editor);
162        }
163    }
164
165    @Override
166    public void doUpdate(PartyApplicationEditorUse partyApplicationEditorUse) {
167        var partyApplicationEditorUseControl = Session.getModelController(PartyApplicationEditorUseControl.class);
168        var partyPK = getPartyPK();
169        var partyApplicationEditorUseDetailValue = partyApplicationEditorUseControl.getPartyApplicationEditorUseDetailValueForUpdate(partyApplicationEditorUse);
170        var strPreferredHeight = edit.getPreferredHeight();
171        var preferredHeight = strPreferredHeight == null ? null : Integer.valueOf(strPreferredHeight);
172        var strPreferredWidth = edit.getPreferredWidth();
173        var preferredWidth = strPreferredWidth == null ? null : Integer.valueOf(strPreferredWidth);
174
175        partyApplicationEditorUseDetailValue.setApplicationEditorPK(applicationEditor == null ? null : applicationEditor.getPrimaryKey());
176        partyApplicationEditorUseDetailValue.setPreferredHeight(preferredHeight);
177        partyApplicationEditorUseDetailValue.setPreferredWidth(preferredWidth);
178
179        partyApplicationEditorUseControl.updatePartyApplicationEditorUseFromValue(partyApplicationEditorUseDetailValue, partyPK);
180    }
181    
182}