1 package com.dotmarketing.viewtools; 2 3 import java.util.ArrayList ; 4 import java.util.Collections ; 5 import java.util.HashMap ; 6 import java.util.HashSet ; 7 import java.util.List ; 8 import java.util.Map ; 9 import java.util.Set ; 10 11 import org.apache.commons.beanutils.PropertyUtils; 12 13 import com.dotmarketing.lucene.DotAnalyzer; 15 16 import org.apache.lucene.queryParser.QueryParser; 17 import org.apache.lucene.search.Query; 18 import org.apache.velocity.tools.view.tools.ViewTool; 19 20 import com.dotmarketing.beans.Identifier; 21 import com.dotmarketing.cache.FieldsCache; 22 import com.dotmarketing.cache.StructureCache; 23 import com.dotmarketing.factories.IdentifierFactory; 24 import com.dotmarketing.factories.InodeFactory; 25 import com.dotmarketing.portlets.categories.factories.CategoryFactory; 26 import com.dotmarketing.portlets.categories.model.Category; 27 import com.dotmarketing.portlets.contentlet.factories.ContentletFactory; 28 import com.dotmarketing.portlets.contentlet.model.Contentlet; 29 import com.dotmarketing.portlets.structure.factories.RelationshipFactory; 30 import com.dotmarketing.portlets.structure.model.Field; 31 import com.dotmarketing.portlets.structure.model.Relationship; 32 import com.dotmarketing.portlets.structure.model.Structure; 33 import com.dotmarketing.util.Config; 34 import com.dotmarketing.util.Logger; 35 import com.dotmarketing.util.LuceneHits; 36 import com.dotmarketing.util.LuceneUtils; 37 38 public class ContentsWebAPI implements ViewTool { 39 40 42 public void init(Object obj) { 43 } 46 47 53 public Identifier getContentIdentifier(Contentlet cont) { 54 return (Identifier) IdentifierFactory.getIdentifierByInode(cont); 55 } 56 57 63 public Contentlet getContentByInode(long inode) { 64 return (Contentlet) InodeFactory.getInode(inode, Contentlet.class); 65 } 66 67 73 public Contentlet getContentByInode(String inode) { 74 return (Contentlet) InodeFactory.getInode(inode, Contentlet.class); 75 } 76 77 83 public Structure getStructureByType(String structureType) { 84 return StructureCache.getStructureByType(structureType); 85 } 86 87 95 public List <Contentlet> getLastestContents(Structure structure, Category category, int maxResults) { 96 return ContentletFactory.getContentletsByStuctureAndCategory(structure, category, "mod_date desc", maxResults); 97 } 98 99 107 public List <Contentlet> getLastestContents(String structureType, String categoryName, int maxResults) { 108 Category category = CategoryFactory.getCategoryByName(categoryName); 109 Structure structure = StructureCache.getStructureByType(structureType); 110 return ContentletFactory.getContentletsByStuctureAndCategory(structure, category, "mod_date desc", maxResults); 111 } 112 113 120 public List <Contentlet> getLastestContents(Structure structure, Category category) { 121 return ContentletFactory.getContentletsByStuctureAndCategory(structure, category, "mod_date desc"); 122 } 123 124 131 public List <Contentlet> getLastestContents(String structureType, String categoryName) { 132 Category category = CategoryFactory.getCategoryByName(categoryName); 133 Structure structure = StructureCache.getStructureByType(structureType); 134 return ContentletFactory.getContentletsByStuctureAndCategory(structure, category, "mod_date desc"); 135 } 136 137 145 public Field getFieldByName(Structure st, String fieldName) { 146 List <Field> fields = FieldsCache.getFieldsByStructureInode(st.getInode()); 147 for (Field f : fields) { 148 if (f.getFieldName().equals(fieldName)) 149 return f; 150 } 151 return new Field(); 152 } 153 154 162 public Field getFieldByName(String structureType, String fieldName) { 163 Structure st = StructureCache.getStructureByType(structureType); 164 return getFieldByName(st, fieldName); 165 } 166 167 175 public Field getFieldByName(long structureInode, String fieldName) { 176 Structure st = StructureCache.getStructureByInode(structureInode); 177 return getFieldByName(st, fieldName); 178 } 179 180 186 public Field getFieldByLogicalName(Structure st, String fieldName) { 187 List <Field> fields = FieldsCache.getFieldsByStructureInode(st.getInode()); 188 for (Field f : fields) { 189 if (f.getFieldContentlet().equals(fieldName)) 190 return f; 191 } 192 return new Field(); 193 } 194 195 201 public Field getFieldByLogicalName(String structureType, String fieldName) { 202 Structure st = StructureCache.getStructureByType(structureType); 203 return getFieldByLogicalName(st, fieldName); 204 } 205 206 212 public Field getFieldByLogicalName(long structureInode, String fieldName) { 213 Structure st = StructureCache.getStructureByInode(structureInode); 214 return getFieldByLogicalName(st, fieldName); 215 } 216 217 224 public Object getFieldValue(String fieldName, Contentlet content) { 225 Structure structure = StructureCache.getStructureByInode(content.getStructureInode()); 226 Field theField = null; 227 List <Field> fields = FieldsCache.getFieldsByStructureInode(structure.getInode()); 228 for (Field field : fields) { 229 if (field.getFieldName().equals(fieldName)) { 230 theField = field; 231 break; 232 } 233 } 234 if (theField == null) 235 return null; 236 try { 237 return PropertyUtils.getProperty(content, theField.getFieldContentlet()); 238 } catch (Exception e) { 239 Logger.error(ContentletFactory.class, e.getMessage(), e); 240 return null; 241 } 242 } 243 244 245 254 public List <Contentlet> getContentsByStructureAndFieldValue(Structure structure, String fieldName, String fieldValue) { 255 return ContentletFactory.getContentletsByStuctureAndFieldValue(structure, fieldName, fieldValue, "mod_date", 0); 256 } 257 258 268 public List <Contentlet> getContentletsByStructureAndOrder(Structure structure, String orderFieldName, 269 String direction, int rowNumber) { 270 return ContentletFactory.getContentletsByStructureAndOrder(structure, orderFieldName, direction, rowNumber); 271 } 272 273 281 public List <Contentlet> getContents(String structureType, String categoryName) { 282 Category category = CategoryFactory.getCategoryByName(categoryName); 283 Structure structure = StructureCache.getStructureByType(structureType); 284 return ContentletFactory.getContentletsByStuctureAndCategory(structure, category, "mod_date"); 285 } 286 287 300 public List <Contentlet> getContentsByStructureAndCategoryList(String structureType, String categoryInodelist, 301 List conditionFields, String condition, String orderFieldName, String direction, int pageNumber, 302 int maxResults) { 303 Structure structure = StructureCache.getStructureByType(structureType); 304 return ContentletFactory.getContentsByStructureAndCategoryList(structure, categoryInodelist, conditionFields, 305 condition, orderFieldName, direction, pageNumber, maxResults); 306 307 } 308 309 329 public HashMap searchWithLuceneQuery(String structureType, String luceneCondition, String sortBy, String pageStr, 330 String rowsPerPage) { 331 332 long structInode = 0; 333 Structure structure = null; 334 try { 335 structInode = Long.parseLong(structureType); 336 structure = new Structure(); 337 structure.setInode(structInode); 338 } catch (Exception e) { 339 structure = StructureCache.getStructureByType(structureType); 340 } 341 342 Logger.debug(ContentsWebAPI.class, "search: luceneCondition: " + luceneCondition + ", sortBy: " + sortBy 343 + ", page: " + pageStr); 344 int perPage = Integer.parseInt(rowsPerPage); 345 int page = Integer.parseInt(pageStr); 346 int offset = (page - 1) * perPage; 347 LuceneHits assets = null; 348 HashMap retMap = new HashMap (); 349 350 assets = ContentletFactory.indexSearch(structure, luceneCondition, false, offset, perPage, sortBy); 351 352 int totalRecords = assets.getTotal(); 353 354 retMap.put("assets", assets); 355 int totalPages = (int) Math.ceil((double) totalRecords / (double) perPage); 356 retMap.put("total_records", String.valueOf(totalRecords)); 357 retMap.put("total_pages", String.valueOf(totalPages)); 358 retMap.put("total_records_int", totalRecords); 359 retMap.put("total_pages_int", totalPages); 360 retMap.put("has_next_page", page < totalPages); 361 retMap.put("has_previous_page", page > 1); 362 363 return retMap; 364 365 } 366 367 public HashMap searchWithLuceneQuery(String structureType, String luceneCondition, String sortBy, int maxResults) { 368 int page = 1; 369 int pageSize = -1; 370 return searchWithLuceneQuery(structureType, luceneCondition, sortBy, maxResults, page, pageSize); 371 } 372 373 public HashMap searchWithLuceneQuery(String structureType, String luceneCondition, String sortBy, int maxResults, 374 int page, int pageSize) { 375 378 379 long structInode = 0; 380 Structure structure = null; 381 try { 382 structInode = Long.parseLong(structureType); 383 structure = new Structure(); 384 structure.setInode(structInode); 385 } catch (Exception e) { 386 structure = StructureCache.getStructureByType(structureType); 387 } 388 389 int offSet = 0; 390 if (pageSize > 0) { 391 offSet = (page - 1) * pageSize; 392 } 393 Logger.debug(ContentsWebAPI.class, "search: luceneCondition: " + luceneCondition + ", sortBy: " + sortBy 394 + ", max results: " + maxResults); 395 LuceneHits assets = null; 396 HashMap retMap = new HashMap (); 397 398 assets = ContentletFactory.indexSearch(structure, luceneCondition, false, offSet, maxResults, sortBy); 399 400 int totalRecords = assets.getTotal(); 401 402 retMap.put("assets", assets); 403 retMap.put("total_records", String.valueOf(totalRecords)); 404 405 return retMap; 406 407 } 408 409 410 417 public List <String > getContentsPathsFromLuceneHits(LuceneHits assets, boolean working) { 418 ArrayList <String > paths = new ArrayList <String >(); 419 for (int i = 0; i < assets.length(); i++) { 420 Logger.debug(this, "getContentsPathsFromLuceneHits: Adding asset path: " 421 + (working ? "/working/" : "/live/") + assets.doc(i).get("identifier") + "_" 422 + assets.doc(i).get("languageId") + "." + Config.getStringProperty("VELOCITY_CONTENT_EXTENSION")); 423 paths.add((working ? "/working/" : "/live/") + assets.doc(i).get("identifier") + "_" 424 + assets.doc(i).get("languageId") + "." + Config.getStringProperty("VELOCITY_CONTENT_EXTENSION")); 425 426 } 427 return paths; 428 } 429 430 public static List <Category> getContentletCategories(String inode) { 431 Contentlet content = (Contentlet) InodeFactory.getInode(inode, Contentlet.class); 432 return (List <Category>) InodeFactory.getParentsOfClass(content, Category.class); 433 } 434 435 438 442 public Relationship getRelationshipByName(String relationshipName) { 443 return RelationshipFactory.getRelationshipByRelationTypeValue(relationshipName); 444 } 445 446 454 public List <Relationship> getRelationshipsOfContentlet(Contentlet cont) { 455 return getRelationshipsOfContentlet(cont.getInode()); 456 } 457 458 public List <Relationship> getRelationshipsOfContentlet(long contentletInode) { 459 return getRelationshipsOfContentlet(((Long ) contentletInode).toString()); 460 } 461 462 public List <Relationship> getRelationshipsOfContentlet(String contentletInode) { 463 Contentlet cont = (Contentlet) InodeFactory.getInode(contentletInode, Contentlet.class); 464 return RelationshipFactory.getAllRelationshipsByStructure(cont.getStructure()); 465 } 466 467 478 public List <Relationship> getRelationshipsOfContentlet(Contentlet cont, boolean hasParent) { 479 return getRelationshipsOfContentlet(cont.getInode(), hasParent); 480 } 481 482 public List <Relationship> getRelationshipsOfContentlet(long contentletInode, boolean hasParent) { 483 return getRelationshipsOfContentlet(((Long ) contentletInode).toString(), hasParent); 484 } 485 486 public List <Relationship> getRelationshipsOfContentlet(String contentletInode, boolean hasParent) { 487 Contentlet cont = (Contentlet) InodeFactory.getInode(contentletInode, Contentlet.class); 488 return RelationshipFactory.getAllRelationshipsByStructure(cont.getStructure(), hasParent); 489 } 490 491 502 public List <Relationship> getRelationshipsOfStructure(Structure st, boolean hasParent) { 503 return getRelationshipsOfStructure(st.getInode(), hasParent); 504 } 505 506 public List <Relationship> getRelationshipsOfStructure(long structureInode, boolean hasParent) { 507 return getRelationshipsOfStructure(((Long ) structureInode).toString(), hasParent); 508 } 509 510 public List <Relationship> getRelationshipsOfStructure(String structureInode, boolean hasParent) { 511 Structure st = (Structure) InodeFactory.getInode(structureInode, Structure.class); 512 return RelationshipFactory.getAllRelationshipsByStructure(st, hasParent); 513 } 514 515 529 public List <Contentlet> getRelatedContentlets(Relationship relationship, Contentlet contentlet, boolean hasParent) { 530 return RelationshipFactory.getAllRelationshipRecords(relationship, contentlet, hasParent, true,""); 531 } 532 533 public List <Contentlet> getRelatedContentlets(long relationshipInode, long contentletInode, boolean hasParent) { 534 Relationship relationship = (Relationship) InodeFactory.getInode(relationshipInode, Relationship.class); 535 Contentlet contentlet = (Contentlet) InodeFactory.getInode(contentletInode, Contentlet.class); 536 return RelationshipFactory.getAllRelationshipRecords(relationship, contentlet, hasParent, true,""); 537 } 538 539 public List <Contentlet> getRelatedContentlets(String relationshipInode, String contentletInode, boolean hasParent) { 540 Relationship relationship = (Relationship) InodeFactory.getInode(relationshipInode, Relationship.class); 541 Contentlet contentlet = (Contentlet) InodeFactory.getInode(contentletInode, Contentlet.class); 542 return RelationshipFactory.getAllRelationshipRecords(relationship, contentlet, hasParent, true,""); 543 } 544 545 public List <Contentlet> getRelatedChildContent(String relationshipName, String contentletInode) { 546 Relationship relationship = RelationshipFactory.getRelationshipByRelationTypeValue(relationshipName); 547 Contentlet contentlet = (Contentlet) InodeFactory.getInode(contentletInode, Contentlet.class); 548 return RelationshipFactory.getAllRelationshipRecords(relationship, contentlet, true, true,""); 549 } 550 551 public List <Contentlet> getRelatedParentContent(String relationshipName, String contentletInode) { 552 Relationship relationship = RelationshipFactory.getRelationshipByRelationTypeValue(relationshipName); 553 Contentlet contentlet = (Contentlet) InodeFactory.getInode(contentletInode, Contentlet.class); 554 return RelationshipFactory.getAllRelationshipRecords(relationship, contentlet, false, true,""); 555 } 556 557 public List <Contentlet> getRelatedSibblingContent(String relationshipName, String contentletInode) { 558 Relationship relationship = RelationshipFactory.getRelationshipByRelationTypeValue(relationshipName); 559 Contentlet contentlet = (Contentlet) InodeFactory.getInode(contentletInode, Contentlet.class); 560 Set <Contentlet> contSet = new HashSet (); 561 contSet.addAll(RelationshipFactory.getAllRelationshipRecords(relationship, contentlet, false)); 562 contSet.addAll(RelationshipFactory.getAllRelationshipRecords(relationship, contentlet, true)); 563 List <Contentlet> conts = new ArrayList (); 564 conts.addAll(contSet); 565 return conts; 566 } 567 568 public List <Contentlet> getRelatedContentletsByCondition(String relationshipInode, String contentletInode, boolean hasParent, String condition) { 569 Relationship relationship = (Relationship) InodeFactory.getInode(relationshipInode, Relationship.class); 570 Contentlet contentlet = (Contentlet) InodeFactory.getInode(contentletInode, Contentlet.class); 571 return RelationshipFactory.getAllRelationshipRecords(relationship, contentlet, hasParent, true, condition); 572 } 573 574 584 public boolean isParentOfTheRelationship(Contentlet contentlet, Relationship relationship) { 585 Structure contStructure = contentlet.getStructure(); 586 return relationship.getParentStructure().getInode() == contStructure.getInode(); 587 } 588 589 public boolean isParentOfTheRelationship(long contentletInode, long relationshipInode) { 590 Relationship relationship = (Relationship) InodeFactory.getInode(relationshipInode, Relationship.class); 591 Contentlet contentlet = (Contentlet) InodeFactory.getInode(contentletInode, Contentlet.class); 592 return isParentOfTheRelationship(contentlet, relationship); 593 } 594 595 public boolean isParentOfTheRelationship(String contentletInode, String relationshipInode) { 596 Relationship relationship = (Relationship) InodeFactory.getInode(relationshipInode, Relationship.class); 597 Contentlet contentlet = (Contentlet) InodeFactory.getInode(contentletInode, Contentlet.class); 598 return isParentOfTheRelationship(contentlet, relationship); 599 } 600 601 611 public boolean isChildOfTheRelationship(Contentlet contentlet, Relationship relationship) { 612 Structure contStructure = contentlet.getStructure(); 613 return relationship.getChildStructure().getInode() == contStructure.getInode(); 614 } 615 616 public boolean isChildOfTheRelationship(long contentletInode, long relationshipInode) { 617 Relationship relationship = (Relationship) InodeFactory.getInode(relationshipInode, Relationship.class); 618 Contentlet contentlet = (Contentlet) InodeFactory.getInode(contentletInode, Contentlet.class); 619 return isChildOfTheRelationship(contentlet, relationship); 620 } 621 622 public boolean isChildOfTheRelationship(String contentletInode, String relationshipInode) { 623 Relationship relationship = (Relationship) InodeFactory.getInode(relationshipInode, Relationship.class); 624 Contentlet contentlet = (Contentlet) InodeFactory.getInode(contentletInode, Contentlet.class); 625 return isChildOfTheRelationship(contentlet, relationship); 626 } 627 632 public List pullContent(String query, String lim, String sortBy) { 633 int limit = 0; 634 List <Map > l = new ArrayList <Map >(); 635 LuceneHits hits = new LuceneHits(); 636 637 int offset = 0; 638 639 try { 640 limit = Integer.parseInt(lim); 641 if(limit == 0){ 642 offset = -1; 643 } 644 645 } catch (Exception e) { 646 return l; 647 } 648 649 try { 650 651 query = LuceneUtils.findAndReplaceQueryDates(query); 652 653 QueryParser parser = new QueryParser("", new DotAnalyzer()); 655 656 Query luceneQuery = parser.parse(query); 657 hits.setLuceneQuery(luceneQuery.toString()); 658 hits = LuceneUtils.searchInCurrentIndex(luceneQuery, offset, limit, sortBy); 659 660 661 662 } catch (Exception ex) { 663 Logger.error(this.getClass(), "indexSearch: Error Searching Contentlets - lucene query: " + query, ex); 664 } 665 666 669 if(limit == 0){ 670 limit = hits.getTotal(); 671 } 672 673 for (int i = 0; i < hits.getTotal(); i++) { 674 if(i==limit){ 675 break; 676 } 677 try{ 678 679 Map <String , Object > hm = new HashMap <String , Object >(); 680 hm.put("inode", hits.doc(i).get("inode")); 681 hm.put("identifier", hits.doc(i).get("identifier")); 682 l.add(hm); 683 684 } 685 catch(Exception e){} 686 } 687 return l; 688 689 } 690 691 public List randomizeList(List list, String limit){ 692 int i = 0; 693 try { 694 i = Integer.parseInt(limit); 695 }catch(Exception e){ 696 i = list.size(); 697 } 698 699 if(list.size() > 0) { 700 Collections.shuffle(list); 701 return (list.size() > i ? list.subList(0,i):list); 702 }else 703 return list; 704 } 705 706 711 public List pageContent(String query, String sortBy, String perPage, String currentPageNumber) { 712 713 int pageNumber = 1; 714 try { 715 pageNumber = Integer.parseInt(currentPageNumber); 716 }catch(Exception e) {} 717 718 int displayPerPage = Config.getIntProperty("PER_PAGE"); 719 try { 720 displayPerPage = Integer.parseInt(perPage); 721 }catch(Exception e) {} 722 723 int minIndex = (pageNumber - 1) * displayPerPage; 724 int maxIndex = displayPerPage * pageNumber; 725 726 int limit = 0; 727 728 List <Map > l = new ArrayList <Map >(); 729 LuceneHits hits = new LuceneHits(); 730 731 try { 732 733 query = LuceneUtils.findAndReplaceQueryDates(query); 734 735 QueryParser parser = new QueryParser("", new DotAnalyzer()); 737 738 Query luceneQuery = parser.parse(query); 739 hits.setLuceneQuery(luceneQuery.toString()); 740 hits = LuceneUtils.searchInCurrentIndex(luceneQuery, -1, limit, sortBy); 741 742 743 744 } catch (Exception ex) { 745 Logger.error(this.getClass(), "indexSearch: Error Searching Contentlets - lucene query: " + query, ex); 746 } 747 748 for (int i = minIndex; i < hits.getTotal(); i++) { 749 if(i==maxIndex){ 750 break; 751 } 752 try{ 753 754 Map <String , Object > hm = new HashMap <String , Object >(); 755 hm.put("inode", hits.doc(i).get("inode")); 756 hm.put("identifier", hits.doc(i).get("identifier")); 757 l.add(hm); 758 759 } 760 catch(Exception e){} 761 } 762 return l; 763 764 } 765 766 public Map getEmptyMap() { 767 return new HashMap (); 768 } 769 770 public List getEmptyList() { 771 return new ArrayList (); 772 } 773 774 } | Popular Tags |