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.GetContentSectionsForm; 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.data.content.server.entity.ContentCollection; 024import com.echothree.model.data.content.server.entity.ContentSection; 025import com.echothree.model.data.content.server.factory.ContentSectionFactory; 026import com.echothree.model.data.user.common.pk.UserVisitPK; 027import com.echothree.util.common.command.BaseResult; 028import com.echothree.util.common.message.ExecutionErrors; 029import com.echothree.util.common.validation.FieldDefinition; 030import com.echothree.util.common.validation.FieldType; 031import com.echothree.util.server.control.BasePaginatedMultipleEntitiesCommand; 032import com.echothree.util.server.persistence.Session; 033import java.util.Arrays; 034import java.util.Collection; 035import java.util.Collections; 036import java.util.List; 037import javax.enterprise.context.RequestScoped; 038 039@RequestScoped 040public class GetContentSectionsCommand 041 extends BasePaginatedMultipleEntitiesCommand<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() { 059 super(null, FORM_FIELD_DEFINITIONS, true); 060 } 061 062 private ContentCollection contentCollection; 063 private ContentSection parentContentSection; 064 065 @Override 066 protected void handleForm() { 067 var contentWebAddressName = form.getContentWebAddressName(); 068 var contentCollectionName = form.getContentCollectionName(); 069 var parameterCount = (contentWebAddressName == null ? 0 : 1) + (contentCollectionName == null ? 0 : 1); 070 071 if(parameterCount == 1) { 072 var contentControl = Session.getModelController(ContentControl.class); 073 074 if(contentWebAddressName != null) { 075 var contentWebAddress = contentControl.getContentWebAddressByName(contentWebAddressName); 076 077 if(contentWebAddress != null) { 078 contentCollection = contentWebAddress.getLastDetail().getContentCollection(); 079 } else { 080 addExecutionError(ExecutionErrors.UnknownContentWebAddressName.name(), contentWebAddressName); 081 } 082 } else { 083 contentCollection = contentControl.getContentCollectionByName(contentCollectionName); 084 085 if(contentCollection == null) { 086 addExecutionError(ExecutionErrors.UnknownContentCollectionName.name(), contentCollectionName); 087 } 088 } 089 090 if(!hasExecutionErrors()) { 091 var parentContentSectionName = form.getParentContentSectionName(); 092 093 if(parentContentSectionName == null) { 094 parentContentSection = null; 095 } else { 096 parentContentSection = contentControl.getContentSectionByName(contentCollection, parentContentSectionName); 097 098 if(parentContentSection == null) { 099 addExecutionError(ExecutionErrors.UnknownParentContentSectionName.name(), 100 contentCollection.getLastDetail().getContentCollectionName(), parentContentSectionName); 101 } 102 } 103 } 104 } else { 105 addExecutionError(ExecutionErrors.InvalidParameterCount.name()); 106 } 107 108 if(!hasExecutionErrors()) { 109 AssociateReferralLogic.getInstance().handleAssociateReferral(session, this, form, getUserVisitForUpdate(), 110 contentCollection.getPrimaryKey(), getPartyPK()); 111 } 112 } 113 114 @Override 115 protected Long getTotalEntities() { 116 var contentControl = Session.getModelController(ContentControl.class); 117 118 return hasExecutionErrors() ? null : 119 parentContentSection == null ? 120 contentControl.countContentSectionsByContentCollection(contentCollection) : 121 contentControl.countContentSectionsByParentContentSection(parentContentSection); 122 } 123 124 @Override 125 protected Collection<ContentSection> getEntities() { 126 Collection<ContentSection> contentSections = null; 127 128 if(!hasExecutionErrors()) { 129 var contentControl = Session.getModelController(ContentControl.class); 130 131 if(parentContentSection == null) { 132 contentSections = contentControl.getContentSections(contentCollection); 133 } else { 134 contentSections = contentControl.getContentSectionsByParentContentSection(parentContentSection); 135 } 136 } 137 138 return contentSections; 139 } 140 141 @Override 142 protected BaseResult getResult(Collection<ContentSection> entities) { 143 var result = ContentResultFactory.getGetContentSectionsResult(); 144 145 if(entities != null) { 146 var contentControl = Session.getModelController(ContentControl.class); 147 var userVisit = getUserVisit(); 148 149 result.setContentCollection(contentControl.getContentCollectionTransfer(userVisit, contentCollection)); 150 151 if(parentContentSection != null) { 152 result.setParentContentSection(contentControl.getContentSectionTransfer(userVisit, parentContentSection)); 153 } 154 155 if(session.hasLimit(ContentSectionFactory.class)) { 156 result.setContentSectionCount(getTotalEntities()); 157 } 158 159 result.setContentSections(contentControl.getContentSectionTransfers(userVisit, entities)); 160 } 161 162 return result; 163 } 164 165}