1 package com.dotmarketing.portlets.structure.factories; 2 3 import java.util.ArrayList ; 4 import java.util.List ; 5 6 import com.dotmarketing.beans.Identifier; 7 import com.dotmarketing.beans.Tree; 8 import com.dotmarketing.cache.IdentifierCache; 9 import com.dotmarketing.db.DbConnectionFactory; 10 import com.dotmarketing.db.DotHibernate; 11 import com.dotmarketing.exception.DotRuntimeException; 12 import com.dotmarketing.factories.IdentifierFactory; 13 import com.dotmarketing.factories.InodeFactory; 14 import com.dotmarketing.factories.TreeFactory; 15 import com.dotmarketing.portlets.contentlet.model.Contentlet; 16 import com.dotmarketing.portlets.structure.model.Relationship; 17 import com.dotmarketing.portlets.structure.model.Structure; 18 import com.dotmarketing.util.Logger; 19 import com.dotmarketing.util.UtilMethods; 20 21 public class RelationshipFactory { 22 public static Relationship getRelationshipByInode(long inode) { 24 return (Relationship) InodeFactory.getInode(inode, Relationship.class); 25 } 26 27 public static Relationship getRelationshipByInode(String inode) { 28 return (Relationship) InodeFactory.getInode(inode, Relationship.class); 29 } 30 31 public static List <Relationship> getAllRelationships() { 32 String orderBy = "inode"; 33 return getRelationships(orderBy); 34 } 35 36 @SuppressWarnings ("unchecked") 37 public static List <Relationship> getRelationships(String orderBy) { 38 List <Relationship> list = new ArrayList <Relationship>(); 39 String query = "select {relationship.*} from relationship, inode relationship_1_, structure parentstruct, " 40 + "structure childstruct where relationship.inode = relationship_1_.inode and " 41 + "relationship.parent_structure_inode = parentstruct.inode and " 42 + "relationship.child_structure_inode = childstruct.inode order by " + orderBy; 43 DotHibernate dh = new DotHibernate(Relationship.class); 44 dh.setSQLQuery(query); 45 list = dh.list(); 46 return list; 47 } 48 49 public static Relationship getRelationshipByRelationTypeValue(String typeValue) { 50 return (Relationship) InodeFactory.getInodeOfClassByCondition(Relationship.class, "relation_type_value = '" 51 + typeValue + "'"); 52 } 53 54 @SuppressWarnings ("unchecked") 55 public static List <Relationship> getAllRelationshipsByStructure(Structure st) { 56 List <Relationship> list = new ArrayList <Relationship>(); 57 String query = "select {relationship.*} from relationship, inode relationship_1_ " 58 + "where relationship.inode = relationship_1_.inode and " 59 + "(relationship.parent_structure_inode = ? or relationship.child_structure_inode = ?)"; 60 DotHibernate dh = new DotHibernate(Relationship.class); 61 dh.setSQLQuery(query); 62 dh.setParam(st.getInode()); 63 dh.setParam(st.getInode()); 64 list = dh.list(); 65 return list; 66 } 67 68 public static List <Relationship> getAllRelationshipsByStructure(Structure st, boolean hasParent) { 69 List <Relationship> list = new ArrayList <Relationship>(); 70 String query = "select {relationship.*} from relationship, inode relationship_1_ " 71 + "where relationship.inode = relationship_1_.inode and "; 72 if (hasParent) 73 query += "(relationship.parent_structure_inode = ?)"; 74 else 75 query += "(relationship.child_structure_inode = ?)"; 76 DotHibernate dh = new DotHibernate(Relationship.class); 77 dh.setSQLQuery(query); 78 dh.setParam(st.getInode()); 79 list = dh.list(); 80 return list; 81 } 82 83 public static List <Contentlet> getAllRelationshipRecords(Relationship relationship, Contentlet contentlet) { 84 long stInode = contentlet.getStructure().getInode(); 85 List <Contentlet> matches = new ArrayList <Contentlet>(); 86 if (relationship.getParentStructureInode() == stInode) { 87 matches = getAllRelationshipRecords(relationship, contentlet, true); 88 } else if (relationship.getChildStructureInode() == stInode) { 89 matches = getAllRelationshipRecords(relationship, contentlet, false); 90 } 91 return matches; 92 } 93 94 public static List <Contentlet> getAllRelationshipRecords(Relationship relationship, Contentlet contentlet, 95 boolean hasParent) { 96 return getAllRelationshipRecords (relationship, contentlet, hasParent, false, ""); 97 } 98 99 public static List <Contentlet> getAllRelationshipRecords(Relationship relationship, Contentlet contentlet, 100 boolean hasParent, String orderBy) { 101 return getAllRelationshipRecords (relationship, contentlet, hasParent, false, orderBy); 102 } 103 104 public static List <Contentlet> getAllRelationshipRecordsByCondition(Relationship relationship, Contentlet contentlet, 105 boolean hasParent, boolean live, String condition) { 106 List <Contentlet> matches = new ArrayList <Contentlet>(); 107 List <Tree> trees = new ArrayList <Tree>(); 108 Identifier iden = IdentifierFactory.getIdentifierByInode(contentlet); 109 if(iden.getInode() == 0) { 110 Logger.warn(RelationshipFactory.class, "getAllRelationshipRecords: contentlet = " + contentlet.getInode() + 111 " doesn't have a valid identifier associated. "); 112 return new ArrayList <Contentlet>(); 113 } 114 115 116 if (hasParent) { 117 trees = TreeFactory.getTreesByParentAndRelationType(iden, relationship.getRelationTypeValue()); 118 for (Tree tree : trees) { 119 Identifier childIden = (Identifier) InodeFactory.getInode(tree.getChild(), Identifier.class); 120 Contentlet cont = null; 121 if (live){ 122 if(!UtilMethods.isSet(condition)) 123 cont = (Contentlet) IdentifierFactory.getLiveChildOfClass(childIden, Contentlet.class); 124 else 125 cont = (Contentlet) IdentifierFactory.getLiveChildOfClassByCondition(childIden, Contentlet.class,condition); 126 }else{ 127 if(!UtilMethods.isSet(condition)) 128 cont = (Contentlet) IdentifierFactory.getWorkingChildOfClass(childIden, Contentlet.class); 129 else 130 cont = (Contentlet) IdentifierFactory.getWorkingChildOfClassByCondition(childIden, Contentlet.class,condition); 131 } 132 if (cont != null && cont.getInode() > 0) { 133 matches.add(cont); 134 } 135 } 136 } else { 137 trees = TreeFactory.getTreesByChildAndRelationType(iden, relationship.getRelationTypeValue()); 138 for (Tree tree : trees) { 139 Identifier parentIden = (Identifier) InodeFactory.getInode(tree.getParent(), Identifier.class); 140 Contentlet cont = null; 141 if (live){ 142 if(!UtilMethods.isSet(condition)) 143 cont = (Contentlet) IdentifierFactory.getLiveChildOfClass(parentIden, Contentlet.class); 144 else 145 cont = (Contentlet) IdentifierFactory.getLiveChildOfClassByCondition(parentIden, Contentlet.class, condition); 146 }else{ 147 if(!UtilMethods.isSet(condition)) 148 cont = (Contentlet) IdentifierFactory.getWorkingChildOfClass(parentIden, Contentlet.class); 149 else 150 cont = (Contentlet) IdentifierFactory.getWorkingChildOfClassByCondition(parentIden, Contentlet.class,condition); 151 } 152 153 if (cont != null && cont.getInode() > 0) { 154 matches.add(cont); 155 } 156 } 157 } 158 return matches; 159 } 160 161 public static List <Tree> getAllRelationshipTrees(Relationship relationship, Contentlet contentlet, boolean hasParent) { 162 List <Tree> matches = new ArrayList <Tree>(); 163 List <Tree> trees = new ArrayList <Tree>(); 164 Identifier iden = IdentifierFactory.getIdentifierByInode(contentlet); 165 if (hasParent) { 166 trees = TreeFactory.getTreesByParentAndRelationType(iden, relationship.getRelationTypeValue()); 167 for (Tree tree : trees) { 168 matches.add(tree); 169 } 170 } else { 171 trees = TreeFactory.getTreesByChildAndRelationType(iden, relationship.getRelationTypeValue()); 172 for (Tree tree : trees) { 173 matches.add(tree); 174 } 175 } 176 return matches; 177 } 178 179 public static List <Contentlet> getAllRelationshipRecords(Relationship relationship, Contentlet contentlet, 180 boolean hasParent, boolean live, String orderBy) { 181 List <Contentlet> matches = new ArrayList <Contentlet>(); 182 long iden = IdentifierCache.getIdentifierByInodeFromCache(contentlet).getInode(); 183 184 if (hasParent) { 185 if (live) 186 matches = getRelatedContentByParent(iden, relationship.getRelationTypeValue(),"contentlet.live = " + DbConnectionFactory.getDBTrue(), orderBy); 187 else 188 matches = getRelatedContentByParent(iden, relationship.getRelationTypeValue(),"contentlet.working = " + DbConnectionFactory.getDBTrue(), orderBy); 189 190 191 } else { 192 if (live) 193 matches = getRelatedContentByChild(iden, relationship.getRelationTypeValue(),"contentlet.live = " + DbConnectionFactory.getDBTrue(), orderBy); 194 else 195 matches = getRelatedContentByChild(iden, relationship.getRelationTypeValue(),"contentlet.working = " + DbConnectionFactory.getDBTrue(), orderBy); 196 } 197 return matches; 198 } 199 200 201 public static boolean isParentOfTheRelationship(Relationship rel, Structure st) { 202 if (rel.getParentStructureInode() == st.getInode() && 203 !(rel.getParentRelationName().equals(rel.getChildRelationName()) && rel.getChildStructureInode() == rel.getParentStructureInode())) 204 return true; 205 return false; 206 } 207 208 public static boolean isChildOfTheRelationship(Relationship rel, Structure st) { 209 if (rel.getChildStructureInode() == st.getInode() && 210 !(rel.getParentRelationName().equals(rel.getChildRelationName()) && rel.getChildStructureInode() == rel.getParentStructureInode())) 211 return true; 212 return false; 213 } 214 215 public static boolean isASibblingRelationship(Relationship rel, Structure st) { 216 if (rel.getChildStructureInode() == rel.getParentStructureInode() && rel.getParentRelationName().equals(rel.getChildRelationName())) 217 return true; 218 return false; 219 } 220 221 public static void saveRelationship(Relationship relationship) { 223 InodeFactory.saveInode(relationship); 224 } 225 226 public static void deleteRelationship(long inode) { 228 Relationship relationship = getRelationshipByInode(inode); 229 deleteRelationship(relationship); 230 } 231 232 public static void deleteRelationship(Relationship relationship) { 233 InodeFactory.deleteInode(relationship); 234 } 235 236 public static List <Contentlet> getRelatedContentByParent(long parentInode, String relationType, String condition, String orderBy) { 237 try { 238 239 DotHibernate dh = new DotHibernate(Contentlet.class); 240 241 String sql = "SELECT {contentlet.*} from contentlet contentlet, inode contentlet_1_, tree tree1, tree tree2 " 242 + "where tree1.parent = ? and tree1.relation_type = ? and tree1.child = tree2.parent " 243 + "and tree2.child = contentlet.inode " 244 + "and contentlet.inode = contentlet_1_.inode " 245 + "and " + condition; 246 247 if (UtilMethods.isSet(orderBy)) { 248 sql = sql + " order by contentlet." + orderBy; 249 } 250 251 252 Logger.debug(RelationshipFactory.class, "sql: " + sql + "\n"); 253 Logger.debug(RelationshipFactory.class, "parentInode: " + parentInode + "\n"); 254 Logger.debug(RelationshipFactory.class, "relationType: " + relationType + "\n"); 255 dh.setSQLQuery(sql); 256 dh.setParam(parentInode); 257 dh.setParam(relationType); 258 259 return dh.list(); 260 } catch (Exception e) { 261 Logger.error(InodeFactory.class, "getChildrenClass failed:" + e, e); 262 throw new DotRuntimeException(e.toString()); 263 } 264 265 } 267 public static List <Contentlet> getRelatedContentByChild(long childInode, String relationType, String condition, String orderBy) { 268 try { 269 270 DotHibernate dh = new DotHibernate(Contentlet.class); 271 272 String sql = "SELECT {contentlet.*} from contentlet contentlet, inode contentlet_1_, tree tree1, tree tree2 " 273 + "where tree1.child = ? and tree1.relation_type = ? and tree1.parent = tree2.parent " 274 + "and tree2.child = contentlet.inode " 275 + "and contentlet.inode = contentlet_1_.inode " 276 + "and " + condition; 277 278 if (UtilMethods.isSet(orderBy)) { 279 sql = sql + " order by contentlet." + orderBy; 280 } 281 282 Logger.debug(RelationshipFactory.class, "sql: " + sql + "\n"); 283 Logger.debug(RelationshipFactory.class, "childInode: " + childInode + "\n"); 284 Logger.debug(RelationshipFactory.class, "relationType: " + relationType + "\n"); 285 dh.setSQLQuery(sql); 286 dh.setParam(childInode); 287 dh.setParam(relationType); 288 289 return dh.list(); 290 } catch (Exception e) { 291 Logger.error(InodeFactory.class, "getChildrenClass failed:" + e, e); 292 throw new DotRuntimeException(e.toString()); 293 } 294 295 } 297 298 299 } 300 | Popular Tags |