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