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