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