1 package com.dotmarketing.factories; 2 3 import java.util.HashSet ; 4 import java.util.List ; 5 import java.util.Set ; 6 7 import com.dotmarketing.beans.Inode; 8 import com.dotmarketing.beans.Tree; 9 import com.dotmarketing.db.DotConnect; 10 import com.dotmarketing.db.DotHibernate; 11 import com.dotmarketing.exception.DotRuntimeException; 12 import com.dotmarketing.portlets.events.model.Event; 13 import com.dotmarketing.util.Logger; 14 import com.dotmarketing.util.UtilMethods; 15 16 20 public class InodeFactory { 21 22 23 27 public static Object getChildOfClass(Inode inode, Class c) { 28 try { 29 String tableName = ((Inode) c.newInstance()).getType(); 30 DotHibernate dh = new DotHibernate(c); 31 String sql = "SELECT {" + tableName + ".*} from " + tableName + " " + tableName + ", tree tree, inode " 32 + tableName + "_1_ where tree.parent = ? and tree.child = " + tableName + ".inode and " + tableName 33 + "_1_.inode = " + tableName + ".inode"; 34 35 Logger.debug(InodeFactory.class, "dotHibernateSQL:getChildOfClass\n " + sql + "\n"); 36 37 Logger.debug(InodeFactory.class, "inode: " + inode.getInode() + "\n"); 38 39 dh.setSQLQuery(sql); 40 41 dh.setParam(inode.getInode()); 44 45 return dh.load(); 46 } catch (Exception e) { 47 Logger.error(InodeFactory.class, "getChildrenClass failed:" + e, e); 48 throw new DotRuntimeException(e.toString()); 49 } 50 51 55 } 56 57 public static Object getChildOfClassbyCondition(Inode inode, Class c, String condition) { 58 try { 59 String tableName = ((Inode) c.newInstance()).getType(); 60 DotHibernate dh = new DotHibernate(c); 61 String sql = "SELECT {" + tableName + ".*} from " + tableName + " " + tableName + ", tree tree, inode " 62 + tableName + "_1_ where tree.parent = ? and tree.child = " + tableName + ".inode and " + tableName 63 + "_1_.inode = " + tableName + ".inode and " + condition; 64 65 Logger.debug(InodeFactory.class, "dotHibernateSQL:getChildOfClassbyCondition\n " + sql + "\n"); 66 67 Logger.debug(InodeFactory.class, "inode: " + inode.getInode() + "\n"); 68 69 dh.setSQLQuery(sql); 70 71 dh.setParam(inode.getInode()); 74 75 return dh.load(); 76 } catch (Exception e) { 77 Logger.error(InodeFactory.class, "getChildrenClass failed:" + e, e); 78 throw new DotRuntimeException(e.toString()); 79 } 80 81 85 } 86 87 public static Object getChildOfClassByRelationTypeAndCondition(Inode inode, Class c, String relationType, 88 String condition) { 89 return getChildOfClassByRelationTypeAndCondition(inode.getInode(), c, relationType, condition); 90 } 91 92 public static Object getChildOfClassByRelationTypeAndCondition(long inode, Class c, String relationType, 93 String condition) { 94 try { 95 String tableName = ((Inode) c.newInstance()).getType(); 96 DotHibernate dh = new DotHibernate(c); 97 String sql = "SELECT {" + tableName + ".*} from " + tableName + " " + tableName + ", tree tree, inode " 98 + tableName + "_1_ where tree.parent = ? and tree.child = " + tableName + ".inode and " + tableName 99 + "_1_.inode = " + tableName + ".inode and " + condition; 100 101 Logger.debug(InodeFactory.class, "dotHibernateSQL:getChildOfClassbyCondition\n " + sql + "\n"); 102 103 dh.setSQLQuery(sql); 104 105 dh.setParam(inode); 108 109 Logger.debug(InodeFactory.class, "inode: " + inode + "\n"); 110 111 return dh.load(); 112 } catch (Exception e) { 113 Logger.error(InodeFactory.class, "getChildrenClass failed:" + e, e); 114 throw new DotRuntimeException(e.toString()); 115 } 116 117 121 } 122 123 127 public static Object getChildOfClassByRelationType(Inode p, Class c, String relationType) { 128 try { 129 String tableName = ((Inode) c.newInstance()).getType(); 130 DotHibernate dh = new DotHibernate(c); 131 132 String sql = "SELECT {" + tableName + ".*} from " + tableName + " " + tableName + ", tree tree, inode " 136 + tableName + "_1_ where tree.parent = ? and tree.child = " + tableName + ".inode and " + tableName 137 + "_1_.inode = " + tableName + ".inode and tree.relation_type = ?"; 138 139 Logger.debug(InodeFactory.class, "dotHibernateSQL:getChildOfClassByRelationType\n " + sql + "\n"); 140 141 dh.setSQLQuery(sql); 142 143 Logger.debug(InodeFactory.class, "inode: " + p.getInode() + "\n"); 144 145 dh.setParam(p.getInode()); 146 dh.setParam(relationType); 147 148 return dh.load(); 149 } catch (Exception e) { 150 Logger.error(InodeFactory.class, "getChildrenClass failed:" + e, e); 151 throw new DotRuntimeException(e.toString()); 152 } 153 154 158 } 159 160 public static java.util.List getChildrenClass(Inode p, Class c) { 161 return getChildrenClass(p, c, 0, 0); 162 } 163 164 public static java.util.List getChildrenClass(Inode p, Class c, int limit, int offset) { 165 try { 166 String tableName = ((Inode) c.newInstance()).getType(); 167 DotHibernate dh = new DotHibernate(c); 168 169 String sql = "SELECT {" + tableName + ".*} from " + tableName + " " + tableName + ", tree tree, inode " 170 + tableName + "_1_ where tree.parent = ? and tree.child = " + tableName + ".inode and " + tableName 171 + "_1_.inode = " + tableName + ".inode"; 172 173 Logger.debug(InodeFactory.class, "dotHibernateSQL:getChildrenClass\n " + sql + "\n"); 174 175 dh.setSQLQuery(sql); 176 177 Logger.debug(InodeFactory.class, "inode: " + p.getInode() + "\n"); 178 179 dh.setParam(p.getInode()); 180 181 if (limit != 0) { 182 dh.setFirstResult(offset); 183 dh.setMaxResults(limit); 184 } 185 186 return dh.list(); 187 } catch (Exception e) { 188 Logger.error(InodeFactory.class, "getChildrenClass failed:" + e, e); 189 throw new DotRuntimeException(e.toString()); 190 } 191 192 } 194 195 public static java.util.List getChildrenClass(Inode p, Class c, String orderBy) { 196 return getChildrenClass(p, c, orderBy, 0, 0); 197 } 198 199 public static java.util.List getChildrenClass(Inode p, Class c, String orderBy, int limit, int offset) { 200 try { 201 202 String tableName = ((Inode) c.newInstance()).getType(); 203 DotHibernate dh = new DotHibernate(c); 204 205 String sql = "SELECT {" + tableName + ".*} from " + tableName + " " + tableName + ", tree tree, inode " 206 + tableName + "_1_ where tree.parent = ? and tree.child = " + tableName + ".inode and " + tableName 207 + "_1_.inode = " + tableName + ".inode order by " + orderBy; 208 209 Logger.debug(InodeFactory.class, "dotHibernateSQL:getChildrenClass\n " + sql + "\n"); 210 211 dh.setSQLQuery(sql); 212 213 Logger.debug(InodeFactory.class, "inode: " + p.getInode() + "\n"); 214 215 dh.setParam(p.getInode()); 216 217 if (limit != 0) { 218 dh.setFirstResult(offset); 219 dh.setMaxResults(limit); 220 } 221 222 return dh.list(); 223 } catch (Exception e) { 224 Logger.error(InodeFactory.class, "getChildrenClass failed:" + e, e); 225 throw new DotRuntimeException(e.toString()); 226 } 227 228 } 230 231 public static java.util.List getChildrenClassByCondition(Inode p, Class c, String condition) { 232 return getChildrenClassByCondition(p, c, condition, 0, 0); 233 } 234 235 public static java.util.List getChildrenClassByCondition(Inode p, Class c, String condition, int limit, int offset) { 236 try { 237 String tableName = ((Inode) c.newInstance()).getType(); 238 DotHibernate dh = new DotHibernate(c); 239 240 String sql = "SELECT {" + tableName + ".*} from " + tableName + " " + tableName + ", tree tree, inode " 241 + tableName + "_1_ where tree.parent = ? and tree.child = " + tableName + ".inode and " + tableName 242 + "_1_.inode = " + tableName + ".inode and " + condition; 243 244 Logger.debug(InodeFactory.class, "dotHibernateSQL:getChildrenClassByCondition\n " + sql); 245 246 dh.setSQLQuery(sql); 247 248 Logger.debug(InodeFactory.class, "inode: " + p.getInode() + "\n"); 249 250 dh.setParam(p.getInode()); 251 252 if (limit != 0) { 253 dh.setFirstResult(offset); 254 dh.setMaxResults(limit); 255 } 256 257 return dh.list(); 258 } catch (Exception e) { 259 Logger.error(InodeFactory.class, "getChildrenClass failed:" + e, e); 260 throw new DotRuntimeException(e.toString()); 261 } 262 263 } 265 266 public static java.util.List getChildrenClassByOrder(Inode p, Class c, String order) { 267 return getChildrenClassByOrder(p, c, order, 0, 0); 268 } 269 270 public static java.util.List getChildrenClassByOrder(Inode p, Class c, String order, int limit, int offset) { 271 try { 272 String tableName = ((Inode) c.newInstance()).getType(); 273 DotHibernate dh = new DotHibernate(c); 274 275 String sql = "SELECT {" + tableName + ".*} from " + tableName + " " + tableName + ", tree tree, inode " 276 + tableName + "_1_ where tree.parent = ? and tree.child = " + tableName + ".inode and " + tableName 277 + "_1_.inode = " + tableName + ".inode order by " + order; 278 279 Logger.debug(InodeFactory.class, "dotHibernateSQL:getChildrenClassByOrder\n " + sql); 280 281 Logger.debug(InodeFactory.class, "inode: " + p.getInode() + "\n"); 282 283 Logger.debug(InodeFactory.class, "order: " + order + "\n"); 284 285 dh.setSQLQuery(sql); 286 287 dh.setParam(p.getInode()); 288 289 if (limit != 0) { 290 dh.setFirstResult(offset); 291 dh.setMaxResults(limit); 292 } 293 294 return dh.list(); 295 } catch (Exception e) { 296 Logger.error(InodeFactory.class, "getChildrenClassByOrder failed:" + e, e); 297 throw new DotRuntimeException(e.toString()); 298 } 299 300 } 302 303 public static java.util.List getChildrenClassOrderByRandom(Inode p, Class c) { 304 return getChildrenClassOrderByRandom(p, c, 0, 0); 305 } 306 307 public static java.util.List getChildrenClassOrderByRandom(Inode p, Class c, int limit, int offset) { 308 try { 309 String tableName = ((Inode) c.newInstance()).getType(); 310 DotHibernate dh = new DotHibernate(c); 311 312 String sql = "SELECT {" + tableName + ".*} from " + tableName + " " + tableName + ", tree tree, inode " 313 + tableName + "_1_ where tree.parent = ? and tree.child = " + tableName + ".inode and " + tableName 314 + "_1_.inode = " + tableName + ".inode order by rand()"; 315 316 Logger.debug(InodeFactory.class, "dotHibernateSQL:getChildrenClassByOrder\n " + sql); 317 318 Logger.debug(InodeFactory.class, "inode: " + p.getInode() + "\n"); 319 320 dh.setFirstResult(offset); 321 dh.setMaxResults(limit); 322 323 dh.setSQLQuery(sql); 324 325 dh.setParam(p.getInode()); 326 327 if (limit != 0) { 328 dh.setFirstResult(offset); 329 dh.setMaxResults(limit); 330 } 331 332 return dh.list(); 333 } catch (Exception e) { 334 Logger.error(InodeFactory.class, "getChildrenClassByOrder failed:" + e, e); 335 throw new DotRuntimeException(e.toString()); 336 } 337 338 } 340 341 public static java.util.List getChildrenClassByCondition(Inode p1, Inode p2, Class c, String condition, int limit, 342 int offset) { 343 return getChildrenClassByCondition(p1.getInode(), p2.getInode(), c, condition, limit, offset); 344 } 345 346 public static java.util.List getChildrenClassByCondition(Inode p1, Inode p2, Class c, String condition) { 347 return getChildrenClassByCondition(p1.getInode(), p2.getInode(), c, condition, 0, 0); 348 } 349 350 public static java.util.List getChildrenClassByCondition(long p1, long p2, Class c, String condition) { 351 return getChildrenClassByCondition(p1, p2, c, condition, 0, 0); 352 } 353 354 public static java.util.List getChildrenClassByCondition(long p1, long p2, Class c, String condition, int limit, 355 int offset) { 356 try { 357 String tableName = ((Inode) c.newInstance()).getType(); 358 DotHibernate dh = new DotHibernate(c); 359 360 String sql = "SELECT {" + tableName + ".*} from " + tableName + " " + tableName 361 + ", tree tree, tree tree2, inode " + tableName 362 + "_1_ where tree.parent = ? and tree2.parent = ? and tree.child = " + tableName 363 + ".inode and tree2.child = " + tableName + ".inode and " + tableName + "_1_.inode = " 364 + tableName + ".inode and " + condition; 365 366 Logger.debug(InodeFactory.class, "dotHibernateSQL:getChildrenClassByCondition\n " + sql + "\n"); 367 368 dh.setSQLQuery(sql); 369 370 Logger.debug(InodeFactory.class, "inode: " + p1 + "\n"); 371 Logger.debug(InodeFactory.class, "inode: " + p2 + "\n"); 372 373 dh.setParam(p1); 374 dh.setParam(p2); 375 376 if (limit != 0) { 377 dh.setFirstResult(offset); 378 dh.setMaxResults(limit); 379 } 380 381 return dh.list(); 382 } catch (Exception e) { 383 Logger.warn(InodeFactory.class, "getChildrenClassByCondition failed:" + e, e); 384 385 } 387 388 return new java.util.ArrayList (); 389 } 390 391 public static java.util.List getChildrenClassByConditionAndOrderBy(long[] inodes, Class c, String condition, 392 String orderBy) { 393 394 if (inodes == null || inodes.length == 0) { 395 return InodeFactory.getInodesOfClassByConditionAndOrderBy(c, condition, orderBy); 396 } 397 398 try { 399 400 String tableName = ((Inode) c.newInstance()).getType(); 401 DotHibernate dh = new DotHibernate(c); 402 403 StringBuffer sql = new StringBuffer ("SELECT {" + tableName + ".*} from " + tableName + " " + tableName); 404 405 for (int x = 1; x < inodes.length + 1; x++) { 406 if (x == 1) { 407 sql.append(", tree tree, "); 408 } else { 409 sql.append(", tree tree" + x + ", "); 410 } 411 } 412 sql.append(" inode " + tableName + "_1_ "); 413 sql.append(" where "); 414 415 sql.append(tableName + "_1_.inode = " + tableName + ".inode and "); 416 for (int x = 1; x < inodes.length + 1; x++) { 417 if (x == 1) { 418 sql.append(" ( tree.parent = ? and "); 419 sql.append(" tree.child = " + tableName + ".inode ) and "); 420 } else { 421 sql.append(" (tree" + x + ".parent = ? and "); 422 sql.append(" tree" + x + ".child = " + tableName + ".inode ) and "); 423 } 424 } 425 426 String query = sql.toString(); 427 query = query.substring(0, query.lastIndexOf("and")); 428 429 condition = (UtilMethods.isSet(condition) ? " and " + condition : ""); 431 orderBy = (UtilMethods.isSet(orderBy) ? " order by " + orderBy : ""); 433 434 query += condition + orderBy; 436 437 dh.setSQLQuery(query); 439 440 for (int x = 0; x < inodes.length; x++) { 441 dh.setParam(inodes[x]); 442 } 443 444 return dh.list(); 445 } catch (Exception e) { 446 Logger.debug(InodeFactory.class, "getChildrenClassByConditionAndOrderBy failed:" + e, e); 447 448 } 450 451 return new java.util.ArrayList (); 452 } 453 454 public static java.util.List getChildrenClassByConditionAndOrderBy(long p1, long p2, Class c, String condition, 455 String orderBy) { 456 457 return getChildrenClassByConditionAndOrderBy(p1, p2, c, condition, orderBy, 0, 0); 458 459 } 460 461 public static java.util.List getChildrenClassByConditionAndOrderBy(long p1, long p2, Class c, String condition, 462 String orderBy, int limit, int offset) { 463 try { 464 String tableName = ((Inode) c.newInstance()).getType(); 465 DotHibernate dh = new DotHibernate(c); 466 467 String sql = "SELECT {" + tableName + ".*} from " + tableName + " " + tableName 468 + ", tree tree, tree tree2, inode " + tableName 469 + "_1_ where tree.parent = ? and tree2.parent = ? and tree.child = " + tableName 470 + ".inode and tree2.child = " + tableName + ".inode and " + tableName + "_1_.inode = " 471 + tableName + ".inode and " + condition + " order by " + orderBy; 472 473 Logger.debug(InodeFactory.class, "dotHibernateSQL:getChildrenClassByCondition\n " + sql + "\n"); 474 475 dh.setSQLQuery(sql); 476 477 Logger.debug(InodeFactory.class, "inode 1: " + p1 + "\n"); 478 479 Logger.debug(InodeFactory.class, "inode 2: " + p2 + "\n"); 480 481 dh.setParam(p1); 482 dh.setParam(p2); 483 484 if (limit != 0) { 485 dh.setFirstResult(offset); 486 dh.setMaxResults(limit); 487 } 488 489 return dh.list(); 490 } catch (Exception e) { 491 Logger.warn(InodeFactory.class, "getChildrenClass failed:" + e, e); 492 493 } 495 496 return new java.util.ArrayList (); 497 } 498 499 public static java.util.List getChildrenClass(List inodes, Class c, String orderBy) { 500 return getChildrenClass(inodes, c, orderBy, 0, 0); 501 } 502 503 public static java.util.List getChildrenClass(List inodes, Class c, int limit, int offset) { 504 return getChildrenClassByConditionAndOrderBy(inodes, c, "", "", limit, offset); 505 } 506 507 public static java.util.List getChildrenClass(List inodes, Class c, String orderBy, int limit, int offset) { 508 return getChildrenClassByConditionAndOrderBy(inodes, c, "", orderBy, limit, offset); 509 } 510 511 public static java.util.List getChildrenClassByConditionAndOrderBy(List inodes, Class c, String condition, 512 String orderBy) { 513 514 return getChildrenClassByConditionAndOrderBy(inodes, c, condition, orderBy, 0, 0); 515 516 } 517 518 public static java.util.List getChildrenClassByConditionAndOrderBy(List inodes, Class c, String condition, 519 String orderBy, int limit, int offset) { 520 521 if (inodes == null || inodes.size() == 0) { 522 return InodeFactory.getInodesOfClassByConditionAndOrderBy(c, condition, orderBy); 523 524 } 525 526 try { 527 528 String tableName = ((Inode) c.newInstance()).getType(); 529 DotHibernate dh = new DotHibernate(c); 530 531 StringBuffer sql = new StringBuffer ("SELECT {" + tableName + ".*} from " + tableName + " " + tableName); 532 533 for (int x = 1; x < inodes.size() + 1; x++) { 534 if (x == 1) { 535 sql.append(", tree tree, "); 536 } else { 537 sql.append(", tree tree" + x + ", "); 538 } 539 } 540 sql.append(" inode " + tableName + "_1_ "); 541 sql.append(" where "); 542 543 sql.append(tableName + "_1_.inode = " + tableName + ".inode and "); 544 for (int x = 1; x < inodes.size() + 1; x++) { 545 if (x == 1) { 546 sql.append(" ( tree.parent = ? and "); 547 sql.append(" tree.child = " + tableName + ".inode ) "); 548 } else { 549 sql.append(" and (tree" + x + ".parent = ? and "); 550 sql.append(" tree" + x + ".child = " + tableName + ".inode ) "); 551 } 552 } 553 554 if (UtilMethods.isSet(condition)) { 556 sql.append(" and " + condition); 557 } 558 559 if (UtilMethods.isSet(orderBy)) { 561 sql.append(" order by "); 562 sql.append(orderBy); 563 } 564 565 dh.setSQLQuery(sql.toString()); 566 567 for (int x = 0; x < inodes.size(); x++) { 568 Inode i = (Inode) inodes.get(x); 569 dh.setParam(i.getInode()); 570 } 571 if (limit != 0) { 572 dh.setFirstResult(offset); 573 dh.setMaxResults(limit); 574 } 575 576 return dh.list(); 577 } catch (Exception e) { 578 Logger.debug(InodeFactory.class, "getChildrenClassByConditionAndOrderBy failed:" + e, e); 579 580 } 582 583 return new java.util.ArrayList (); 584 } 585 586 public static java.util.List getChildrenClassByCondition(List inodes, Class c, String condition) { 587 return getChildrenClassByCondition(inodes, c, condition, 0, 0); 588 589 } 590 591 public static java.util.List getChildrenClassByCondition(List inodes, Class c, String condition, int limit, 592 int offset) { 593 594 if (inodes == null || inodes.size() == 0) { 595 return InodeFactory.getInodesOfClassByCondition(Event.class, condition); 596 597 } 598 599 try { 600 601 String tableName = ((Inode) c.newInstance()).getType(); 602 DotHibernate dh = new DotHibernate(c); 603 604 StringBuffer sql = new StringBuffer ("SELECT {" + tableName + ".*} from " + tableName + " " + tableName); 605 606 for (int x = 1; x < inodes.size() + 1; x++) { 607 if (x == 1) { 608 sql.append(", tree tree, "); 609 } else { 610 sql.append(", tree tree" + x + ", "); 611 } 612 } 613 sql.append(" inode " + tableName + "_1_ "); 614 sql.append(" where "); 615 616 sql.append(tableName + "_1_.inode = " + tableName + ".inode and "); 617 for (int x = 1; x < inodes.size() + 1; x++) { 618 if (x == 1) { 619 sql.append(" ( tree.parent = ? and "); 620 sql.append(" tree.child = " + tableName + ".inode ) and "); 621 } else { 622 sql.append(" (tree" + x + ".parent = ? and "); 623 sql.append(" tree" + x + ".child = " + tableName + ".inode ) and "); 624 } 625 } 626 627 sql.append(condition); 628 dh.setSQLQuery(sql.toString()); 629 630 for (int x = 0; x < inodes.size(); x++) { 631 Inode i = (Inode) inodes.get(x); 632 dh.setParam(i.getInode()); 633 } 634 if (limit != 0) { 635 dh.setFirstResult(offset); 636 dh.setMaxResults(limit); 637 } 638 639 return dh.list(); 640 } catch (Exception e) { 641 Logger.debug(InodeFactory.class, "getChildrenClassByConditionAndOrderBy failed:" + e, e); 642 643 } 645 646 return new java.util.ArrayList (); 647 } 648 649 public static java.util.List getChildrenClassByConditionAndOrderBy(Inode p, Class c, String condition, 650 String orderby) { 651 652 return getChildrenClassByConditionAndOrderBy(p.getInode(), c, condition, orderby, 0, 0); 653 } 654 655 public static java.util.List getChildrenClassByConditionAndOrderBy(Inode p, Class c, String condition, 656 String orderby, int limit, int offset) { 657 658 return getChildrenClassByConditionAndOrderBy(p.getInode(), c, condition, orderby, limit, offset); 659 } 660 661 public static java.util.List getChildrenClassByConditionAndOrderBy(long p, Class c, String condition, String orderby) { 662 663 return getChildrenClassByConditionAndOrderBy(p, c, condition, orderby, 0, 0); 664 } 665 666 public static java.util.List getChildrenClassByConditionAndOrderBy(long p, Class c, String condition, 667 String orderby, int limit, int offset) { 668 try { 669 String tableName = ((Inode) c.newInstance()).getType(); 670 DotHibernate dh = new DotHibernate(c); 671 672 String sql = "SELECT {" + tableName + ".*} from " + tableName + " " + tableName + ", tree tree, inode " 673 + tableName + "_1_ where tree.parent = ? and tree.child = " + tableName + ".inode and " + tableName 674 + "_1_.inode = " + tableName + ".inode and " + condition + " order by " + orderby; 675 676 if(!UtilMethods.isSet(orderby)) 677 sql += tableName + ".inode desc"; 678 679 681 Logger.debug(InodeFactory.class, "dotHibernateSQL:getChildrenClassByConditionAndOrderBy\n " + sql + "\n"); 682 683 dh.setSQLQuery(sql); 684 685 Logger.debug(InodeFactory.class, "inode: " + p + "\n"); 686 687 dh.setParam(p); 688 689 if (limit != 0) { 690 dh.setFirstResult(offset); 691 dh.setMaxResults(limit); 692 } 693 694 return dh.list(); 695 } catch (Exception e) { 696 Logger.warn(InodeFactory.class, "getChildrenClassByConditionAndOrderBy failed:" + e, e); 697 698 } 700 701 return new java.util.ArrayList (); 702 } 703 704 public static Inode getInode(String x, Class c) { 705 try { 706 return (Inode) new DotHibernate(c).load(Long.parseLong(x)); 707 } catch (Exception e) { 708 return (Inode) new DotHibernate(c).load(x); 709 } 710 } 711 712 public static Inode getInode(long x, Class c) { 713 return (Inode) new DotHibernate(c).load(x); 714 } 715 716 public static java.util.List getInodesOfClass(Class c) { 717 return getInodesOfClass(c, 0, 0); 718 } 719 720 public static java.util.List getInodesOfClass(Class c, int limit, int offset) { 721 try { 722 DotHibernate dh = new DotHibernate(c); 723 dh.setQuery("from inode in class " + c.getName()); 724 725 if (limit != 0) { 726 dh.setFirstResult(offset); 727 dh.setMaxResults(limit); 728 } 729 730 return dh.list(); 731 } catch (Exception e) { 732 Logger.warn(InodeFactory.class, "getObjectsOfClass failed:" + e, e); 733 } 735 736 return new java.util.ArrayList (); 737 } 738 739 public static java.util.List getInodesOfClassByCondition(Class c, String condition) { 740 return getInodesOfClassByCondition(c, condition, 0, 0); 741 } 742 743 public static java.util.List getInodesOfClassByCondition(Class c, String condition, int limit, int offset) { 744 try { 745 DotHibernate dh = new DotHibernate(c); 746 dh.setQuery("from inode in class " + c.getName() + " where " + condition); 747 748 if (limit != 0) { 749 dh.setFirstResult(offset); 750 dh.setMaxResults(limit); 751 } 752 753 return dh.list(); 754 } catch (Exception e) { 755 Logger.warn(InodeFactory.class, "getInodesOfClassByCondition(Class c, String condition) failed:" + e, e); 756 757 } 759 760 return new java.util.ArrayList (); 761 } 762 763 public static Object getInodeOfClassByCondition(Class c, String condition) { 764 try { 765 DotHibernate dh = new DotHibernate(c); 766 dh.setQuery("from inode in class " + c.getName() + " where " + condition); 767 Logger.debug(InodeFactory.class, "getInodeOfClassByCondition query: " + dh.getQuery()); 768 return dh.load(); 769 } catch (Exception e) { 770 Logger.warn(InodeFactory.class, "getInodeOfClassByCondition(Class c, String condition) failed:" + e, e); 771 772 } 774 775 try { 776 return c.newInstance(); 777 } catch (Exception e) { 778 throw new DotRuntimeException(e.toString()); 779 } 780 } 781 782 public static java.util.List getInodesOfClassByCondition(Class c, String condition, String orderby) { 783 return getInodesOfClassByConditionAndOrderBy(c, condition, orderby, 0, 0); 784 } 785 786 public static java.util.List getInodesOfClassByConditionAndOrderBy(Class c, String condition, String orderby) { 787 return getInodesOfClassByConditionAndOrderBy(c, condition, orderby, 0, 0); 788 } 789 790 public static java.util.List getInodesOfClassByConditionAndOrderBy(Class c, String condition, String orderby, 791 int limit, int offset) { 792 try { 793 DotHibernate dh = new DotHibernate(c); 794 dh.setQuery("from inode in class " + c.getName() + " where " + condition + " order by " + orderby); 795 796 if (limit != 0) { 797 dh.setFirstResult(offset); 798 dh.setMaxResults(limit); 799 } 800 801 return dh.list(); 802 } catch (Exception e) { 803 Logger.warn(InodeFactory.class, 804 "getInodesOfClassByCondition(Class c, String condition, String orderby) failed:" + e, e); 805 } 807 808 return new java.util.ArrayList (); 809 } 810 811 public static java.util.List getInodesOfClass(Class c, int maxRows) { 812 try { 813 DotHibernate dh = new DotHibernate(c); 814 dh.setMaxResults(maxRows); 815 dh.setQuery("from inode in class " + c.getName()); 816 817 return dh.list(); 818 } catch (Exception e) { 819 Logger.warn(InodeFactory.class, "getObjectsOfClass failed:" + e, e); 820 821 } 823 824 return new java.util.ArrayList (); 825 } 826 827 public static java.util.List getInodesOfClass(Class c, String orderBy) { 828 return getInodesOfClass(c, orderBy, 0, 0); 829 } 830 831 public static java.util.List getInodesOfClass(Class c, String orderBy, int limit, int offset) { 832 try { 833 DotHibernate dh = new DotHibernate(c); 834 dh.setQuery("from inode in class " + c.getName() + " order by " + orderBy); 835 836 if (limit != 0) { 837 dh.setFirstResult(offset); 838 dh.setMaxResults(limit); 839 } 840 841 return dh.list(); 842 } catch (Exception e) { 843 Logger.warn(InodeFactory.class, "getInodesOfClass failed:" + e, e); 844 } 845 846 return new java.util.ArrayList (); 847 } 848 849 public static Object getObject(long x, Class c) { 850 return new DotHibernate(c).load(x); 851 } 852 853 public static Object getParentOfClass(Inode i, Class c) { 854 return getParentOfClass(String.valueOf(i.getInode()), c); 855 } 856 857 public static Object getParentOfClass(String i, Class c) { 858 try { 859 String tableName = ((Inode) c.newInstance()).getType(); 860 DotHibernate dh = new DotHibernate(c); 861 String sql = "SELECT {" + tableName + ".*} from " + tableName + " " + tableName + ", tree tree, inode " 862 + tableName + "_1_ where tree.child = ? and tree.parent = " + tableName + ".inode and " + tableName 863 + "_1_.inode = " + tableName + ".inode "; 864 865 Logger.debug(InodeFactory.class, "dotHibernateSQL:getParentOfClass:\n " + sql + "\n"); 866 867 dh.setSQLQuery(sql); 868 dh.setParam(i); 869 870 Logger.debug(InodeFactory.class, "inode: " + i + "\n"); 871 872 List list = dh.list(); 873 874 if ((list != null) && (list.size() != 0)) { 875 return list.get(0); 876 } 877 } catch (Exception e) { 878 try { 879 Logger.warn(InodeFactory.class, "getParentOfClass failed:" + e, e); 880 881 return c.newInstance(); 882 } catch (Exception ex) { 883 Logger.warn(InodeFactory.class, "getParentOfClass failed:" + e, e); 884 885 } 887 } 888 889 try { 890 return c.newInstance(); 891 } catch (Exception e) { 892 return new Object (); 893 } 894 } 895 896 public static java.util.List getParentsOfClassNoLock(Inode p, Class c) { 897 try { 898 String tableName = ((Inode) c.newInstance()).getType(); 899 DotHibernate dh = new DotHibernate(c); 900 String sql = "SELECT {" + tableName + ".*} from " + tableName + " " + tableName 901 + " with (nolock), tree tree with (nolock), inode " + tableName + "_1_ with (nolock) where tree.child = ? and tree.parent = " 902 + tableName + ".inode and " + tableName + "_1_.inode = " + tableName + ".inode "; 903 904 Logger.debug(InodeFactory.class, "dotHibernateSQL:getParentOfClass:\n " + sql + "\n"); 905 906 dh.setSQLQuery(sql); 907 908 dh.setParam(p.getInode()); 911 912 Logger.debug(InodeFactory.class, "inode: " + p.getInode() + "\n"); 913 914 return dh.list(); 915 } catch (Exception e) { 916 Logger.warn(InodeFactory.class, "getParentsOfClass failed:" + e, e); 917 918 } 920 921 return new java.util.ArrayList (); 922 } 923 924 public static Object getParentOfClassByRelationType(Inode p, Class c, String relationType) { 925 try { 926 String tableName = ((Inode) c.newInstance()).getType(); 927 DotHibernate dh = new DotHibernate(c); 928 929 String sql = "SELECT {" + tableName + ".*} from " + tableName + " " + tableName + ", tree tree, inode " 933 + tableName + "_1_ where tree.child = ? and tree.parent = " + tableName + ".inode and " + tableName 934 + "_1_.inode = " + tableName + ".inode and tree.relation_type = ?"; 935 936 Logger.debug(InodeFactory.class, "dotHibernateSQL:getChildOfClassByRelationType\n " + sql + "\n"); 937 938 dh.setSQLQuery(sql); 939 940 Logger.debug(InodeFactory.class, "inode: " + p.getInode() + "\n"); 941 942 dh.setParam(p.getInode()); 943 dh.setParam(relationType); 944 945 return dh.load(); 946 } catch (Exception e) { 947 Logger.error(InodeFactory.class, "getChildrenClass failed:" + e, e); 948 throw new DotRuntimeException(e.toString()); 949 } 950 } 951 952 public static java.util.List getParentsOfClassByCondition(Inode p, Class c, String condition) { 953 try { 954 String tableName = ((Inode) c.newInstance()).getType(); 955 DotHibernate dh = new DotHibernate(c); 956 957 String sql = "SELECT {" + tableName + ".*} from " + tableName + " " + tableName + ", tree tree, inode " 958 + tableName + "_1_ where tree.child = ? and tree.parent = " + tableName + ".inode and " + tableName 959 + "_1_.inode = " + tableName + ".inode and " + condition; 960 961 Logger.debug(InodeFactory.class, "dotHibernateSQL:getParentsOfClassByCondition\n " + sql); 962 963 Logger.debug(InodeFactory.class, "inode: " + p.getInode() + "\n"); 964 965 Logger.debug(InodeFactory.class, "condition: " + condition + "\n"); 966 967 dh.setSQLQuery(sql); 968 dh.setParam(p.getInode()); 969 970 return dh.list(); 971 } catch (Exception e) { 972 Logger.warn(InodeFactory.class, "getParentsOfClassByCondition failed:" + e, e); 973 974 } 976 977 return new java.util.ArrayList (); 978 } 979 980 public static java.util.List getParentsOfClass(Inode p, Class c) { 981 return getParentsOfClass( p, c, ""); 982 } 983 public static java.util.List getParentsOfClass(Inode p, Class c, String sortOrder) { 984 try { 985 String tableName = ((Inode) c.newInstance()).getType(); 986 DotHibernate dh = new DotHibernate(c); 987 String sql = "SELECT {" + tableName + ".*} from " + tableName + " " + tableName + ", tree tree, inode " 988 + tableName + "_1_ where tree.child = ? and tree.parent = " + tableName + ".inode and " + tableName 989 + "_1_.inode = " + tableName + ".inode "; 990 991 if(UtilMethods.isSet(sortOrder)){ 992 sql = sql + " order by "+ sortOrder; 993 } 994 Logger.debug(InodeFactory.class, "dotHibernateSQL:getParentOfClass:\n " + sql + "\n"); 995 996 dh.setSQLQuery(sql); 997 998 dh.setParam(p.getInode()); 1001 1002 Logger.debug(InodeFactory.class, "inode: " + p.getInode() + "\n"); 1003 1004 return dh.list(); 1005 } catch (Exception e) { 1006 Logger.warn(InodeFactory.class, "getParentsOfClass failed:" + e, e); 1007 1008 } 1010 1011 return new java.util.ArrayList (); 1012 } 1013 1014 public static Object getParentOfClassByCondition(Inode inode, Class c, String condition) { 1015 try { 1016 String tableName = ((Inode) c.newInstance()).getType(); 1017 DotHibernate dh = new DotHibernate(c); 1018 String sql = "SELECT {" + tableName + ".*} from " + tableName + " " + tableName + ", tree tree, inode " 1019 + tableName + "_1_ where tree.child = ? and tree.parent = " + tableName + ".inode and " + tableName 1020 + "_1_.inode = " + tableName + ".inode and " + condition; 1021 1022 Logger.debug(InodeFactory.class, "dotHibernateSQL:getParentOfClassbyCondition\n " + sql + "\n"); 1023 1024 Logger.debug(InodeFactory.class, "inode: " + inode.getInode() + "\n"); 1025 1026 dh.setSQLQuery(sql); 1027 dh.setParam(inode.getInode()); 1028 1029 return dh.load(); 1030 } catch (Exception e) { 1031 Logger.warn(InodeFactory.class, "getParentOfClassbyCondition failed:" + e, e); 1032 throw new DotRuntimeException(e.toString()); 1033 } 1034 1035 1039 } 1040 1041 public static void deleteInode(Object o) { 1042 Inode inode = (Inode) o; 1043 1044 try { 1045 1046 DotHibernate.delete(o); 1047 1048 DotConnect db = new DotConnect(); 1051 db.setSQL("delete from tree where child = ? or parent =?"); 1052 db.addParam(inode.getInode()); 1053 db.addParam(inode.getInode()); 1054 db.getResult(); 1055 1056 db.setSQL("delete from multi_tree where child = ? or parent1 =? or parent2 = ?"); 1059 db.addParam(inode.getInode()); 1060 db.addParam(inode.getInode()); 1061 db.addParam(inode.getInode()); 1062 db.getResult(); 1063 1064 } catch (Exception e) { 1065 throw new DotRuntimeException(e.getMessage()); 1066 } 1067 } 1068 1069 public static void deleteChildrenOfClass(Inode parent, Class c) { 1070 java.util.List children = getChildrenClass(parent, c); 1071 java.util.Iterator childrenIter = children.iterator(); 1072 1073 while (childrenIter.hasNext()) { 1074 parent.deleteChild((Inode) childrenIter.next()); 1075 } 1076 } 1077 1078 public static void deleteChildrenOfClassByRelationType(Inode parent, Class c,String relationType) { 1079 java.util.List children = getChildrenClass(parent, c); 1080 java.util.Iterator childrenIter = children.iterator(); 1081 1082 while (childrenIter.hasNext()) { 1083 parent.deleteChild((Inode) childrenIter.next()); 1084 } 1085 } 1086 1087 public static void saveInode(Inode o) { 1088 1089 DotHibernate.saveOrUpdate(o); 1090 } 1091 1092 public static int countChildrenOfClass(Inode i, Class c) { 1093 return countChildrenOfClass(i, c, 0, 5); 1094 } 1095 1096 public static int countChildrenOfClass(Inode i, Class c, int limit, int offset) { 1097 try { 1098 1099 String tableName = ((Inode) c.newInstance()).getType(); 1100 DotConnect db = new DotConnect(); 1101 db 1102 .setSQL("select count(*) as test from inode, tree where inode.inode = tree.child and tree.parent = ? and inode.type = '" 1103 + tableName + "' "); 1104 db.addParam(i.getInode()); 1105 return db.getInt("test"); 1107 } catch (Exception e) { 1108 Logger.warn(InodeFactory.class, "countChildrenOfClass failed:" + e, e); 1109 throw new DotRuntimeException(e.toString()); 1110 } 1112 } 1113 1114 public static List getChildrenOfClassByRelationType(Inode p, Class c, String relationType) { 1115 try { 1116 String tableName = ((Inode) c.newInstance()).getType(); 1117 DotHibernate dh = new DotHibernate(c); 1118 1119 String sql = "SELECT {" + tableName + ".*} from " + tableName + " " + tableName + ", tree tree, inode " 1123 + tableName + "_1_ where tree.parent = ? and tree.child = " + tableName + ".inode and " + tableName 1124 + "_1_.inode = " + tableName + ".inode and tree.relation_type = ?"; 1125 1126 Logger.debug(InodeFactory.class, "dotHibernateSQL:getChildOfClassByRelationType\n " + sql + "\n"); 1127 1128 dh.setSQLQuery(sql); 1129 1130 Logger.debug(InodeFactory.class, "inode: " + p.getInode() + "\n"); 1131 1132 dh.setParam(p.getInode()); 1133 dh.setParam(relationType); 1134 1135 return dh.list(); 1136 } catch (Exception e) { 1137 Logger.error(InodeFactory.class, "getChildrenClass failed:" + e, e); 1138 throw new DotRuntimeException(e.toString()); 1139 } 1140 1141 1145 } 1146 1147 public static List getChildrenOfClassByRelationTypeAndCondition(Inode inode, Class c, String relationType, 1148 String condition) { 1149 return getChildrenOfClassByRelationTypeAndCondition(inode.getInode(), c, relationType, condition); 1150 } 1151 1152 public static List getChildrenOfClassByRelationTypeAndCondition(long inode, Class c, String relationType, 1153 String condition) { 1154 try { 1155 String tableName = ((Inode) c.newInstance()).getType(); 1156 DotHibernate dh = new DotHibernate(c); 1157 String sql = "SELECT {" + tableName + ".*} from " + tableName + " " + tableName + ", tree tree, inode " 1158 + tableName + "_1_ where tree.parent = ? and tree.child = " + tableName + ".inode and " + tableName 1159 + "_1_.inode = " + tableName + ".inode and " + condition; 1160 1161 Logger.debug(InodeFactory.class, "dotHibernateSQL:getChildOfClassbyCondition\n " + sql + "\n"); 1162 1163 dh.setSQLQuery(sql); 1164 1165 dh.setParam(inode); 1168 1169 Logger.debug(InodeFactory.class, "inode: " + inode + "\n"); 1170 1171 return dh.list(); 1172 } catch (Exception e) { 1173 Logger.error(InodeFactory.class, "getChildrenClass failed:" + e, e); 1174 throw new DotRuntimeException(e.toString()); 1175 } 1176 1177 1181 } 1182 1183 public static java.util.List getInodesOfClassByConditionAndOrderBy(Class c, String condition, String orderby, 1184 int limit) { 1185 try { 1186 DotHibernate dh = new DotHibernate(c); 1187 String query = "from inode in class " + c.getName(); 1188 query += (UtilMethods.isSet(condition) ? " where " + condition : ""); 1190 query += (UtilMethods.isSet(orderby) ? " order by upper(" + orderby + ") " : ""); 1192 if (limit != 0) { 1194 dh.setMaxResults(limit); 1195 } 1196 1197 dh.setQuery(query); 1198 return dh.list(); 1199 } catch (Exception e) { 1200 Logger.warn(InodeFactory.class, 1201 "getInodesOfClassByCondition(Class c, String condition, String orderby) failed:" + e, e); 1202 } 1204 1205 return new java.util.ArrayList (); 1206 } 1207 1208 public static java.util.List getChildrenClassByRelationType(Inode p, Class c,String relationType) { 1209 try { 1210 String tableName = ((Inode) c.newInstance()).getType(); 1211 DotHibernate dh = new DotHibernate(c); 1212 1213 String sql = "SELECT {" + tableName + ".*} from " + tableName + " " + tableName 1214 + ", tree tree, inode " + tableName + "_1_ where tree.parent = ? and tree.child = " 1215 + tableName + ".inode and " + tableName + "_1_.inode = " + tableName + ".inode and tree.relation_type = '" + relationType + "'"; 1216 1217 Logger.debug(InodeFactory.class, "dotHibernateSQL:getChildrenClass\n " + sql + "\n"); 1218 1219 dh.setSQLQuery(sql); 1220 1221 Logger.debug(InodeFactory.class, "inode: " + p.getInode() + "\n"); 1222 1223 dh.setParam(p.getInode()); 1224 return dh.list(); 1225 } catch (Exception e) { 1226 Logger.error(InodeFactory.class, "getChildrenClass failed:" + e, e); 1227 throw new DotRuntimeException(e.toString()); 1228 } 1229 1230 } 1232 1233 public static java.util.List getParentsOfClassByRelationType(Inode p, Class c, String relationType) { 1234 try { 1235 String tableName = ((Inode) c.newInstance()).getType(); 1236 DotHibernate dh = new DotHibernate(c); 1237 1238 String sql = "SELECT {" + tableName + ".*} from " + tableName + " " + tableName 1239 + ", tree tree, inode " + tableName + "_1_ where tree.child = ? and tree.parent = " 1240 + tableName + ".inode and " + tableName + "_1_.inode = " + tableName + ".inode and tree.relation_type = ?"; 1241 1242 Logger.debug(InodeFactory.class, "dotHibernateSQL:getParentsOfClassByCondition\n " + sql); 1243 1244 Logger.debug(InodeFactory.class, "inode: " + p.getInode() + "\n"); 1245 1246 Logger.debug(InodeFactory.class, "relation: " + relationType + "\n"); 1247 1248 dh.setSQLQuery(sql); 1249 dh.setParam(p.getInode()); 1250 dh.setParam(relationType); 1251 1252 return dh.list(); 1253 } catch (Exception e) { 1254 Logger.warn(InodeFactory.class, "getParentsOfClassByCondition failed:" + e, e); 1255 1256 } 1258 1259 return new java.util.ArrayList (); 1260 } 1261 1262 public static java.util.List getChildrenClassByConditionAndOrderBy(long[] inodes, Class c, String condition, 1263 String orderBy,int quantity) { 1264 1265 if (inodes == null || inodes.length == 0) 1266 { 1267 return InodeFactory.getInodesOfClassByConditionAndOrderBy(c, condition, orderBy,quantity); 1268 } 1269 1270 try { 1271 1272 String tableName = ((Inode) c.newInstance()).getType(); 1273 DotHibernate dh = new DotHibernate(c); 1274 1275 StringBuffer sql = new StringBuffer ("SELECT {" + tableName + ".*} from " + tableName + " " + tableName); 1276 1277 for (int x = 1; x < inodes.length + 1; x++) { 1278 if (x == 1) { 1279 sql.append(", tree tree "); 1280 } else { 1281 sql.append(", tree tree" + x + " "); 1282 } 1283 } 1284 sql.append(", inode " + tableName + "_1_ "); 1285 sql.append(" where "); 1286 1287 sql.append(tableName + "_1_.inode = " + tableName + ".inode and "); 1288 for (int x = 1; x < inodes.length + 1; x++) { 1289 if (x == 1) { 1290 sql.append(" ( tree.parent = ? and "); 1291 sql.append(" tree.child = " + tableName + ".inode ) and "); 1292 } else { 1293 sql.append(" (tree" + x + ".parent = ? and "); 1294 sql.append(" tree" + x + ".child = " + tableName + ".inode ) and "); 1295 } 1296 } 1297 1298 String query = sql.toString(); 1299 query = query.substring(0,query.lastIndexOf("and")); 1300 1301 condition = (UtilMethods.isSet(condition) ? " and " + condition : ""); 1303 orderBy = (UtilMethods.isSet(orderBy) ? " order by " + orderBy : ""); 1305 1306 query += condition + orderBy; 1308 1309 if(quantity >= 0) 1311 { 1312 dh.setFirstResult(0); 1313 dh.setMaxResults(quantity); 1314 } 1315 dh.setSQLQuery(query); 1316 1317 for (int x = 0; x < inodes.length; x++) { 1318 dh.setParam(inodes[x]); 1319 } 1320 1321 return dh.list(); 1322 } catch (Exception e) { 1323 Logger.debug(InodeFactory.class, "getChildrenClassByConditionAndOrderBy failed:" + e, e); 1324 1325 } 1327 1328 return new java.util.ArrayList (); 1329 } 1330} 1331 | Popular Tags |