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.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.Arrays; 035import java.util.Collection; 036import java.util.Collections; 037import java.util.List; 038import javax.enterprise.context.RequestScoped; 039 040@RequestScoped 041public class GetContentCategoryItemsCommand 042 extends BasePaginatedMultipleEntitiesCommand<ContentCategoryItem, GetContentCategoryItemsForm> { 043 044 // No COMMAND_SECURITY_DEFINITION, anyone may execute this command. 045 private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS; 046 047 static { 048 FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 049 new FieldDefinition("ContentWebAddressName", FieldType.HOST_NAME, false, null, null), 050 new FieldDefinition("ContentCollectionName", FieldType.ENTITY_NAME, false, null, null), 051 new FieldDefinition("ContentCatalogName", FieldType.ENTITY_NAME, false, null, null), 052 new FieldDefinition("ContentCategoryName", FieldType.ENTITY_NAME, false, null, null), 053 new FieldDefinition("AssociateProgramName", FieldType.STRING, false, null, null), 054 new FieldDefinition("AssociateName", FieldType.STRING, false, null, null), 055 new FieldDefinition("AssociatePartyContactMechanismName", FieldType.STRING, false, null, null) 056 )); 057 } 058 059 /** Creates a new instance of GetContentCategoryItemsCommand */ 060 public GetContentCategoryItemsCommand() { 061 super(null, FORM_FIELD_DEFINITIONS, true); 062 } 063 064 private ContentCategory contentCategory; 065 066 @Override 067 protected void handleForm() { 068 var contentWebAddressName = form.getContentWebAddressName(); 069 var contentCollectionName = form.getContentCollectionName(); 070 var parameterCount = (contentWebAddressName == null ? 0 : 1) + (contentCollectionName == null ? 0 : 1); 071 072 if(parameterCount == 1) { 073 var contentControl = Session.getModelController(ContentControl.class); 074 ContentCollection contentCollection = null; 075 076 if(contentWebAddressName != null) { 077 var contentWebAddress = contentControl.getContentWebAddressByName(contentWebAddressName); 078 079 if(contentWebAddress != null) { 080 contentCollection = contentWebAddress.getLastDetail().getContentCollection(); 081 } else { 082 addExecutionError(ExecutionErrors.UnknownContentWebAddressName.name(), contentWebAddressName); 083 } 084 } else { 085 contentCollection = contentControl.getContentCollectionByName(contentCollectionName); 086 087 if(contentCollection == null) { 088 addExecutionError(ExecutionErrors.UnknownContentCollectionName.name(), contentCollectionName); 089 } 090 } 091 092 if(!hasExecutionErrors()) { 093 var contentCatalogName = form.getContentCatalogName(); 094 var contentCatalog = contentCatalogName == null ? contentControl.getDefaultContentCatalog(contentCollection) 095 : contentControl.getContentCatalogByName(contentCollection, contentCatalogName); 096 097 if(contentCatalog != null) { 098 var contentCategoryName = form.getContentCategoryName(); 099 100 if(contentCategoryName == null) { 101 contentCategory = contentControl.getDefaultContentCategory(contentCatalog); 102 103 if(contentCategory == null) { 104 addExecutionError(ExecutionErrors.UnknownDefaultContentCategory.name(), 105 contentCollection.getLastDetail().getContentCollectionName(), contentCatalogName); 106 } 107 108 } else { 109 contentCategory = contentControl.getContentCategoryByName(contentCatalog, contentCategoryName); 110 111 if(contentCategory == null) { 112 addExecutionError(ExecutionErrors.UnknownContentCategoryName.name(), 113 contentCollection.getLastDetail().getContentCollectionName(), contentCatalogName, contentCategoryName); 114 } 115 } 116 } else { 117 if(contentCatalogName == null) { 118 addExecutionError(ExecutionErrors.UnknownDefaultContentCatalog.name(), 119 contentCollection.getLastDetail().getContentCollectionName()); 120 } else { 121 addExecutionError(ExecutionErrors.UnknownContentCatalogName.name(), 122 contentCollection.getLastDetail().getContentCollectionName(), contentCatalogName); 123 } 124 } 125 } 126 } else { 127 addExecutionError(ExecutionErrors.InvalidParameterCount.name()); 128 } 129 130 if(!hasExecutionErrors()) { 131 AssociateReferralLogic.getInstance().handleAssociateReferral(session, this, form, getUserVisit(), 132 contentCategory.getPrimaryKey(), getPartyPK()); 133 } 134 } 135 136 @Override 137 protected Long getTotalEntities() { 138 var contentControl = Session.getModelController(ContentControl.class); 139 140 return hasExecutionErrors() ? null : 141 contentControl.countContentCategoryItemsByContentCategory(contentCategory); 142 } 143 144 @Override 145 protected Collection<ContentCategoryItem> getEntities() { 146 Collection<ContentCategoryItem> contentCategoryItems = null; 147 148 if(!hasExecutionErrors()) { 149 var contentControl = Session.getModelController(ContentControl.class); 150 151 contentCategoryItems = contentControl.getContentCategoryItemsByContentCategory(contentCategory); 152 } 153 154 return contentCategoryItems; 155 } 156 157 @Override 158 protected BaseResult getResult(Collection<ContentCategoryItem> entities) { 159 var result = ContentResultFactory.getGetContentCategoryItemsResult(); 160 161 if(entities != null) { 162 var contentControl = Session.getModelController(ContentControl.class); 163 var userVisit = getUserVisit(); 164 165 if(session.hasLimit(ContentCategoryItemFactory.class)) { 166 result.setContentCategoryItemCount(getTotalEntities()); 167 } 168 169 result.setContentCategory(contentControl.getContentCategoryTransfer(userVisit, contentCategory)); 170 result.setContentCategoryItems(contentControl.getContentCategoryItemTransfers(userVisit, entities)); 171 } 172 173 return result; 174 } 175 176}