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.CreateContentCollectionForm; 020import com.echothree.control.user.content.common.result.ContentResultFactory; 021import com.echothree.model.control.content.common.ContentSections; 022import com.echothree.model.control.content.server.control.ContentControl; 023import com.echothree.model.control.offer.server.control.OfferControl; 024import com.echothree.model.control.offer.server.control.OfferUseControl; 025import com.echothree.model.control.offer.server.control.SourceControl; 026import com.echothree.model.control.offer.server.control.UseControl; 027import com.echothree.model.control.party.common.PartyTypes; 028import com.echothree.model.control.security.common.SecurityRoleGroups; 029import com.echothree.model.control.security.common.SecurityRoles; 030import com.echothree.model.data.offer.server.entity.OfferUse; 031import com.echothree.model.data.user.common.pk.UserVisitPK; 032import com.echothree.util.common.command.BaseResult; 033import com.echothree.util.common.message.ExecutionErrors; 034import com.echothree.util.common.validation.FieldDefinition; 035import com.echothree.util.common.validation.FieldType; 036import com.echothree.util.server.control.BaseSimpleCommand; 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.List; 042import javax.enterprise.context.Dependent; 043 044@Dependent 045public class CreateContentCollectionCommand 046 extends BaseSimpleCommand<CreateContentCollectionForm> { 047 048 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 049 private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS; 050 051 static { 052 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of( 053 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 054 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of( 055 new SecurityRoleDefinition(SecurityRoleGroups.ContentCollection.name(), SecurityRoles.Create.name()) 056 )) 057 )); 058 059 FORM_FIELD_DEFINITIONS = List.of( 060 new FieldDefinition("ContentCollectionName", FieldType.ENTITY_NAME, true, null, null), 061 new FieldDefinition("DefaultOfferName", FieldType.ENTITY_NAME, false, null, null), 062 new FieldDefinition("DefaultUseName", FieldType.ENTITY_NAME, false, null, null), 063 new FieldDefinition("DefaultSourceName", FieldType.ENTITY_NAME, false, null, null), 064 new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L) 065 ); 066 } 067 068 /** Creates a new instance of CreateContentCollectionCommand */ 069 public CreateContentCollectionCommand() { 070 super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false); 071 } 072 073 @Override 074 protected BaseResult execute() { 075 var result = ContentResultFactory.getCreateContentCollectionResult(); 076 var contentControl = Session.getModelController(ContentControl.class); 077 var contentCollectionName = form.getContentCollectionName(); 078 var contentCollection = contentControl.getContentCollectionByName(contentCollectionName); 079 080 if(contentCollection == null) { 081 var offerControl = Session.getModelController(OfferControl.class); 082 var defaultOfferName = form.getDefaultOfferName(); 083 var defaultUseName = form.getDefaultUseName(); 084 var defaultSourceName = form.getDefaultSourceName(); 085 OfferUse defaultOfferUse = null; 086 087 if(defaultOfferName != null && defaultUseName != null && defaultSourceName == null) { 088 var defaultOffer = offerControl.getOfferByName(defaultOfferName); 089 090 if(defaultOffer != null) { 091 var useControl = Session.getModelController(UseControl.class); 092 var defaultUse = useControl.getUseByName(defaultUseName); 093 094 if(defaultUse != null) { 095 var offerUseControl = Session.getModelController(OfferUseControl.class); 096 defaultOfferUse = offerUseControl.getOfferUse(defaultOffer, defaultUse); 097 098 if(defaultOfferUse == null) { 099 addExecutionError(ExecutionErrors.UnknownDefaultOfferUse.name()); 100 } 101 } else { 102 addExecutionError(ExecutionErrors.UnknownDefaultUseName.name(), defaultUseName); 103 } 104 } else { 105 addExecutionError(ExecutionErrors.UnknownDefaultOfferName.name(), defaultOfferName); 106 } 107 } else if(defaultOfferName == null && defaultUseName == null && defaultSourceName != null) { 108 var sourceControl = Session.getModelController(SourceControl.class); 109 var source = sourceControl.getSourceByName(defaultSourceName); 110 111 if(source != null) { 112 defaultOfferUse = source.getLastDetail().getOfferUse(); 113 } else { 114 addExecutionError(ExecutionErrors.UnknownDefaultSourceName.name(), defaultSourceName); 115 } 116 } else { 117 var sourceControl = Session.getModelController(SourceControl.class); 118 // If all three parameters are null, then try to get the default Source and use its OfferUse. 119 var source = sourceControl.getDefaultSource(); 120 121 if(source != null) { 122 defaultOfferUse = source.getLastDetail().getOfferUse(); 123 } else { 124 addExecutionError(ExecutionErrors.InvalidDefaultOfferOrSourceSpecification.name()); 125 } 126 } 127 128 if(defaultOfferUse != null) { 129 var description = form.getDescription(); 130 var partyPK = getPartyPK(); 131 132 contentCollection = contentControl.createContentCollection(contentCollectionName, defaultOfferUse, partyPK); 133 contentControl.createContentSection(contentCollection, ContentSections.ROOT.toString(), null, false, 0, partyPK); 134 135 if(description != null) { 136 var language = getPreferredLanguage(); 137 138 contentControl.createContentCollectionDescription(contentCollection, language, description, partyPK); 139 } 140 } 141 } else { 142 addExecutionError(ExecutionErrors.DuplicateContentCollectionName.name(), contentCollectionName); 143 } 144 145 if(contentCollection != null) { 146 result.setContentCollectionName(contentCollection.getLastDetail().getContentCollectionName()); 147 result.setEntityRef(contentCollection.getPrimaryKey().getEntityRef()); 148 } 149 150 return result; 151 } 152 153}