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