001// --------------------------------------------------------------------------------
002// Copyright 2002-2026 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 java.util.Collection;
034import java.util.List;
035import javax.enterprise.context.Dependent;
036import javax.inject.Inject;
037
038@Dependent
039public class GetContentPagesCommand
040        extends BasePaginatedMultipleEntitiesCommand<ContentPage, GetContentPagesForm> {
041    
042    // No COMMAND_SECURITY_DEFINITION, anyone may execute this command.
043    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
044    
045    static {
046        FORM_FIELD_DEFINITIONS = List.of(
047                new FieldDefinition("ContentWebAddressName", FieldType.HOST_NAME, false, null, null),
048                new FieldDefinition("ContentCollectionName", FieldType.ENTITY_NAME, false, null, null),
049                new FieldDefinition("ContentSectionName", FieldType.ENTITY_NAME, false, null, null),
050                new FieldDefinition("AssociateProgramName", FieldType.STRING, false, null, null),
051                new FieldDefinition("AssociateName", FieldType.STRING, false, null, null),
052                new FieldDefinition("AssociatePartyContactMechanismName", FieldType.STRING, false, null, null)
053        );
054    }
055
056    @Inject
057    ContentControl contentControl;
058
059    @Inject
060    AssociateReferralLogic associateReferralLogic;
061
062    
063    /** Creates a new instance of GetContentPagesCommand */
064    public GetContentPagesCommand() {
065        super(null, FORM_FIELD_DEFINITIONS, true);
066    }
067    
068    private ContentSection contentSection;
069
070    @Override
071    protected void handleForm() {
072        var contentWebAddressName = form.getContentWebAddressName();
073        var contentCollectionName = form.getContentCollectionName();
074        var parameterCount = (contentWebAddressName == null ? 0 : 1) + (contentCollectionName == null ? 0 : 1);
075
076        if(parameterCount == 1) {
077            ContentCollection contentCollection = null;
078
079            if(contentWebAddressName != null) {
080                var contentWebAddress = contentControl.getContentWebAddressByName(contentWebAddressName);
081
082                if(contentWebAddress != null) {
083                    contentCollection = contentWebAddress.getLastDetail().getContentCollection();
084                } else {
085                    addExecutionError(ExecutionErrors.UnknownContentWebAddressName.name(), contentWebAddressName);
086                }
087            } else {
088                contentCollection = contentControl.getContentCollectionByName(contentCollectionName);
089
090                if(contentCollection == null) {
091                    addExecutionError(ExecutionErrors.UnknownContentCollectionName.name(), contentCollectionName);
092                }
093            }
094
095            if(!hasExecutionErrors()) {
096                var contentSectionName = form.getContentSectionName();
097
098                contentSection = contentSectionName == null ? contentControl.getDefaultContentSection(contentCollection)
099                        : contentControl.getContentSectionByName(contentCollection, contentSectionName);
100
101                if(contentSection == null) {
102                    addExecutionError(ExecutionErrors.UnknownContentSectionName.name(),
103                            contentCollection.getLastDetail().getContentCollectionName(), contentSectionName);
104                }
105            }
106        } else {
107            addExecutionError(ExecutionErrors.InvalidParameterCount.name());
108        }
109
110        if(!hasExecutionErrors()) {
111            associateReferralLogic.handleAssociateReferral(session, this, form, getUserVisit(),
112                    contentSection.getPrimaryKey(), getPartyPK());
113        }
114    }
115
116    @Override
117    protected Long getTotalEntities() {
118        return hasExecutionErrors() ? null :
119                contentControl.countContentPagesByContentSection(contentSection);
120    }
121
122    @Override
123    protected Collection<ContentPage> getEntities() {
124        Collection<ContentPage> contentPages = null;
125        
126        if(!hasExecutionErrors()) {
127            contentPages = contentControl.getContentPagesByContentSection(contentSection);
128        }
129
130        return contentPages;
131    }
132    
133    @Override
134    protected BaseResult getResult(Collection<ContentPage> entities) {
135        var result = ContentResultFactory.getGetContentPagesResult();
136
137        if(entities != null) {
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}