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.content.server.command;
018
019import com.echothree.control.user.content.common.edit.ContentEditFactory;
020import com.echothree.control.user.content.common.edit.ContentWebAddressEdit;
021import com.echothree.control.user.content.common.form.EditContentWebAddressForm;
022import com.echothree.control.user.content.common.result.ContentResultFactory;
023import com.echothree.control.user.content.common.result.EditContentWebAddressResult;
024import com.echothree.control.user.content.common.spec.ContentWebAddressSpec;
025import com.echothree.model.control.content.server.control.ContentControl;
026import com.echothree.model.control.party.common.PartyTypes;
027import com.echothree.model.control.security.common.SecurityRoleGroups;
028import com.echothree.model.control.security.common.SecurityRoles;
029import com.echothree.model.data.content.server.entity.ContentCollection;
030import com.echothree.model.data.content.server.entity.ContentWebAddress;
031import com.echothree.model.data.user.common.pk.UserVisitPK;
032import com.echothree.util.common.message.ExecutionErrors;
033import com.echothree.util.common.validation.FieldDefinition;
034import com.echothree.util.common.validation.FieldType;
035import com.echothree.util.common.command.EditMode;
036import com.echothree.util.server.control.BaseAbstractEditCommand;
037import com.echothree.util.server.control.CommandSecurityDefinition;
038import com.echothree.util.server.control.PartyTypeDefinition;
039import com.echothree.util.server.control.SecurityRoleDefinition;
040import com.echothree.util.server.persistence.Session;
041import java.util.Arrays;
042import java.util.Collections;
043import java.util.List;
044import javax.enterprise.context.RequestScoped;
045
046@RequestScoped
047public class EditContentWebAddressCommand
048        extends BaseAbstractEditCommand<ContentWebAddressSpec, ContentWebAddressEdit, EditContentWebAddressResult, ContentWebAddress, ContentWebAddress> {
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.ContentWebAddress.name(), SecurityRoles.Edit.name())
059                        )))
060                )));
061        
062        SPEC_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
063                new FieldDefinition("ContentWebAddressName", FieldType.HOST_NAME, true, null, null)
064                ));
065        
066        EDIT_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
067                new FieldDefinition("ContentWebAddressName", FieldType.HOST_NAME, true, null, null),
068                new FieldDefinition("ContentCollectionName", FieldType.ENTITY_NAME, true, null, null),
069                new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L)
070                ));
071    }
072    
073    /** Creates a new instance of EditContentWebAddressCommand */
074    public EditContentWebAddressCommand() {
075        super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
076    }
077    
078    @Override
079    public EditContentWebAddressResult getResult() {
080        return ContentResultFactory.getEditContentWebAddressResult();
081    }
082    
083    @Override
084    public ContentWebAddressEdit getEdit() {
085        return ContentEditFactory.getContentWebAddressEdit();
086    }
087    
088    @Override
089    public ContentWebAddress getEntity(EditContentWebAddressResult result) {
090        var contentControl = Session.getModelController(ContentControl.class);
091        ContentWebAddress contentWebAddress;
092        var contentWebAddressName = spec.getContentWebAddressName();
093
094        if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) {
095            contentWebAddress = contentControl.getContentWebAddressByName(contentWebAddressName);
096        } else { // EditMode.UPDATE
097            contentWebAddress = contentControl.getContentWebAddressByNameForUpdate(contentWebAddressName);
098        }
099
100        if(contentWebAddress != null) {
101            result.setContentWebAddress(contentControl.getContentWebAddressTransfer(getUserVisit(), contentWebAddress));
102        } else {
103            addExecutionError(ExecutionErrors.UnknownContentWebAddressName.name(), contentWebAddressName);
104        }
105
106        return contentWebAddress;
107    }
108    
109    @Override
110    public ContentWebAddress getLockEntity(ContentWebAddress contentWebAddress) {
111        return contentWebAddress;
112    }
113    
114    @Override
115    public void fillInResult(EditContentWebAddressResult result, ContentWebAddress contentWebAddress) {
116        var contentControl = Session.getModelController(ContentControl.class);
117        
118        result.setContentWebAddress(contentControl.getContentWebAddressTransfer(getUserVisit(), contentWebAddress));
119    }
120    
121    @Override
122    public void doLock(ContentWebAddressEdit edit, ContentWebAddress contentWebAddress) {
123        var contentControl = Session.getModelController(ContentControl.class);
124        var contentWebAddressDescription = contentControl.getContentWebAddressDescription(contentWebAddress, getPreferredLanguage());
125        var contentWebAddressDetail = contentWebAddress.getLastDetail();
126
127        edit.setContentWebAddressName(contentWebAddressDetail.getContentWebAddressName());
128        edit.setContentCollectionName(contentWebAddressDetail.getContentCollection().getLastDetail().getContentCollectionName());
129
130        if(contentWebAddressDescription != null) {
131            edit.setDescription(contentWebAddressDescription.getDescription());
132        }
133    }
134    
135    ContentCollection contentCollection = null;
136    
137    @Override
138    public void canUpdate(ContentWebAddress contentWebAddress) {
139        var contentControl = Session.getModelController(ContentControl.class);
140        var contentWebAddressName = edit.getContentWebAddressName();
141        var duplicateContentWebAddress = contentControl.getContentWebAddressByName(contentWebAddressName);
142
143        if(duplicateContentWebAddress == null || contentWebAddress.equals(duplicateContentWebAddress)) {
144            var contentCollectionName = edit.getContentCollectionName();
145
146            contentCollection = contentControl.getContentCollectionByName(contentCollectionName);
147
148            if(contentCollection == null) {
149                addExecutionError(ExecutionErrors.UnknownContentCollectionName.name(), contentCollectionName);
150            }
151        } else {
152            addExecutionError(ExecutionErrors.DuplicateContentWebAddressName.name(), contentWebAddressName);
153        }
154    }
155    
156    @Override
157    public void doUpdate(ContentWebAddress contentWebAddress) {
158        var contentControl = Session.getModelController(ContentControl.class);
159        var partyPK = getPartyPK();
160        var contentWebAddressDetailValue = contentControl.getContentWebAddressDetailValueForUpdate(contentWebAddress);
161        var contentWebAddressDescription = contentControl.getContentWebAddressDescriptionForUpdate(contentWebAddress, getPreferredLanguage());
162        var description = edit.getDescription();
163
164        contentWebAddressDetailValue.setContentWebAddressName(edit.getContentWebAddressName());
165        contentWebAddressDetailValue.setContentCollectionPK(contentCollection.getPrimaryKey());
166
167        contentControl.updateContentWebAddressFromValue(contentWebAddressDetailValue, partyPK);
168
169        if(contentWebAddressDescription == null && description != null) {
170            contentControl.createContentWebAddressDescription(contentWebAddress, getPreferredLanguage(), description, partyPK);
171        } else if(contentWebAddressDescription != null && description == null) {
172            contentControl.deleteContentWebAddressDescription(contentWebAddressDescription, partyPK);
173        } else if(contentWebAddressDescription != null && description != null) {
174            var contentWebAddressDescriptionValue = contentControl.getContentWebAddressDescriptionValue(contentWebAddressDescription);
175
176            contentWebAddressDescriptionValue.setDescription(description);
177            contentControl.updateContentWebAddressDescriptionFromValue(contentWebAddressDescriptionValue, partyPK);
178        }
179    }
180    
181}