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.GetContentSectionsForm;
020import com.echothree.control.user.content.common.result.ContentResultFactory;
021import com.echothree.control.user.content.common.result.GetContentSectionsResult;
022import com.echothree.model.control.associate.server.logic.AssociateReferralLogic;
023import com.echothree.model.control.content.server.control.ContentControl;
024import com.echothree.model.data.content.server.entity.ContentCollection;
025import com.echothree.model.data.content.server.entity.ContentSection;
026import com.echothree.model.data.content.server.entity.ContentWebAddress;
027import com.echothree.model.data.user.common.pk.UserVisitPK;
028import com.echothree.model.data.user.server.entity.UserVisit;
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.common.command.BaseResult;
033import com.echothree.util.server.control.BaseMultipleEntitiesCommand;
034import com.echothree.util.server.persistence.Session;
035import java.util.Arrays;
036import java.util.Collection;
037import java.util.Collections;
038import java.util.List;
039
040public class GetContentSectionsCommand
041        extends BaseMultipleEntitiesCommand<ContentSection, GetContentSectionsForm> {
042    
043    // No COMMAND_SECURITY_DEFINITION, anyone may execute this command.
044    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
045    
046    static {
047        FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
048                new FieldDefinition("ContentWebAddressName", FieldType.HOST_NAME, false, null, null),
049                new FieldDefinition("ContentCollectionName", FieldType.ENTITY_NAME, false, null, null),
050                new FieldDefinition("ParentContentSectionName", 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 GetContentSectionsCommand */
058    public GetContentSectionsCommand(UserVisitPK userVisitPK, GetContentSectionsForm form) {
059        super(userVisitPK, form, null, FORM_FIELD_DEFINITIONS, true);
060    }
061    
062    private ContentSection parentContentSection;
063    private ContentCollection contentCollection;
064    
065    @Override
066    protected Collection<ContentSection> getEntities() {
067        String contentWebAddressName = form.getContentWebAddressName();
068        String contentCollectionName = form.getContentCollectionName();
069        var parameterCount = (contentWebAddressName == null ? 0 : 1) + (contentCollectionName == null ? 0 : 1);
070        Collection<ContentSection> contentSections = null;
071
072        if(parameterCount == 1) {
073            var contentControl = Session.getModelController(ContentControl.class);
074
075            if(contentWebAddressName != null) {
076                ContentWebAddress contentWebAddress = contentControl.getContentWebAddressByName(contentWebAddressName);
077
078                if(contentWebAddress != null) {
079                    contentCollection = contentWebAddress.getLastDetail().getContentCollection();
080                } else {
081                    addExecutionError(ExecutionErrors.UnknownContentWebAddressName.name(), contentWebAddressName);
082                }
083            } else {
084                contentCollection = contentControl.getContentCollectionByName(contentCollectionName);
085
086                if(contentCollection == null) {
087                    addExecutionError(ExecutionErrors.UnknownContentCollectionName.name(), contentCollectionName);
088                }
089            }
090
091            if(!hasExecutionErrors()) {
092                String parentContentSectionName = form.getParentContentSectionName();
093                var partyPK = getPartyPK();
094
095                parentContentSection = parentContentSectionName == null ? null : contentControl.getContentSectionByName(contentCollection, parentContentSectionName);
096                
097                if(parentContentSectionName == null || parentContentSection != null) {
098                    AssociateReferralLogic.getInstance().handleAssociateReferral(session, this, form, getUserVisitForUpdate(), contentCollection.getPrimaryKey(), partyPK);
099
100                    if(!hasExecutionErrors()) {
101                        if(parentContentSection == null) {
102                            contentSections = contentControl.getContentSections(contentCollection);
103                        } else {
104                            contentSections = contentControl.getContentSectionsByParentContentSection(parentContentSection);
105                        }
106                    }
107                } else {
108                    addExecutionError(ExecutionErrors.UnknownParentContentSectionName.name(), contentCollection.getLastDetail().getContentCollectionName(), parentContentSectionName);
109                }
110            }
111        } else {
112            addExecutionError(ExecutionErrors.InvalidParameterCount.name());
113        }
114
115        return contentSections;
116    }
117    
118    @Override
119    protected BaseResult getResult(Collection<ContentSection> entities) {
120        GetContentSectionsResult result = ContentResultFactory.getGetContentSectionsResult();
121        
122        if(entities != null) {
123            var contentControl = Session.getModelController(ContentControl.class);
124            UserVisit userVisit = getUserVisit();
125
126            result.setContentCollection(contentControl.getContentCollectionTransfer(userVisit, contentCollection));
127            result.setContentSections(contentControl.getContentSectionTransfers(userVisit, entities));
128
129            if(parentContentSection != null) {
130                result.setParentContentSection(contentControl.getContentSectionTransfer(userVisit, parentContentSection));
131            }
132        }
133                        
134        return result;
135    }
136    
137}