001// --------------------------------------------------------------------------------
002// Copyright 2002-2024 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.core.server.command;
018
019import com.echothree.control.user.core.common.edit.CoreEditFactory;
020import com.echothree.control.user.core.common.edit.ServerDescriptionEdit;
021import com.echothree.control.user.core.common.form.EditServerDescriptionForm;
022import com.echothree.control.user.core.common.result.CoreResultFactory;
023import com.echothree.control.user.core.common.result.EditServerDescriptionResult;
024import com.echothree.control.user.core.common.spec.ServerDescriptionSpec;
025import com.echothree.model.control.party.common.PartyTypes;
026import com.echothree.model.control.party.server.control.PartyControl;
027import com.echothree.model.control.security.common.SecurityRoleGroups;
028import com.echothree.model.control.security.common.SecurityRoles;
029import com.echothree.model.data.core.server.entity.Server;
030import com.echothree.model.data.core.server.entity.ServerDescription;
031import com.echothree.model.data.core.server.value.ServerDescriptionValue;
032import com.echothree.model.data.party.server.entity.Language;
033import com.echothree.model.data.user.common.pk.UserVisitPK;
034import com.echothree.util.common.message.ExecutionErrors;
035import com.echothree.util.common.validation.FieldDefinition;
036import com.echothree.util.common.validation.FieldType;
037import com.echothree.util.common.command.EditMode;
038import com.echothree.util.server.control.BaseAbstractEditCommand;
039import com.echothree.util.server.control.CommandSecurityDefinition;
040import com.echothree.util.server.control.PartyTypeDefinition;
041import com.echothree.util.server.control.SecurityRoleDefinition;
042import com.echothree.util.server.persistence.Session;
043import java.util.Arrays;
044import java.util.Collections;
045import java.util.List;
046
047public class EditServerDescriptionCommand
048        extends BaseAbstractEditCommand<ServerDescriptionSpec, ServerDescriptionEdit, EditServerDescriptionResult, ServerDescription, Server> {
049    
050    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
051    private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS;
052    private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS;
053    
054    static {
055        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(Collections.unmodifiableList(Arrays.asList(
056                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
057                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), Collections.unmodifiableList(Arrays.asList(
058                        new SecurityRoleDefinition(SecurityRoleGroups.Server.name(), SecurityRoles.Description.name())
059                        )))
060                )));
061        
062        SPEC_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
063                new FieldDefinition("ServerName", FieldType.HOST_NAME, true, null, null),
064                new FieldDefinition("LanguageIsoName", FieldType.ENTITY_NAME, true, null, null)
065                ));
066        
067        EDIT_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
068                new FieldDefinition("Description", FieldType.STRING, true, 1L, 132L)
069                ));
070    }
071    
072    /** Creates a new instance of EditServerDescriptionCommand */
073    public EditServerDescriptionCommand(UserVisitPK userVisitPK, EditServerDescriptionForm form) {
074        super(userVisitPK, form, COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
075    }
076    
077    @Override
078    public EditServerDescriptionResult getResult() {
079        return CoreResultFactory.getEditServerDescriptionResult();
080    }
081
082    @Override
083    public ServerDescriptionEdit getEdit() {
084        return CoreEditFactory.getServerDescriptionEdit();
085    }
086
087    @Override
088    public ServerDescription getEntity(EditServerDescriptionResult result) {
089        var coreControl = getCoreControl();
090        ServerDescription serverDescription = null;
091        String serverName = spec.getServerName();
092        Server server = coreControl.getServerByName(serverName);
093
094        if(server != null) {
095            var partyControl = Session.getModelController(PartyControl.class);
096            String languageIsoName = spec.getLanguageIsoName();
097            Language language = partyControl.getLanguageByIsoName(languageIsoName);
098
099            if(language != null) {
100                if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) {
101                    serverDescription = coreControl.getServerDescription(server, language);
102                } else { // EditMode.UPDATE
103                    serverDescription = coreControl.getServerDescriptionForUpdate(server, language);
104                }
105
106                if(serverDescription == null) {
107                    addExecutionError(ExecutionErrors.UnknownServerDescription.name(), serverName, languageIsoName);
108                }
109            } else {
110                addExecutionError(ExecutionErrors.UnknownLanguageIsoName.name(), languageIsoName);
111            }
112        } else {
113            addExecutionError(ExecutionErrors.UnknownServerName.name(), serverName);
114        }
115
116        return serverDescription;
117    }
118
119    @Override
120    public Server getLockEntity(ServerDescription serverDescription) {
121        return serverDescription.getServer();
122    }
123
124    @Override
125    public void fillInResult(EditServerDescriptionResult result, ServerDescription serverDescription) {
126        var coreControl = getCoreControl();
127
128        result.setServerDescription(coreControl.getServerDescriptionTransfer(getUserVisit(), serverDescription));
129    }
130
131    @Override
132    public void doLock(ServerDescriptionEdit edit, ServerDescription serverDescription) {
133        edit.setDescription(serverDescription.getDescription());
134    }
135
136    @Override
137    public void doUpdate(ServerDescription serverDescription) {
138        var coreControl = getCoreControl();
139        ServerDescriptionValue serverDescriptionValue = coreControl.getServerDescriptionValue(serverDescription);
140        serverDescriptionValue.setDescription(edit.getDescription());
141
142        coreControl.updateServerDescriptionFromValue(serverDescriptionValue, getPartyPK());
143    }
144    
145}