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.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.Collection; 034import java.util.List; 035import javax.enterprise.context.Dependent; 036 037@Dependent 038public class GetContentSectionsCommand 039 extends BasePaginatedMultipleEntitiesCommand<ContentSection, GetContentSectionsForm> { 040 041 // No COMMAND_SECURITY_DEFINITION, anyone may execute this command. 042 private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS; 043 044 static { 045 FORM_FIELD_DEFINITIONS = List.of( 046 new FieldDefinition("ContentWebAddressName", FieldType.HOST_NAME, false, null, null), 047 new FieldDefinition("ContentCollectionName", FieldType.ENTITY_NAME, false, null, null), 048 new FieldDefinition("ParentContentSectionName", FieldType.ENTITY_NAME, false, null, null), 049 new FieldDefinition("AssociateProgramName", FieldType.STRING, false, null, null), 050 new FieldDefinition("AssociateName", FieldType.STRING, false, null, null), 051 new FieldDefinition("AssociatePartyContactMechanismName", FieldType.STRING, false, null, null) 052 ); 053 } 054 055 /** Creates a new instance of GetContentSectionsCommand */ 056 public GetContentSectionsCommand() { 057 super(null, FORM_FIELD_DEFINITIONS, true); 058 } 059 060 private ContentCollection contentCollection; 061 private ContentSection parentContentSection; 062 063 @Override 064 protected void handleForm() { 065 var contentWebAddressName = form.getContentWebAddressName(); 066 var contentCollectionName = form.getContentCollectionName(); 067 var parameterCount = (contentWebAddressName == null ? 0 : 1) + (contentCollectionName == null ? 0 : 1); 068 069 if(parameterCount == 1) { 070 var contentControl = Session.getModelController(ContentControl.class); 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 parentContentSectionName = form.getParentContentSectionName(); 090 091 if(parentContentSectionName == null) { 092 parentContentSection = null; 093 } else { 094 parentContentSection = contentControl.getContentSectionByName(contentCollection, parentContentSectionName); 095 096 if(parentContentSection == null) { 097 addExecutionError(ExecutionErrors.UnknownParentContentSectionName.name(), 098 contentCollection.getLastDetail().getContentCollectionName(), parentContentSectionName); 099 } 100 } 101 } 102 } else { 103 addExecutionError(ExecutionErrors.InvalidParameterCount.name()); 104 } 105 106 if(!hasExecutionErrors()) { 107 AssociateReferralLogic.getInstance().handleAssociateReferral(session, this, form, getUserVisitForUpdate(), 108 contentCollection.getPrimaryKey(), getPartyPK()); 109 } 110 } 111 112 @Override 113 protected Long getTotalEntities() { 114 var contentControl = Session.getModelController(ContentControl.class); 115 116 return hasExecutionErrors() ? null : 117 parentContentSection == null ? 118 contentControl.countContentSectionsByContentCollection(contentCollection) : 119 contentControl.countContentSectionsByParentContentSection(parentContentSection); 120 } 121 122 @Override 123 protected Collection<ContentSection> getEntities() { 124 Collection<ContentSection> contentSections = null; 125 126 if(!hasExecutionErrors()) { 127 var contentControl = Session.getModelController(ContentControl.class); 128 129 if(parentContentSection == null) { 130 contentSections = contentControl.getContentSections(contentCollection); 131 } else { 132 contentSections = contentControl.getContentSectionsByParentContentSection(parentContentSection); 133 } 134 } 135 136 return contentSections; 137 } 138 139 @Override 140 protected BaseResult getResult(Collection<ContentSection> entities) { 141 var result = ContentResultFactory.getGetContentSectionsResult(); 142 143 if(entities != null) { 144 var contentControl = Session.getModelController(ContentControl.class); 145 var userVisit = getUserVisit(); 146 147 result.setContentCollection(contentControl.getContentCollectionTransfer(userVisit, contentCollection)); 148 149 if(parentContentSection != null) { 150 result.setParentContentSection(contentControl.getContentSectionTransfer(userVisit, parentContentSection)); 151 } 152 153 if(session.hasLimit(ContentSectionFactory.class)) { 154 result.setContentSectionCount(getTotalEntities()); 155 } 156 157 result.setContentSections(contentControl.getContentSectionTransfers(userVisit, entities)); 158 } 159 160 return result; 161 } 162 163}