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.ServerEdit;
021import com.echothree.control.user.core.common.form.EditServerForm;
022import com.echothree.control.user.core.common.result.CoreResultFactory;
023import com.echothree.control.user.core.common.result.EditServerResult;
024import com.echothree.control.user.core.common.spec.ServerSpec;
025import com.echothree.model.control.party.common.PartyTypes;
026import com.echothree.model.control.security.common.SecurityRoleGroups;
027import com.echothree.model.control.security.common.SecurityRoles;
028import com.echothree.model.data.core.server.entity.Server;
029import com.echothree.model.data.core.server.entity.ServerDescription;
030import com.echothree.model.data.core.server.entity.ServerDetail;
031import com.echothree.model.data.core.server.value.ServerDescriptionValue;
032import com.echothree.model.data.core.server.value.ServerDetailValue;
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 java.util.Arrays;
043import java.util.Collections;
044import java.util.List;
045
046public class EditServerCommand
047        extends BaseAbstractEditCommand<ServerSpec, ServerEdit, EditServerResult, Server, Server> {
048    
049    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
050    private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS;
051    private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS;
052    
053    static {
054        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(Collections.unmodifiableList(Arrays.asList(
055                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
056                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), Collections.unmodifiableList(Arrays.asList(
057                        new SecurityRoleDefinition(SecurityRoleGroups.Server.name(), SecurityRoles.Edit.name())
058                        )))
059                )));
060        
061        SPEC_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
062                new FieldDefinition("ServerName", FieldType.HOST_NAME, true, null, null)
063                ));
064        
065        EDIT_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
066                new FieldDefinition("ServerName", FieldType.HOST_NAME, true, null, null),
067                new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null),
068                new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null),
069                new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L)
070                ));
071    }
072    
073    /** Creates a new instance of EditServerCommand */
074    public EditServerCommand(UserVisitPK userVisitPK, EditServerForm form) {
075        super(userVisitPK, form, COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
076    }
077    
078    @Override
079    public EditServerResult getResult() {
080        return CoreResultFactory.getEditServerResult();
081    }
082
083    @Override
084    public ServerEdit getEdit() {
085        return CoreEditFactory.getServerEdit();
086    }
087
088    @Override
089    public Server getEntity(EditServerResult result) {
090        var coreControl = getCoreControl();
091        Server server = null;
092        String serverName = spec.getServerName();
093
094        if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) {
095            server = coreControl.getServerByName(serverName);
096        } else { // EditMode.UPDATE
097            server = coreControl.getServerByNameForUpdate(serverName);
098        }
099
100        if(server != null) {
101            result.setServer(coreControl.getServerTransfer(getUserVisit(), server));
102        } else {
103            addExecutionError(ExecutionErrors.UnknownServerName.name(), serverName);
104        }
105
106        return server;
107    }
108
109    @Override
110    public Server getLockEntity(Server server) {
111        return server;
112    }
113
114    @Override
115    public void fillInResult(EditServerResult result, Server server) {
116        var coreControl = getCoreControl();
117
118        result.setServer(coreControl.getServerTransfer(getUserVisit(), server));
119    }
120
121    @Override
122    public void doLock(ServerEdit edit, Server server) {
123        var coreControl = getCoreControl();
124        ServerDescription serverDescription = coreControl.getServerDescription(server, getPreferredLanguage());
125        ServerDetail serverDetail = server.getLastDetail();
126
127        edit.setServerName(serverDetail.getServerName());
128        edit.setIsDefault(serverDetail.getIsDefault().toString());
129        edit.setSortOrder(serverDetail.getSortOrder().toString());
130
131        if(serverDescription != null) {
132            edit.setDescription(serverDescription.getDescription());
133        }
134    }
135
136    @Override
137    public void canUpdate(Server server) {
138        var coreControl = getCoreControl();
139        String serverName = edit.getServerName();
140        Server duplicateServer = coreControl.getServerByName(serverName);
141
142        if(duplicateServer != null && !server.equals(duplicateServer)) {
143            addExecutionError(ExecutionErrors.DuplicateServerName.name(), serverName);
144        }
145    }
146
147    @Override
148    public void doUpdate(Server server) {
149        var coreControl = getCoreControl();
150        var partyPK = getPartyPK();
151        ServerDetailValue serverDetailValue = coreControl.getServerDetailValueForUpdate(server);
152        ServerDescription serverDescription = coreControl.getServerDescriptionForUpdate(server, getPreferredLanguage());
153        String description = edit.getDescription();
154
155        serverDetailValue.setServerName(edit.getServerName());
156        serverDetailValue.setIsDefault(Boolean.valueOf(edit.getIsDefault()));
157        serverDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder()));
158
159        coreControl.updateServerFromValue(serverDetailValue, partyPK);
160
161        if(serverDescription == null && description != null) {
162            coreControl.createServerDescription(server, getPreferredLanguage(), description, partyPK);
163        } else {
164            if(serverDescription != null && description == null) {
165                coreControl.deleteServerDescription(serverDescription, partyPK);
166            } else {
167                if(serverDescription != null && description != null) {
168                    ServerDescriptionValue serverDescriptionValue = coreControl.getServerDescriptionValue(serverDescription);
169
170                    serverDescriptionValue.setDescription(description);
171                    coreControl.updateServerDescriptionFromValue(serverDescriptionValue, partyPK);
172                }
173            }
174        }
175    }
176    
177}