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.GetContentCatalogItemsForm; 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.control.party.common.PartyTypes; 024import com.echothree.model.control.security.common.SecurityRoleGroups; 025import com.echothree.model.control.security.common.SecurityRoles; 026import com.echothree.model.data.content.server.entity.ContentCatalog; 027import com.echothree.model.data.content.server.entity.ContentCatalogItem; 028import com.echothree.model.data.content.server.entity.ContentCollection; 029import com.echothree.model.data.content.server.factory.ContentCatalogItemFactory; 030import com.echothree.model.data.user.common.pk.UserVisitPK; 031import com.echothree.util.common.message.ExecutionErrors; 032import com.echothree.util.common.validation.FieldDefinition; 033import com.echothree.util.common.validation.FieldType; 034import com.echothree.util.common.command.BaseResult; 035import com.echothree.util.server.control.BaseMultipleEntitiesCommand; 036import com.echothree.util.server.control.BasePaginatedMultipleEntitiesCommand; 037import com.echothree.util.server.control.CommandSecurityDefinition; 038import com.echothree.util.server.control.PartyTypeDefinition; 039import com.echothree.util.server.control.SecurityRoleDefinition; 040import com.echothree.util.server.persistence.Session; 041import java.util.Arrays; 042import java.util.Collection; 043import java.util.Collections; 044import java.util.List; 045import javax.enterprise.context.RequestScoped; 046 047@RequestScoped 048public class GetContentCatalogItemsCommand 049 extends BasePaginatedMultipleEntitiesCommand<ContentCatalogItem, GetContentCatalogItemsForm> { 050 051 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 052 private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS; 053 054 static { 055 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(Collections.unmodifiableList(Arrays.asList( 056 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 057 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), Collections.unmodifiableList(Arrays.asList( 058 new SecurityRoleDefinition(SecurityRoleGroups.ContentCatalogItem.name(), SecurityRoles.List.name()) 059 ))) 060 ))); 061 062 FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 063 new FieldDefinition("ContentWebAddressName", FieldType.HOST_NAME, false, null, null), 064 new FieldDefinition("ContentCollectionName", FieldType.ENTITY_NAME, false, null, null), 065 new FieldDefinition("ContentCatalogName", FieldType.ENTITY_NAME, false, null, null), 066 new FieldDefinition("AssociateProgramName", FieldType.STRING, false, null, null), 067 new FieldDefinition("AssociateName", FieldType.STRING, false, null, null), 068 new FieldDefinition("AssociatePartyContactMechanismName", FieldType.STRING, false, null, null) 069 )); 070 } 071 072 /** Creates a new instance of GetContentCatalogItemsCommand */ 073 public GetContentCatalogItemsCommand() { 074 super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, true); 075 } 076 077 private ContentCatalog contentCatalog; 078 079 @Override 080 protected void handleForm() { 081 var contentWebAddressName = form.getContentWebAddressName(); 082 var contentCollectionName = form.getContentCollectionName(); 083 var parameterCount = (contentWebAddressName == null ? 0 : 1) + (contentCollectionName == null ? 0 : 1); 084 085 if(parameterCount == 1) { 086 var contentControl = Session.getModelController(ContentControl.class); 087 ContentCollection contentCollection = null; 088 089 if(contentWebAddressName != null) { 090 var contentWebAddress = contentControl.getContentWebAddressByName(contentWebAddressName); 091 092 if(contentWebAddress != null) { 093 contentCollection = contentWebAddress.getLastDetail().getContentCollection(); 094 } else { 095 addExecutionError(ExecutionErrors.UnknownContentWebAddressName.name(), contentWebAddressName); 096 } 097 } else { 098 contentCollection = contentControl.getContentCollectionByName(contentCollectionName); 099 100 if(contentCollection == null) { 101 addExecutionError(ExecutionErrors.UnknownContentCollectionName.name(), contentCollectionName); 102 } 103 } 104 105 if(!hasExecutionErrors()) { 106 var contentCatalogName = form.getContentCatalogName(); 107 108 if(contentCatalogName == null) { 109 contentCatalog = contentControl.getDefaultContentCatalog(contentCollection); 110 111 if(contentCatalog == null) { 112 addExecutionError(ExecutionErrors.UnknownDefaultContentCatalog.name(), 113 contentCollection.getLastDetail().getContentCollectionName()); 114 } 115 } else { 116 contentCatalog = contentControl.getContentCatalogByName(contentCollection, contentCatalogName); 117 118 if(contentCatalog == null) { 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, getUserVisitForUpdate(), 130 contentCatalog.getPrimaryKey(), getPartyPK()); 131 132 } 133 } 134 135 @Override 136 protected Long getTotalEntities() { 137 var contentControl = Session.getModelController(ContentControl.class); 138 139 return hasExecutionErrors() ? null : 140 contentControl.countContentCatalogItemsByContentCatalog(contentCatalog); 141 } 142 143 @Override 144 protected Collection<ContentCatalogItem> getEntities() { 145 Collection<ContentCatalogItem> contentCatalogItems = null; 146 147 if(!hasExecutionErrors()) { 148 var contentControl = Session.getModelController(ContentControl.class); 149 150 contentCatalogItems = contentControl.getContentCatalogItemsByContentCatalog(contentCatalog); 151 } 152 153 return contentCatalogItems; 154 } 155 156 @Override 157 protected BaseResult getResult(Collection<ContentCatalogItem> entities) { 158 var result = ContentResultFactory.getGetContentCatalogItemsResult(); 159 160 if(entities != null) { 161 var contentControl = Session.getModelController(ContentControl.class); 162 var userVisit = getUserVisit(); 163 164 result.setContentCatalog(contentControl.getContentCatalogTransfer(userVisit, contentCatalog)); 165 166 if(session.hasLimit(ContentCatalogItemFactory.class)) { 167 result.setContentCatalogItemCount(getTotalEntities()); 168 } 169 170 result.setContentCatalogItems(contentControl.getContentCatalogItemTransfers(userVisit, entities)); 171 } 172 173 return result; 174 } 175 176}