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.form.GetContentPagesForm;
020import com.echothree.control.user.content.common.result.ContentResultFactory;
021import com.echothree.model.control.associate.server.logic.AssociateReferralLogic;
022import com.echothree.model.control.content.server.control.ContentControl;
023import com.echothree.model.data.content.server.entity.ContentCollection;
024import com.echothree.model.data.content.server.entity.ContentPage;
025import com.echothree.model.data.content.server.entity.ContentSection;
026import com.echothree.model.data.content.server.factory.ContentPageFactory;
027import com.echothree.model.data.user.common.pk.UserVisitPK;
028import com.echothree.util.common.command.BaseResult;
029import com.echothree.util.common.message.ExecutionErrors;
030import com.echothree.util.common.validation.FieldDefinition;
031import com.echothree.util.common.validation.FieldType;
032import com.echothree.util.server.control.BasePaginatedMultipleEntitiesCommand;
033import com.echothree.util.server.persistence.Session;
034import java.util.Arrays;
035import java.util.Collection;
036import java.util.Collections;
037import java.util.List;
038import javax.enterprise.context.RequestScoped;
039
040@RequestScoped
041public class GetContentPagesCommand
042        extends BasePaginatedMultipleEntitiesCommand<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() {
060        super(null, FORM_FIELD_DEFINITIONS, true);
061    }
062    
063    private ContentSection contentSection;
064
065    @Override
066    protected void handleForm() {
067        var contentWebAddressName = form.getContentWebAddressName();
068        var contentCollectionName = form.getContentCollectionName();
069        var parameterCount = (contentWebAddressName == null ? 0 : 1) + (contentCollectionName == null ? 0 : 1);
070
071        if(parameterCount == 1) {
072            var contentControl = Session.getModelController(ContentControl.class);
073            ContentCollection contentCollection = null;
074
075            if(contentWebAddressName != null) {
076                var contentWebAddress = contentControl.getContentWebAddressByName(contentWebAddressName);
077
078                if(contentWebAddress != null) {
079                    contentCollection = contentWebAddress.getLastDetail().getContentCollection();
080                } else {
081                    addExecutionError(ExecutionErrors.UnknownContentWebAddressName.name(), contentWebAddressName);
082                }
083            } else {
084                contentCollection = contentControl.getContentCollectionByName(contentCollectionName);
085
086                if(contentCollection == null) {
087                    addExecutionError(ExecutionErrors.UnknownContentCollectionName.name(), contentCollectionName);
088                }
089            }
090
091            if(!hasExecutionErrors()) {
092                var contentSectionName = form.getContentSectionName();
093
094                contentSection = contentSectionName == null ? contentControl.getDefaultContentSection(contentCollection)
095                        : contentControl.getContentSectionByName(contentCollection, contentSectionName);
096
097                if(contentSection == null) {
098                    addExecutionError(ExecutionErrors.UnknownContentSectionName.name(),
099                            contentCollection.getLastDetail().getContentCollectionName(), contentSectionName);
100                }
101            }
102        } else {
103            addExecutionError(ExecutionErrors.InvalidParameterCount.name());
104        }
105
106        if(!hasExecutionErrors()) {
107            AssociateReferralLogic.getInstance().handleAssociateReferral(session, this, form, getUserVisit(),
108                    contentSection.getPrimaryKey(), getPartyPK());
109        }
110    }
111
112    @Override
113    protected Long getTotalEntities() {
114        var contentControl = Session.getModelController(ContentControl.class);
115
116        return hasExecutionErrors() ? null :
117                contentControl.countContentPagesByContentSection(contentSection);
118    }
119
120    @Override
121    protected Collection<ContentPage> getEntities() {
122        var contentControl = Session.getModelController(ContentControl.class);
123        Collection<ContentPage> contentPages = null;
124        
125        if(!hasExecutionErrors()) {
126            contentPages = contentControl.getContentPagesByContentSection(contentSection);
127        }
128
129        return contentPages;
130    }
131    
132    @Override
133    protected BaseResult getResult(Collection<ContentPage> entities) {
134        var result = ContentResultFactory.getGetContentPagesResult();
135
136        if(entities != null) {
137            var contentControl = Session.getModelController(ContentControl.class);
138            var userVisit = getUserVisit();
139
140            result.setContentSection(contentControl.getContentSectionTransfer(userVisit, contentSection));
141
142            if(session.hasLimit(ContentPageFactory.class)) {
143                result.setContentPageCount(getTotalEntities());
144            }
145
146            result.setContentPages(contentControl.getContentPageTransfers(userVisit, entities));
147        }
148
149        return result;
150    }
151    
152}