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.content.server.command;
018
019import com.echothree.control.user.content.common.form.GetContentSectionForm;
020import com.echothree.control.user.content.common.result.ContentResultFactory;
021import com.echothree.control.user.content.common.result.GetContentSectionResult;
022import com.echothree.model.control.associate.server.logic.AssociateReferralLogic;
023import com.echothree.model.control.content.server.control.ContentControl;
024import com.echothree.model.control.core.common.EventTypes;
025import com.echothree.model.data.content.server.entity.ContentCollection;
026import com.echothree.model.data.content.server.entity.ContentSection;
027import com.echothree.model.data.content.server.entity.ContentWebAddress;
028import com.echothree.model.data.user.common.pk.UserVisitPK;
029import com.echothree.model.data.user.server.entity.UserVisit;
030import com.echothree.util.common.message.ExecutionErrors;
031import com.echothree.util.common.validation.FieldDefinition;
032import com.echothree.util.common.validation.FieldType;
033import com.echothree.util.common.command.BaseResult;
034import com.echothree.util.server.control.BaseSingleEntityCommand;
035import com.echothree.util.server.persistence.Session;
036import java.util.Arrays;
037import java.util.Collections;
038import java.util.List;
039
040public class GetContentSectionCommand
041        extends BaseSingleEntityCommand<ContentSection, GetContentSectionForm> {
042    
043    // No COMMAND_SECURITY_DEFINITION, anyone may execute this command.
044    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
045    
046    static {
047        FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
048                new FieldDefinition("ContentWebAddressName", FieldType.HOST_NAME, false, null, null),
049                new FieldDefinition("ContentCollectionName", FieldType.ENTITY_NAME, false, null, null),
050                new FieldDefinition("ContentSectionName", FieldType.ENTITY_NAME, false, null, null),
051                new FieldDefinition("AssociateProgramName", FieldType.STRING, false, null, null),
052                new FieldDefinition("AssociateName", FieldType.STRING, false, null, null),
053                new FieldDefinition("AssociatePartyContactMechanismName", FieldType.STRING, false, null, null)
054                ));
055    }
056    
057    /** Creates a new instance of GetContentSectionCommand */
058    public GetContentSectionCommand(UserVisitPK userVisitPK, GetContentSectionForm form) {
059        super(userVisitPK, form, null, FORM_FIELD_DEFINITIONS, true);
060    }
061    
062    @Override
063    protected ContentSection getEntity() {
064        String contentWebAddressName = form.getContentWebAddressName();
065        String contentCollectionName = form.getContentCollectionName();
066        var parameterCount = (contentWebAddressName == null ? 0 : 1) + (contentCollectionName == null ? 0 : 1);
067        ContentSection contentSection = null;
068
069        if(parameterCount == 1) {
070            var contentControl = Session.getModelController(ContentControl.class);
071            ContentCollection contentCollection = null;
072
073            if(contentWebAddressName != null) {
074                ContentWebAddress contentWebAddress = contentControl.getContentWebAddressByName(contentWebAddressName);
075
076                if(contentWebAddress != null) {
077                    contentCollection = contentWebAddress.getLastDetail().getContentCollection();
078                } else {
079                    addExecutionError(ExecutionErrors.UnknownContentWebAddressName.name(), contentWebAddressName);
080                }
081            } else {
082                contentCollection = contentControl.getContentCollectionByName(contentCollectionName);
083
084                if(contentCollection == null) {
085                    addExecutionError(ExecutionErrors.UnknownContentCollectionName.name(), contentCollectionName);
086                }
087            }
088
089            if(!hasExecutionErrors()) {
090                String contentSectionName = form.getContentSectionName();
091                var partyPK = getPartyPK();
092                UserVisit userVisit = getUserVisitForUpdate();
093
094                contentSection = contentSectionName == null ? contentControl.getDefaultContentSection(contentCollection)
095                        : contentControl.getContentSectionByName(contentCollection, contentSectionName);
096
097                if(contentSection != null) {
098                    AssociateReferralLogic.getInstance().handleAssociateReferral(session, this, form, userVisit, contentSection.getPrimaryKey(), partyPK);
099
100                    if(!hasExecutionErrors()) {
101                        sendEvent(contentSection.getPrimaryKey(), EventTypes.READ, null, null, partyPK);
102                    }
103                } else {
104                    addExecutionError(ExecutionErrors.UnknownContentSectionName.name(), contentCollection.getLastDetail().getContentCollectionName(), contentSectionName);
105                }
106            }
107        } else {
108            addExecutionError(ExecutionErrors.InvalidParameterCount.name());
109        }
110        
111        return contentSection;
112    }
113    
114    @Override
115    protected BaseResult getResult(ContentSection contentSection) {
116        GetContentSectionResult result = ContentResultFactory.getGetContentSectionResult();
117        
118        if(contentSection != null) {
119            var contentControl = Session.getModelController(ContentControl.class);
120            
121            result.setContentSection(contentControl.getContentSectionTransfer(getUserVisit(), contentSection));
122        }
123        
124        return result;
125    }
126}