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 java.util.List;
033import javax.enterprise.context.Dependent;
034import javax.inject.Inject;
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    @Inject
055    ContentControl contentControl;
056
057    @Inject
058    AssociateReferralLogic associateReferralLogic;
059
060    
061    /** Creates a new instance of GetContentPageCommand */
062    public GetContentPageCommand() {
063        super(null, FORM_FIELD_DEFINITIONS, true);
064    }
065    
066    @Override
067    protected ContentPage getEntity() {
068        var contentWebAddressName = form.getContentWebAddressName();
069        var contentCollectionName = form.getContentCollectionName();
070        var parameterCount = (contentWebAddressName == null ? 0 : 1) + (contentCollectionName == null ? 0 : 1);
071        ContentPage contentPage = null;
072
073        if(parameterCount == 1) {
074            ContentCollection contentCollection = null;
075
076            if(contentWebAddressName != null) {
077                var 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                var contentSectionName = form.getContentSectionName();
094                var contentPageName = form.getContentPageName();
095                var partyPK = getPartyPK();
096
097                var contentSection = contentSectionName == null ? contentControl.getDefaultContentSection(contentCollection)
098                        : contentControl.getContentSectionByName(contentCollection, contentSectionName);
099
100                if(contentSection != null) {
101                    contentPage = contentPageName == null ? contentControl.getDefaultContentPage(contentSection)
102                            : contentControl.getContentPageByName(contentSection, contentPageName);
103
104                    if(contentPage != null) {
105                        associateReferralLogic.handleAssociateReferral(session, this, form, getUserVisitForUpdate(),
106                                contentPage.getPrimaryKey(), partyPK);
107
108                        if(!hasExecutionErrors()) {
109                            sendEvent(contentPage.getPrimaryKey(), EventTypes.READ, null, null, partyPK);
110                        }
111                    } else {
112                        addExecutionError(ExecutionErrors.UnknownContentPageName.name(),
113                                contentCollection.getLastDetail().getContentCollectionName(), contentSectionName, contentPageName);
114                    }
115                } else {
116                    addExecutionError(ExecutionErrors.UnknownContentSectionName.name(),
117                            contentCollection.getLastDetail().getContentCollectionName(), contentSectionName);
118                }
119            }
120        } else {
121            addExecutionError(ExecutionErrors.InvalidParameterCount.name());
122        }
123
124        return contentPage;
125    }
126    
127    @Override
128    protected BaseResult getResult(ContentPage contentPage) {
129        var result = ContentResultFactory.getGetContentPageResult();
130
131        if (contentPage != null) {
132            result.setContentPage(contentControl.getContentPageTransfer(getUserVisit(), contentPage));
133        }
134
135        return result;
136    }
137    
138}