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