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.GetContentCatalogForm; 020import com.echothree.control.user.content.common.result.ContentResultFactory; 021import com.echothree.control.user.content.common.result.GetContentCatalogResult; 022import com.echothree.model.control.associate.server.logic.AssociateReferralLogic; 023import com.echothree.model.control.content.server.control.ContentControl; 024import com.echothree.model.control.core.common.EventTypes; 025import com.echothree.model.data.content.server.entity.ContentCatalog; 026import com.echothree.model.data.content.server.entity.ContentCollection; 027import com.echothree.model.data.content.server.entity.ContentWebAddress; 028import com.echothree.model.data.user.common.pk.UserVisitPK; 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.common.command.BaseResult; 033import com.echothree.util.server.control.BaseSingleEntityCommand; 034import com.echothree.util.server.persistence.Session; 035import java.util.Arrays; 036import java.util.Collections; 037import java.util.List; 038 039public class GetContentCatalogCommand 040 extends BaseSingleEntityCommand<ContentCatalog, GetContentCatalogForm> { 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 = Collections.unmodifiableList(Arrays.asList( 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("AssociateProgramName", FieldType.STRING, false, null, null), 051 new FieldDefinition("AssociateName", FieldType.STRING, false, null, null), 052 new FieldDefinition("AssociatePartyContactMechanismName", FieldType.STRING, false, null, null) 053 )); 054 } 055 056 /** Creates a new instance of GetContentCatalogCommand */ 057 public GetContentCatalogCommand(UserVisitPK userVisitPK, GetContentCatalogForm form) { 058 super(userVisitPK, form, null, FORM_FIELD_DEFINITIONS, true); 059 } 060 061 @Override 062 protected ContentCatalog getEntity() { 063 String contentWebAddressName = form.getContentWebAddressName(); 064 String contentCollectionName = form.getContentCollectionName(); 065 var parameterCount = (contentWebAddressName == null ? 0 : 1) + (contentCollectionName == null ? 0 : 1); 066 ContentCatalog contentCatalog = null; 067 068 if(parameterCount == 1) { 069 var contentControl = Session.getModelController(ContentControl.class); 070 ContentCollection contentCollection = null; 071 072 if(contentWebAddressName != null) { 073 ContentWebAddress 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 String contentCatalogName = form.getContentCatalogName(); 090 var partyPK = getPartyPK(); 091 092 contentCatalog = contentCatalogName == null ? contentControl.getDefaultContentCatalog(contentCollection) 093 : contentControl.getContentCatalogByName(contentCollection, contentCatalogName); 094 095 if(contentCatalog != null) { 096 AssociateReferralLogic.getInstance().handleAssociateReferral(session, this, form, getUserVisitForUpdate(), 097 contentCatalog.getPrimaryKey(), partyPK); 098 099 if(!hasExecutionErrors()) { 100 sendEvent(contentCatalog.getPrimaryKey(), EventTypes.READ, null, null, partyPK); 101 } 102 } else { 103 addExecutionError(ExecutionErrors.UnknownContentCatalogName.name(), 104 contentCollection.getLastDetail().getContentCollectionName(), contentCatalogName); 105 } 106 } 107 } else { 108 addExecutionError(ExecutionErrors.InvalidParameterCount.name()); 109 } 110 111 return contentCatalog; 112 } 113 114 @Override 115 protected BaseResult getResult(ContentCatalog contentCatalog) { 116 GetContentCatalogResult result = ContentResultFactory.getGetContentCatalogResult(); 117 118 if(contentCatalog != null) { 119 var contentControl = Session.getModelController(ContentControl.class); 120 121 result.setContentCatalog(contentControl.getContentCatalogTransfer(getUserVisit(), contentCatalog)); 122 } 123 124 return result; 125 } 126 127}