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 com.echothree.util.server.persistence.Session;
034import java.util.Collection;
035import java.util.List;
036import javax.enterprise.context.Dependent;
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    /** Creates a new instance of GetContentPagesCommand */
057    public GetContentPagesCommand() {
058        super(null, FORM_FIELD_DEFINITIONS, true);
059    }
060    
061    private ContentSection contentSection;
062
063    @Override
064    protected void handleForm() {
065        var contentWebAddressName = form.getContentWebAddressName();
066        var contentCollectionName = form.getContentCollectionName();
067        var parameterCount = (contentWebAddressName == null ? 0 : 1) + (contentCollectionName == null ? 0 : 1);
068
069        if(parameterCount == 1) {
070            var contentControl = Session.getModelController(ContentControl.class);
071            ContentCollection contentCollection = null;
072
073            if(contentWebAddressName != null) {
074                var 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                var contentSectionName = form.getContentSectionName();
091
092                contentSection = contentSectionName == null ? contentControl.getDefaultContentSection(contentCollection)
093                        : contentControl.getContentSectionByName(contentCollection, contentSectionName);
094
095                if(contentSection == null) {
096                    addExecutionError(ExecutionErrors.UnknownContentSectionName.name(),
097                            contentCollection.getLastDetail().getContentCollectionName(), contentSectionName);
098                }
099            }
100        } else {
101            addExecutionError(ExecutionErrors.InvalidParameterCount.name());
102        }
103
104        if(!hasExecutionErrors()) {
105            AssociateReferralLogic.getInstance().handleAssociateReferral(session, this, form, getUserVisit(),
106                    contentSection.getPrimaryKey(), getPartyPK());
107        }
108    }
109
110    @Override
111    protected Long getTotalEntities() {
112        var contentControl = Session.getModelController(ContentControl.class);
113
114        return hasExecutionErrors() ? null :
115                contentControl.countContentPagesByContentSection(contentSection);
116    }
117
118    @Override
119    protected Collection<ContentPage> getEntities() {
120        var contentControl = Session.getModelController(ContentControl.class);
121        Collection<ContentPage> contentPages = null;
122        
123        if(!hasExecutionErrors()) {
124            contentPages = contentControl.getContentPagesByContentSection(contentSection);
125        }
126
127        return contentPages;
128    }
129    
130    @Override
131    protected BaseResult getResult(Collection<ContentPage> entities) {
132        var result = ContentResultFactory.getGetContentPagesResult();
133
134        if(entities != null) {
135            var contentControl = Session.getModelController(ContentControl.class);
136            var userVisit = getUserVisit();
137
138            result.setContentSection(contentControl.getContentSectionTransfer(userVisit, contentSection));
139
140            if(session.hasLimit(ContentPageFactory.class)) {
141                result.setContentPageCount(getTotalEntities());
142            }
143
144            result.setContentPages(contentControl.getContentPageTransfers(userVisit, entities));
145        }
146
147        return result;
148    }
149    
150}