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.util.server.persistence; 018 019import com.echothree.model.control.communication.server.control.CommunicationControl; 020import com.echothree.model.control.core.common.ComponentVendors; 021import com.echothree.model.control.core.common.EntityTypes; 022import com.echothree.model.control.core.server.control.CoreControl; 023import com.echothree.model.control.forum.common.ForumConstants; 024import com.echothree.model.control.forum.server.control.ForumControl; 025import com.echothree.model.control.item.common.ItemConstants; 026import com.echothree.model.control.item.server.control.ItemControl; 027import com.echothree.model.control.party.server.control.PartyControl; 028import com.echothree.model.control.training.server.control.TrainingControl; 029import com.echothree.model.control.user.server.control.UserControl; 030import com.echothree.model.control.vendor.server.control.VendorControl; 031import com.echothree.model.control.warehouse.server.control.WarehouseControl; 032import com.echothree.model.data.communication.server.entity.CommunicationEvent; 033import com.echothree.model.data.core.server.entity.EntityInstance; 034import com.echothree.model.data.core.server.entity.MimeType; 035import com.echothree.model.data.forum.server.entity.Forum; 036import com.echothree.model.data.forum.server.entity.ForumGroup; 037import com.echothree.model.data.forum.server.entity.ForumMessage; 038import com.echothree.model.data.forum.server.entity.ForumMessagePart; 039import com.echothree.model.data.forum.server.entity.ForumMessagePartType; 040import com.echothree.model.data.forum.server.entity.ForumStringMessagePart; 041import com.echothree.model.data.forum.server.entity.ForumThread; 042import com.echothree.model.data.item.server.entity.Item; 043import com.echothree.model.data.item.server.entity.ItemDescription; 044import com.echothree.model.data.item.server.entity.ItemDescriptionDetail; 045import com.echothree.model.data.item.server.entity.ItemDescriptionType; 046import com.echothree.model.data.party.server.entity.Language; 047import com.echothree.model.data.party.server.entity.Party; 048import com.echothree.model.data.training.server.entity.PartyTrainingClass; 049import com.echothree.model.data.training.server.entity.TrainingClass; 050import com.echothree.model.data.training.server.entity.TrainingClassTranslation; 051import com.echothree.model.data.user.server.entity.UserVisit; 052import com.echothree.model.data.vendor.server.entity.VendorItem; 053import com.echothree.model.data.vendor.server.entity.VendorItemDetail; 054import java.util.List; 055 056public class EntityDescriptionUtils { 057 058 private EntityDescriptionUtils() { 059 super(); 060 } 061 062 private static class EntityDescriptionUtilsHolder { 063 static EntityDescriptionUtils instance = new EntityDescriptionUtils(); 064 } 065 066 public static EntityDescriptionUtils getInstance() { 067 return EntityDescriptionUtilsHolder.instance; 068 } 069 070 private ItemDescriptionType getItemDefaultDescriptionType() { 071 ItemControl itemControl = Session.getModelController(ItemControl.class); 072 073 // TODO: Context Cache 074 return itemControl.getItemDescriptionTypeByName(ItemConstants.ItemDescriptionType_DEFAULT_DESCRIPTION); 075 } 076 077 private ItemDescriptionType getItemPurchaseOrderDescriptionType() { 078 ItemControl itemControl = Session.getModelController(ItemControl.class); 079 080 // TODO: Context Cache 081 return itemControl.getItemDescriptionTypeByName(ItemConstants.ItemDescriptionType_PURCHASE_ORDER_DESCRIPTION); 082 } 083 084 private String getDescriptionForForumMessage(UserVisit userVisit, ForumMessage forumMessage) { 085 var forumControl = Session.getModelController(ForumControl.class); 086 String description = null; 087 088 ForumMessagePartType forumMessagePartType = forumControl.getForumMessagePartTypeByName(ForumConstants.ForumMessagePartType_TITLE); 089 ForumMessagePart forumMessagePart = forumControl.getBestForumMessagePart(forumMessage, forumMessagePartType, getLanguage(userVisit)); 090 091 if(forumMessagePart != null) { 092 ForumStringMessagePart forumStringMessagePart = forumControl.getForumStringMessagePart(forumMessagePart); 093 094 description = forumStringMessagePart.getString(); 095 } 096 097 return description; 098 } 099 100 private Language getLanguage(UserVisit userVisit) { 101 var userControl = Session.getModelController(UserControl.class); 102 103 // TODO: Context Cache 104 return userControl.getPreferredLanguageFromUserVisit(userVisit); 105 } 106 107 public String getDescription(UserVisit userVisit, EntityInstance entityInstance) { 108 String description = null; 109 110 if(entityInstance != null) { 111 var entityTypeDetail = entityInstance.getEntityType().getLastDetail(); 112 var componentVendorName = entityTypeDetail.getComponentVendor().getLastDetail().getComponentVendorName(); 113 114 if(componentVendorName.equals(ComponentVendors.ECHO_THREE.name())) { 115 var coreControl = Session.getModelController(CoreControl.class); 116 var entityTypeName = entityTypeDetail.getEntityTypeName(); 117 118 if(entityTypeName.equals(EntityTypes.Party.name())) { 119 var partyControl = Session.getModelController(PartyControl.class); 120 Party party = partyControl.getPartyByEntityInstance(entityInstance); 121 122 description = party == null ? null : partyControl.getBestPartyDescription(party, getLanguage(userVisit)); 123 } else if(entityTypeName.equals(EntityTypes.CommunicationEvent.name())) { 124 var communicationControl = Session.getModelController(CommunicationControl.class); 125 CommunicationEvent communicationEvent = communicationControl.getCommunicationEventByEntityInstance(entityInstance); 126 127 description = communicationEvent == null ? null : communicationEvent.getLastDetail().getCommunicationEventName(); 128 } else if(entityTypeName.equals(EntityTypes.TrainingClass.name())) { 129 var trainingControl = Session.getModelController(TrainingControl.class); 130 TrainingClass trainingClass = trainingControl.getTrainingClassByEntityInstance(entityInstance); 131 132 if(trainingClass != null) { 133 TrainingClassTranslation trainingClassTranslation = trainingControl.getBestTrainingClassTranslation(trainingClass, getLanguage(userVisit)); 134 135 description = trainingClassTranslation == null ? null : trainingClassTranslation.getDescription(); 136 } 137 } else if(entityTypeName.equals(EntityTypes.PartyTrainingClass.name())) { 138 var trainingControl = Session.getModelController(TrainingControl.class); 139 PartyTrainingClass partyTrainingClass = trainingControl.getPartyTrainingClassByEntityInstance(entityInstance); 140 141 description = partyTrainingClass == null ? null : partyTrainingClass.getLastDetail().getPartyTrainingClassName(); 142 } else if(entityTypeName.equals(EntityTypes.Item.name())) { 143 var itemControl = Session.getModelController(ItemControl.class); 144 Item item = itemControl.getItemByEntityInstance(entityInstance); 145 146 description = item == null ? null : itemControl.getBestItemStringDescription(getItemDefaultDescriptionType(), item, getLanguage(userVisit)); 147 } else if(entityTypeName.equals(EntityTypes.ItemDescription.name())) { 148 var itemControl = Session.getModelController(ItemControl.class); 149 ItemDescription itemDescription = itemControl.getItemDescriptionByEntityInstance(entityInstance); 150 151 if(itemDescription != null) { 152 var partyControl = Session.getModelController(PartyControl.class); 153 ItemDescriptionDetail itemDescriptionDetail = itemDescription.getLastDetail(); 154 Language language = getLanguage(userVisit); 155 156 description = new StringBuilder(itemControl.getBestItemDescriptionTypeDescription(itemDescriptionDetail.getItemDescriptionType(), language)).append(", ").append(itemControl.getBestItemStringDescription(getItemDefaultDescriptionType(), itemDescriptionDetail.getItem(), language)).append(", ").append(partyControl.getBestLanguageDescription(itemDescriptionDetail.getLanguage(), language)).toString(); 157 } 158 } else if(entityTypeName.equals(EntityTypes.VendorItem.name())) { 159 var vendorControl = Session.getModelController(VendorControl.class); 160 VendorItem vendorItem = vendorControl.getVendorItemByEntityInstance(entityInstance); 161 162 if(vendorItem != null) { 163 VendorItemDetail vendorItemDetail = vendorItem.getLastDetail(); 164 165 description = vendorItemDetail.getDescription(); 166 167 if(description == null) { 168 var itemControl = Session.getModelController(ItemControl.class); 169 description = itemControl.getBestItemStringDescription(getItemPurchaseOrderDescriptionType(), vendorItemDetail.getItem(), getLanguage(userVisit)); 170 } 171 } 172 } else if(entityTypeName.equals(EntityTypes.ForumGroup.name())) { 173 var forumControl = Session.getModelController(ForumControl.class); 174 ForumGroup forumGroup = forumControl.getForumGroupByEntityInstance(entityInstance); 175 176 description = forumGroup == null ? null : forumControl.getBestForumGroupDescription(forumGroup, getLanguage(userVisit)); 177 } else if(entityTypeName.equals(EntityTypes.Forum.name())) { 178 var forumControl = Session.getModelController(ForumControl.class); 179 Forum forum = forumControl.getForumByEntityInstance(entityInstance); 180 181 description = forum == null ? null : forumControl.getBestForumDescription(forum, getLanguage(userVisit)); 182 } else if(entityTypeName.equals(EntityTypes.ForumThread.name())) { 183 // TODO: A method is needed to push the current limit on ForumMessages, and then pop it. A quick implemention of that 184 // proved difficult due to the limitCache in the Session. Its key is the Class for the BaseFactory vs. a String used by 185 // the limits Map. 186 var forumControl = Session.getModelController(ForumControl.class); 187 ForumThread forumThread = forumControl.getForumThreadByEntityInstance(entityInstance); 188 189 if(forumThread != null) { 190 List<ForumMessage> forumMessages = forumControl.getForumMessagesByForumThread(forumThread); 191 192 if(forumMessages.size() > 0) { 193 description = getDescriptionForForumMessage(userVisit, forumMessages.iterator().next()); 194 } 195 } 196 } else if(entityTypeName.equals(EntityTypes.ForumMessage.name())) { 197 var forumControl = Session.getModelController(ForumControl.class); 198 ForumMessage forumMessage = forumControl.getForumMessageByEntityInstance(entityInstance); 199 200 description = forumMessage == null ? null : getDescriptionForForumMessage(userVisit, forumMessage); 201 } else if(entityTypeName.equals(EntityTypes.MimeType.name())) { 202 MimeType mimeType = coreControl.getMimeTypeByEntityInstance(entityInstance); 203 204 description = mimeType == null ? null : coreControl.getBestMimeTypeDescription(mimeType, getLanguage(userVisit)); 205 } else if(entityTypeName.equals(EntityTypes.Location.name())) { 206 var warehouseControl = Session.getModelController(WarehouseControl.class); 207 var location = warehouseControl.getLocationByEntityInstance(entityInstance); 208 209 description = location == null ? null : warehouseControl.getBestLocationDescription(location, getLanguage(userVisit)); 210 } else if(entityTypeName.equals(EntityTypes.ComponentVendor.name())) { 211 var componentVendor = coreControl.getComponentVendorByEntityInstance(entityInstance); 212 213 description = componentVendor == null ? null : componentVendor.getLastDetail().getDescription(); 214 } else if(entityTypeName.equals(EntityTypes.EntityType.name())) { 215 var entityType = coreControl.getEntityTypeByEntityInstance(entityInstance); 216 217 description = entityType == null ? null : coreControl.getBestEntityTypeDescription(entityType, getLanguage(userVisit)); 218 } 219 } 220 } 221 222 return description; 223 } 224 225}