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.GetContentCategoryItemsForm; 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.ContentCategory; 024import com.echothree.model.data.content.server.entity.ContentCategoryItem; 025import com.echothree.model.data.content.server.entity.ContentCollection; 026import com.echothree.model.data.content.server.factory.ContentCategoryItemFactory; 027import com.echothree.model.data.user.common.pk.UserVisitPK; 028import com.echothree.util.common.command.BaseResult; 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.server.control.BasePaginatedMultipleEntitiesCommand; 033import com.echothree.util.server.persistence.Session; 034import java.util.Collection; 035import java.util.List; 036import javax.enterprise.context.Dependent; 037 038@Dependent 039public class GetContentCategoryItemsCommand 040 extends BasePaginatedMultipleEntitiesCommand<ContentCategoryItem, GetContentCategoryItemsForm> { 041 042 // No COMMAND_SECURITY_DEFINITION, anyone may execute this command. 043 private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS; 044 045 static { 046 FORM_FIELD_DEFINITIONS = List.of( 047 new FieldDefinition("ContentWebAddressName", FieldType.HOST_NAME, false, null, null), 048 new FieldDefinition("ContentCollectionName", FieldType.ENTITY_NAME, false, null, null), 049 new FieldDefinition("ContentCatalogName", FieldType.ENTITY_NAME, false, null, null), 050 new FieldDefinition("ContentCategoryName", 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 GetContentCategoryItemsCommand */ 058 public GetContentCategoryItemsCommand() { 059 super(null, FORM_FIELD_DEFINITIONS, true); 060 } 061 062 private ContentCategory contentCategory; 063 064 @Override 065 protected void handleForm() { 066 var contentWebAddressName = form.getContentWebAddressName(); 067 var contentCollectionName = form.getContentCollectionName(); 068 var parameterCount = (contentWebAddressName == null ? 0 : 1) + (contentCollectionName == null ? 0 : 1); 069 070 if(parameterCount == 1) { 071 var contentControl = Session.getModelController(ContentControl.class); 072 ContentCollection contentCollection = null; 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 contentCatalogName = form.getContentCatalogName(); 092 var contentCatalog = contentCatalogName == null ? contentControl.getDefaultContentCatalog(contentCollection) 093 : contentControl.getContentCatalogByName(contentCollection, contentCatalogName); 094 095 if(contentCatalog != null) { 096 var contentCategoryName = form.getContentCategoryName(); 097 098 if(contentCategoryName == null) { 099 contentCategory = contentControl.getDefaultContentCategory(contentCatalog); 100 101 if(contentCategory == null) { 102 addExecutionError(ExecutionErrors.UnknownDefaultContentCategory.name(), 103 contentCollection.getLastDetail().getContentCollectionName(), contentCatalogName); 104 } 105 106 } else { 107 contentCategory = contentControl.getContentCategoryByName(contentCatalog, contentCategoryName); 108 109 if(contentCategory == null) { 110 addExecutionError(ExecutionErrors.UnknownContentCategoryName.name(), 111 contentCollection.getLastDetail().getContentCollectionName(), contentCatalogName, contentCategoryName); 112 } 113 } 114 } else { 115 if(contentCatalogName == null) { 116 addExecutionError(ExecutionErrors.UnknownDefaultContentCatalog.name(), 117 contentCollection.getLastDetail().getContentCollectionName()); 118 } else { 119 addExecutionError(ExecutionErrors.UnknownContentCatalogName.name(), 120 contentCollection.getLastDetail().getContentCollectionName(), contentCatalogName); 121 } 122 } 123 } 124 } else { 125 addExecutionError(ExecutionErrors.InvalidParameterCount.name()); 126 } 127 128 if(!hasExecutionErrors()) { 129 AssociateReferralLogic.getInstance().handleAssociateReferral(session, this, form, getUserVisit(), 130 contentCategory.getPrimaryKey(), getPartyPK()); 131 } 132 } 133 134 @Override 135 protected Long getTotalEntities() { 136 var contentControl = Session.getModelController(ContentControl.class); 137 138 return hasExecutionErrors() ? null : 139 contentControl.countContentCategoryItemsByContentCategory(contentCategory); 140 } 141 142 @Override 143 protected Collection<ContentCategoryItem> getEntities() { 144 Collection<ContentCategoryItem> contentCategoryItems = null; 145 146 if(!hasExecutionErrors()) { 147 var contentControl = Session.getModelController(ContentControl.class); 148 149 contentCategoryItems = contentControl.getContentCategoryItemsByContentCategory(contentCategory); 150 } 151 152 return contentCategoryItems; 153 } 154 155 @Override 156 protected BaseResult getResult(Collection<ContentCategoryItem> entities) { 157 var result = ContentResultFactory.getGetContentCategoryItemsResult(); 158 159 if(entities != null) { 160 var contentControl = Session.getModelController(ContentControl.class); 161 var userVisit = getUserVisit(); 162 163 if(session.hasLimit(ContentCategoryItemFactory.class)) { 164 result.setContentCategoryItemCount(getTotalEntities()); 165 } 166 167 result.setContentCategory(contentControl.getContentCategoryTransfer(userVisit, contentCategory)); 168 result.setContentCategoryItems(contentControl.getContentCategoryItemTransfers(userVisit, entities)); 169 } 170 171 return result; 172 } 173 174}