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