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