1 24 package org.ofbiz.content.content; 25 26 import java.io.IOException ; 27 import java.io.StringReader ; 28 import java.io.StringWriter ; 29 import java.io.Writer ; 30 import java.sql.Timestamp ; 31 import java.util.ArrayList ; 32 import java.util.HashMap ; 33 import java.util.Iterator ; 34 import java.util.List ; 35 import java.util.Locale ; 36 import java.util.Map ; 37 import java.util.Set ; 38 39 import javax.xml.parsers.ParserConfigurationException ; 40 41 import org.ofbiz.base.util.BshUtil; 42 import org.ofbiz.base.util.Debug; 43 import org.ofbiz.base.util.GeneralException; 44 import org.ofbiz.base.util.StringUtil; 45 import org.ofbiz.base.util.UtilDateTime; 46 import org.ofbiz.base.util.UtilMisc; 47 import org.ofbiz.base.util.UtilValidate; 48 import org.ofbiz.base.util.string.FlexibleStringExpander; 49 import org.ofbiz.content.ContentManagementWorker; 50 import org.ofbiz.content.data.DataResourceWorker; 51 import org.ofbiz.entity.GenericDelegator; 52 import org.ofbiz.entity.GenericEntityException; 53 import org.ofbiz.entity.GenericPK; 54 import org.ofbiz.entity.GenericValue; 55 import org.ofbiz.entity.condition.EntityConditionList; 56 import org.ofbiz.entity.condition.EntityExpr; 57 import org.ofbiz.entity.condition.EntityOperator; 58 import org.ofbiz.entity.util.EntityUtil; 59 import org.ofbiz.minilang.MiniLangException; 60 import org.ofbiz.minilang.SimpleMapProcessor; 61 import org.ofbiz.service.GenericServiceException; 62 import org.ofbiz.service.LocalDispatcher; 63 import org.ofbiz.service.ServiceUtil; 64 import org.xml.sax.InputSource ; 65 import org.xml.sax.SAXException ; 66 67 import bsh.EvalError; 68 import freemarker.ext.dom.NodeModel; 69 71 80 public class ContentWorker implements org.ofbiz.widget.ContentWorkerInterface { 81 82 public static final String module = ContentWorker.class.getName(); 83 84 public ContentWorker() { } 85 86 public GenericValue getCurrentContentExt(GenericDelegator delegator, List trail, GenericValue userLogin, Map ctx, Boolean nullThruDatesOnly, String contentAssocPredicateId) throws GeneralException { 87 return getCurrentContent(delegator, trail, userLogin, ctx, nullThruDatesOnly, contentAssocPredicateId); 88 } 89 90 public Map renderSubContentAsTextExt(GenericDelegator delegator, String contentId, Writer out, String mapKey, String subContentId, GenericValue subContentDataResourceView, 91 Map templateContext, Locale locale, String mimeTypeId, GenericValue userLogin, Timestamp fromDate) throws GeneralException, IOException { 92 return renderSubContentAsText(delegator, contentId, out, mapKey, subContentId, subContentDataResourceView, 93 templateContext, locale, mimeTypeId, userLogin, fromDate); 94 } 95 96 public String renderSubContentAsTextCacheExt(GenericDelegator delegator, String contentId, String mapKey, GenericValue subContentDataResourceView, 97 Map templateRoot, Locale locale, String mimeTypeId, GenericValue userLogin, Timestamp fromDate) throws GeneralException, IOException { 98 return renderSubContentAsTextCache(delegator, contentId, mapKey, subContentDataResourceView, 99 templateRoot, locale, mimeTypeId, userLogin, fromDate); 100 } 101 102 public Map renderSubContentAsTextCacheExt(GenericDelegator delegator, String contentId, Writer out, String mapKey, GenericValue subContentDataResourceView, 103 Map templateRoot, Locale locale, String mimeTypeId, GenericValue userLogin, Timestamp fromDate) throws GeneralException, IOException { 104 return renderSubContentAsTextCache(delegator, contentId, out, mapKey, subContentDataResourceView, 105 templateRoot, locale, mimeTypeId, userLogin, fromDate); 106 } 107 108 public Map renderSubContentAsTextCacheExt(GenericDelegator delegator, String contentId, Writer out, String mapKey, GenericValue subContentDataResourceView, 109 Map templateRoot, Locale locale, String mimeTypeId, GenericValue userLogin, Timestamp fromDate, Boolean nullThruDatesOnly) throws GeneralException, IOException { 110 return renderSubContentAsTextCache(delegator, contentId, out, mapKey, subContentDataResourceView, 111 templateRoot, locale, mimeTypeId, userLogin, fromDate, nullThruDatesOnly); 112 } 113 114 public Map renderContentAsTextExt(GenericDelegator delegator, String contentId, Writer out, Map templateContext, GenericValue view, Locale locale, String mimeTypeId) throws GeneralException, IOException { 115 return renderContentAsText(delegator, contentId, out, templateContext, view, locale, mimeTypeId); 116 } 117 118 public String renderContentAsTextCacheExt(GenericDelegator delegator, String contentId, Map templateContext, GenericValue view, Locale locale, String mimeTypeId) throws GeneralException, IOException { 119 return renderContentAsTextCache(delegator, contentId, templateContext, view, locale, mimeTypeId); 120 } 121 122 public Map renderContentAsTextCacheExt(GenericDelegator delegator, String contentId, Writer out, Map templateContext, GenericValue view, Locale locale, String mimeTypeId) throws GeneralException, IOException { 123 return renderContentAsTextCache(delegator, contentId, out, templateContext, view, locale, mimeTypeId); 124 } 125 126 public String getMimeTypeIdExt(GenericDelegator delegator, GenericValue view, Map ctx) { 127 return getMimeTypeId(delegator, view, ctx); 128 } 129 130 public GenericValue getWebSitePublishPointExt(GenericDelegator delegator, String contentId, boolean ignoreCache) throws GenericEntityException { 131 return ContentManagementWorker.getWebSitePublishPoint(delegator, contentId, ignoreCache); 132 } 133 134 135 public static GenericValue findAlternateLocaleContent(GenericDelegator delegator, GenericValue view, Locale locale) { 136 GenericValue contentAssocDataResourceViewFrom = view; 137 if (locale == null) { 138 return contentAssocDataResourceViewFrom; 139 } 140 141 String localeStr = locale.toString(); 142 boolean isTwoLetterLocale = localeStr.length() == 2; 143 144 List alternateViews = null; 145 try { 146 alternateViews = view.getRelated("ContentAssocDataResourceViewTo", UtilMisc.toMap("caContentAssocTypeId", "ALTERNATE_LOCALE"), UtilMisc.toList("-caFromDate")); 147 } catch (GenericEntityException e) { 148 Debug.logError(e, "Error finding alternate locale content: " + e.toString(), module); 149 return contentAssocDataResourceViewFrom; 150 } 151 152 alternateViews = EntityUtil.filterByDate(alternateViews, UtilDateTime.nowTimestamp(), "caFromDate", "caThruDate", true); 153 Iterator alternateViewIter = alternateViews.iterator(); 154 while (alternateViewIter.hasNext()) { 155 GenericValue thisView = (GenericValue) alternateViewIter.next(); 156 String currentLocaleString = thisView.getString("localeString"); 157 if (UtilValidate.isEmpty(currentLocaleString)) { 158 continue; 159 } 160 161 int currentLocaleLength = currentLocaleString.length(); 162 163 if (isTwoLetterLocale) { 165 if (currentLocaleLength == 2) { 166 if (localeStr.equals(currentLocaleString)) { 168 contentAssocDataResourceViewFrom = thisView; 169 break; 170 } 171 } else if (currentLocaleLength == 5) { 172 if (localeStr.equals(currentLocaleString.substring(0, 2))) { 174 contentAssocDataResourceViewFrom = thisView; 175 } 176 } 177 } else { 178 if (currentLocaleLength == 2) { 179 if (localeStr.substring(0, 2).equals(currentLocaleString)) { 181 contentAssocDataResourceViewFrom = thisView; 182 } 183 } else if (currentLocaleLength == 5) { 184 if (localeStr.equals(currentLocaleString)) { 186 contentAssocDataResourceViewFrom = thisView; 187 break; 188 } 189 } 190 } 191 } 192 193 return contentAssocDataResourceViewFrom; 194 } 195 196 public static void traverse(GenericDelegator delegator, GenericValue content, Timestamp fromDate, Timestamp thruDate, Map whenMap, int depthIdx, Map masterNode, String contentAssocTypeId, List pickList, String direction) { 197 198 String contentTypeId = null; 200 String contentId = null; 201 try { 202 if (contentAssocTypeId == null) { 203 contentAssocTypeId = ""; 204 } 205 contentId = (String ) content.get("contentId"); 206 contentTypeId = (String ) content.get("contentTypeId"); 207 List topicList = content.getRelatedByAnd("ToContentAssoc", UtilMisc.toMap("contentAssocTypeId", "TOPIC")); 208 List topics = new ArrayList (); 209 for (int i = 0; i < topicList.size(); i++) { 210 GenericValue assoc = (GenericValue) topicList.get(i); 211 topics.add(assoc.get("contentId")); 212 } 213 List keywordList = content.getRelatedByAnd("ToContentAssoc", UtilMisc.toMap("contentAssocTypeId", "KEYWORD")); 214 List keywords = new ArrayList (); 215 for (int i = 0; i < keywordList.size(); i++) { 216 GenericValue assoc = (GenericValue) keywordList.get(i); 217 keywords.add(assoc.get("contentId")); 218 } 219 List purposeValueList = content.getRelatedCache("ContentPurpose"); 220 List purposes = new ArrayList (); 221 for (int i = 0; i < purposeValueList.size(); i++) { 222 GenericValue purposeValue = (GenericValue) purposeValueList.get(i); 223 purposes.add(purposeValue.get("contentPurposeTypeId")); 224 } 225 List contentTypeAncestry = new ArrayList (); 226 getContentTypeAncestry(delegator, contentTypeId, contentTypeAncestry); 227 228 Map context = new HashMap (); 229 context.put("content", content); 230 context.put("contentAssocTypeId", contentAssocTypeId); 231 context.put("purposes", purposes); 233 context.put("topics", topics); 234 context.put("keywords", keywords); 235 context.put("typeAncestry", contentTypeAncestry); 236 boolean isPick = checkWhen(context, (String ) whenMap.get("pickWhen")); 237 boolean isReturnBefore = checkReturnWhen(context, (String ) whenMap.get("returnBeforePickWhen")); 238 Map thisNode = null; 239 if (isPick || !isReturnBefore) { 240 thisNode = new HashMap (); 241 thisNode.put("contentId", contentId); 242 thisNode.put("contentTypeId", contentTypeId); 243 thisNode.put("contentAssocTypeId", contentAssocTypeId); 244 List kids = (List ) masterNode.get("kids"); 245 if (kids == null) { 246 kids = new ArrayList (); 247 masterNode.put("kids", kids); 248 } 249 kids.add(thisNode); 250 } 251 if (isPick) { 252 pickList.add(content); 253 thisNode.put("value", content); 254 } 255 boolean isReturnAfter = checkReturnWhen(context, (String ) whenMap.get("returnAfterPickWhen")); 256 if (!isReturnAfter) { 257 258 List relatedAssocs = getContentAssocsWithId(delegator, contentId, fromDate, thruDate, direction, new ArrayList ()); 259 Iterator it = relatedAssocs.iterator(); 260 Map assocContext = new HashMap (); 261 assocContext.put("related", relatedAssocs); 262 while (it.hasNext()) { 263 GenericValue assocValue = (GenericValue) it.next(); 264 contentAssocTypeId = (String ) assocValue.get("contentAssocTypeId"); 265 assocContext.put("contentAssocTypeId", contentAssocTypeId); 266 assocContext.put("parentContent", content); 268 String assocRelation = null; 269 String relatedDirection = null; 271 if (direction != null && direction.equalsIgnoreCase("From")) { 272 assocContext.put("contentIdFrom", assocValue.get("contentId")); 273 assocRelation = "ToContent"; 274 relatedDirection = "From"; 275 } else { 276 assocContext.put("contentIdTo", assocValue.get("contentId")); 277 assocRelation = "FromContent"; 278 relatedDirection = "To"; 279 } 280 281 boolean isFollow = checkWhen(assocContext, (String ) whenMap.get("followWhen")); 282 if (isFollow) { 283 GenericValue thisContent = assocValue.getRelatedOne(assocRelation); 284 traverse(delegator, thisContent, fromDate, thruDate, whenMap, depthIdx + 1, thisNode, contentAssocTypeId, pickList, relatedDirection); 285 } 286 } 287 } 288 } catch (GenericEntityException e) { 289 Debug.logError("Entity Error:" + e.getMessage(), null); 290 } 291 return; 292 } 293 294 public static boolean traverseSubContent(Map ctx) { 295 296 boolean inProgress = false; 297 List nodeTrail = (List )ctx.get("nodeTrail"); 298 ContentWorker.traceNodeTrail("11",nodeTrail); 299 int sz = nodeTrail.size(); 300 if (sz == 0) { 301 return false; 302 } 303 304 Map currentNode = (Map )nodeTrail.get(sz - 1); 305 Boolean isReturnAfter = (Boolean )currentNode.get("isReturnAfter"); 306 if (isReturnAfter != null && isReturnAfter.booleanValue()) { 307 return false; 308 } 309 310 List kids = (List )currentNode.get("kids"); 311 if (kids != null && kids.size() > 0) { 312 int idx = 0; 313 while (idx < kids.size()) { 314 currentNode = (Map )kids.get(idx); 315 ContentWorker.traceNodeTrail("12",nodeTrail); 316 Boolean isPick = (Boolean )currentNode.get("isPick"); 317 318 if (isPick != null && isPick.booleanValue()) { 319 nodeTrail.add(currentNode); 320 inProgress = true; 321 selectKids(currentNode, ctx); 322 ContentWorker.traceNodeTrail("14",nodeTrail); 323 break; 324 } else { 325 Boolean isFollow = (Boolean )currentNode.get("isFollow"); 326 if (isFollow != null || isFollow.booleanValue()) { 327 nodeTrail.add(currentNode); 328 boolean foundPick = traverseSubContent(ctx); 329 if (foundPick) { 330 inProgress = true; 331 break; 332 } 333 } 334 } 335 idx++; 336 } 337 } 338 339 if (!inProgress) { 340 while (sz > 1) { 342 currentNode = (Map )nodeTrail.remove(--sz); 343 ContentWorker.traceNodeTrail("15",nodeTrail); 344 Map parentNode = (Map )nodeTrail.get(sz - 1); 345 kids = (List )parentNode.get("kids"); 346 if (kids == null) { 347 continue; 348 } 349 350 int idx = kids.indexOf(currentNode); 351 while (idx < (kids.size() - 1)) { 352 currentNode = (Map )kids.get(idx + 1); 353 Boolean isFollow = (Boolean )currentNode.get("isFollow"); 354 if (isFollow == null || !isFollow.booleanValue()) { 355 idx++; 356 continue; 357 } 358 String contentAssocTypeId = (String )currentNode.get("contentAssocTypeId"); 359 nodeTrail.add(currentNode); 360 ContentWorker.traceNodeTrail("16",nodeTrail); 361 Boolean isPick = (Boolean )currentNode.get("isPick"); 362 if (isPick == null || !isPick.booleanValue()) { 363 inProgress = traverseSubContent(ctx); 365 ContentWorker.traceNodeTrail("17",nodeTrail); 366 if (inProgress) { 367 break; 368 } 369 } else { 370 inProgress = true; 371 break; 372 } 373 idx++; 374 } 375 if (inProgress) { 376 break; 377 } 378 } 379 } 380 return inProgress; 381 } 382 383 public static List getPurposes(GenericValue content) { 384 List purposes = new ArrayList (); 385 try { 386 List purposeValueList = content.getRelatedCache("ContentPurpose"); 387 for (int i = 0; i < purposeValueList.size(); i++) { 388 GenericValue purposeValue = (GenericValue) purposeValueList.get(i); 389 purposes.add(purposeValue.get("contentPurposeTypeId")); 390 } 391 } catch (GenericEntityException e) { 392 Debug.logError("Entity Error:" + e.getMessage(), null); 393 } 394 return purposes; 395 } 396 397 public static List getSections(GenericValue content) { 398 List sections = new ArrayList (); 399 try { 400 List sectionValueList = content.getRelatedCache("FromContentAssoc"); 401 for (int i = 0; i < sectionValueList.size(); i++) { 402 GenericValue sectionValue = (GenericValue) sectionValueList.get(i); 403 String contentAssocPredicateId = (String )sectionValue.get("contentAssocPredicateId"); 404 if (contentAssocPredicateId != null && contentAssocPredicateId.equals("categorizes")) { 405 sections.add(sectionValue.get("contentIdTo")); 406 } 407 } 408 } catch (GenericEntityException e) { 409 Debug.logError("Entity Error:" + e.getMessage(), null); 410 } 411 return sections; 412 } 413 414 public static List getTopics(GenericValue content) { 415 List topics = new ArrayList (); 416 try { 417 List topicValueList = content.getRelatedCache("FromContentAssoc"); 418 for (int i = 0; i < topicValueList.size(); i++) { 419 GenericValue topicValue = (GenericValue) topicValueList.get(i); 420 String contentAssocPredicateId = (String )topicValue.get("contentAssocPredicateId"); 421 if (contentAssocPredicateId != null && contentAssocPredicateId.equals("topifies")) 422 topics.add(topicValue.get("contentIdTo")); 423 } 424 } catch (GenericEntityException e) { 425 Debug.logError("Entity Error:" + e.getMessage(), null); 426 } 427 return topics; 428 } 429 430 public static void selectKids(Map currentNode, Map ctx) { 431 432 GenericDelegator delegator = (GenericDelegator) ctx.get("delegator"); 433 GenericValue parentContent = (GenericValue) currentNode.get("value"); 434 String contentAssocTypeId = (String ) ctx.get("contentAssocTypeId"); 435 String contentTypeId = (String ) ctx.get("contentTypeId"); 436 String mapKey = (String ) ctx.get("mapKey"); 437 String parentContentId = (String ) parentContent.get("contentId"); 438 Map whenMap = (Map ) ctx.get("whenMap"); 440 Map context = new HashMap (); 441 List kids = new ArrayList (); 442 currentNode.put("kids", kids); 443 String direction = (String ) ctx.get("direction"); 444 if (UtilValidate.isEmpty(direction)) 445 direction = "From"; 446 Timestamp fromDate = (Timestamp ) ctx.get("fromDate"); 447 Timestamp thruDate = (Timestamp ) ctx.get("thruDate"); 448 449 List assocTypeList = StringUtil.split(contentAssocTypeId, " "); 450 List contentTypeList = StringUtil.split(contentTypeId, " "); 451 String contentAssocPredicateId = null; 452 Boolean nullThruDatesOnly = new Boolean (true); 453 Map results = null; 454 try { 455 results = ContentServicesComplex.getAssocAndContentAndDataResourceCacheMethod(delegator, parentContentId, mapKey, direction, null, null, assocTypeList, contentTypeList, nullThruDatesOnly, contentAssocPredicateId); 456 } catch (GenericEntityException e) { 457 throw new RuntimeException (e.getMessage()); 458 } catch (MiniLangException e2) { 459 throw new RuntimeException (e2.getMessage()); 460 } 461 List relatedViews = (List ) results.get("entityList"); 462 Iterator it = relatedViews.iterator(); 464 while (it.hasNext()) { 465 GenericValue assocValue = (GenericValue) it.next(); 466 Map thisNode = ContentWorker.makeNode(assocValue); 467 checkConditions(delegator, thisNode, null, whenMap); 468 boolean isReturnBeforePick = booleanDataType(thisNode.get("isReturnBeforePick")); 469 boolean isReturnAfterPick = booleanDataType(thisNode.get("isReturnAfterPick")); 470 boolean isPick = booleanDataType(thisNode.get("isPick")); 471 boolean isFollow = booleanDataType(thisNode.get("isFollow")); 472 kids.add(thisNode); 473 if (isPick) { 474 Integer count = (Integer ) currentNode.get("count"); 475 if (count == null) { 476 count = new Integer (1); 477 } else { 478 count = new Integer (count.intValue() + 1); 479 } 480 currentNode.put("count", count); 481 } 482 } 483 return; 484 } 485 486 public static boolean checkWhen(Map context, String whenStr) { 487 488 boolean isWhen = true; if (whenStr != null && whenStr.length() > 0) { 490 FlexibleStringExpander fse = new FlexibleStringExpander(whenStr); 491 String newWhen = fse.expandString(context); 492 try { 495 Boolean isWhenObj = (Boolean ) BshUtil.eval(newWhen, context); 496 isWhen = isWhenObj.booleanValue(); 497 } catch (EvalError e) { 498 Debug.logError("Error in evaluating :" + whenStr + " : " + e.getMessage(), null); 499 throw new RuntimeException (e.getMessage()); 500 501 } 502 } 503 return isWhen; 505 } 506 507 public static boolean checkReturnWhen(Map context, String whenStr) { 508 boolean isWhen = false; if (whenStr != null && whenStr.length() > 0) { 510 FlexibleStringExpander fse = new FlexibleStringExpander(whenStr); 511 String newWhen = fse.expandString(context); 512 try { 513 Boolean isWhenObj = (Boolean ) BshUtil.eval(newWhen, context); 514 isWhen = isWhenObj.booleanValue(); 515 } catch (EvalError e) { 516 Debug.logError("Error in evaluating :" + whenStr + " : " + e.getMessage(), null); 517 throw new RuntimeException (e.getMessage()); 518 } 519 } 520 return isWhen; 521 } 522 523 public static List getAssociatedContent(GenericValue currentContent, String linkDir, List assocTypes, List contentTypes, String fromDate, String thruDate) 524 throws GenericEntityException { 525 526 GenericDelegator delegator = currentContent.getDelegator(); 527 List assocList = getAssociations(currentContent, linkDir, assocTypes, fromDate, thruDate); 528 if (assocList == null || assocList.size() == 0) 529 return assocList; 530 if (Debug.infoOn()) Debug.logInfo("assocList:" + assocList.size() + " contentId:" + currentContent.getString("contentId"), ""); 531 532 List contentList = new ArrayList (); 533 String contentIdName = "contentId"; 534 if (linkDir != null && linkDir.equalsIgnoreCase("TO")) { 535 contentIdName.concat("To"); 536 } 537 GenericValue assoc = null; 538 GenericValue content = null; 539 String contentTypeId = null; 540 Iterator assocIt = assocList.iterator(); 541 while (assocIt.hasNext()) { 542 assoc = (GenericValue) assocIt.next(); 543 String contentId = (String ) assoc.get(contentIdName); 544 if (Debug.infoOn()) Debug.logInfo("contentId:" + contentId, ""); 545 content = delegator.findByPrimaryKey("Content", UtilMisc.toMap("contentId", contentId)); 546 if (contentTypes != null && contentTypes.size() > 0) { 547 contentTypeId = (String ) content.get("contentTypeId"); 548 if (contentTypes.contains(contentTypeId)) { 549 contentList.add(content); 550 } 551 } else { 552 contentList.add(content); 553 } 554 555 } 556 if (Debug.infoOn()) Debug.logInfo("contentList:" + contentList.size() , ""); 557 return contentList; 558 559 } 560 561 public static List getAssociatedContentView(GenericValue currentContent, String linkDir, List assocTypes, List contentTypes, String fromDate, String thruDate) throws GenericEntityException { 562 List contentList = new ArrayList (); 563 List exprListAnd = new ArrayList (); 564 565 String origContentId = (String ) currentContent.get("contentId"); 566 String contentIdName = "contentId"; 567 String contentAssocViewName = "contentAssocView"; 568 if (linkDir != null && linkDir.equalsIgnoreCase("TO")) { 569 contentIdName.concat("To"); 570 contentAssocViewName.concat("To"); 571 } 572 EntityExpr expr = new EntityExpr(contentIdName, EntityOperator.EQUALS, origContentId); 573 exprListAnd.add(expr); 574 575 if (contentTypes.size() > 0) { 576 List exprListOr = new ArrayList (); 577 Iterator it = contentTypes.iterator(); 578 while (it.hasNext()) { 579 String contentType = (String ) it.next(); 580 expr = new EntityExpr("contentTypeId", EntityOperator.EQUALS, contentType); 581 exprListOr.add(expr); 582 } 583 EntityConditionList contentExprList = new EntityConditionList(exprListOr, EntityOperator.OR); 584 exprListAnd.add(contentExprList); 585 } 586 if (assocTypes.size() > 0) { 587 List exprListOr = new ArrayList (); 588 Iterator it = assocTypes.iterator(); 589 while (it.hasNext()) { 590 String assocType = (String ) it.next(); 591 expr = new EntityExpr("contentAssocTypeId", EntityOperator.EQUALS, assocType); 592 exprListOr.add(expr); 593 } 594 EntityConditionList assocExprList = new EntityConditionList(exprListOr, EntityOperator.OR); 595 exprListAnd.add(assocExprList); 596 } 597 598 if (fromDate != null) { 599 Timestamp tsFrom = UtilDateTime.toTimestamp(fromDate); 600 expr = new EntityExpr("fromDate", EntityOperator.GREATER_THAN_EQUAL_TO, tsFrom); 601 exprListAnd.add(expr); 602 } 603 604 if (thruDate != null) { 605 Timestamp tsThru = UtilDateTime.toTimestamp(thruDate); 606 expr = new EntityExpr("thruDate", EntityOperator.LESS_THAN, tsThru); 607 exprListAnd.add(expr); 608 } 609 EntityConditionList contentCondList = new EntityConditionList(exprListAnd, EntityOperator.AND); 610 GenericDelegator delegator = currentContent.getDelegator(); 611 contentList = delegator.findByCondition(contentAssocViewName, contentCondList, null, null); 612 return contentList; 613 } 614 615 public static List getAssociations(GenericValue currentContent, String linkDir, List assocTypes, String strFromDate, String strThruDate) throws GenericEntityException { 616 GenericDelegator delegator = currentContent.getDelegator(); 617 String origContentId = (String ) currentContent.get("contentId"); 618 Timestamp fromDate = null; 619 if (strFromDate != null) { 620 fromDate = UtilDateTime.toTimestamp(strFromDate); 621 } 622 Timestamp thruDate = null; 623 if (strThruDate != null) { 624 thruDate = UtilDateTime.toTimestamp(strThruDate); 625 } 626 List assocs = getContentAssocsWithId(delegator, origContentId, fromDate, thruDate, linkDir, assocTypes); 627 return assocs; 629 } 630 631 public static List getContentAssocsWithId(GenericDelegator delegator, String contentId, Timestamp fromDate, Timestamp thruDate, String direction, List assocTypes) throws GenericEntityException { 632 List exprList = new ArrayList (); 633 EntityExpr joinExpr = null; 634 EntityExpr expr = null; 635 if (direction != null && direction.equalsIgnoreCase("From")) { 636 joinExpr = new EntityExpr("contentIdTo", EntityOperator.EQUALS, contentId); 637 } else { 638 joinExpr = new EntityExpr("contentId", EntityOperator.EQUALS, contentId); 639 } 640 exprList.add(joinExpr); 641 if (assocTypes != null && assocTypes.size() > 0) { 642 List exprListOr = new ArrayList (); 643 Iterator it = assocTypes.iterator(); 644 while (it.hasNext()) { 645 String assocType = (String ) it.next(); 646 expr = new EntityExpr("contentAssocTypeId", EntityOperator.EQUALS, assocType); 647 exprListOr.add(expr); 648 } 649 EntityConditionList assocExprList = new EntityConditionList(exprListOr, EntityOperator.OR); 650 exprList.add(assocExprList); 651 } 652 if (fromDate != null) { 653 EntityExpr fromExpr = new EntityExpr("fromDate", EntityOperator.GREATER_THAN_EQUAL_TO, fromDate); 654 exprList.add(fromExpr); 655 } 656 if (thruDate != null) { 657 List thruList = new ArrayList (); 658 660 EntityExpr thruExpr = new EntityExpr("thruDate", EntityOperator.LESS_THAN, thruDate); 661 thruList.add(thruExpr); 662 EntityExpr thruExpr2 = new EntityExpr("thruDate", EntityOperator.EQUALS, null); 663 thruList.add(thruExpr2); 664 EntityConditionList thruExprList = new EntityConditionList(thruList, EntityOperator.OR); 665 exprList.add(thruExprList); 666 } else if (fromDate != null) { 667 List thruList = new ArrayList (); 668 669 EntityExpr thruExpr = new EntityExpr("thruDate", EntityOperator.GREATER_THAN, fromDate); 670 thruList.add(thruExpr); 671 EntityExpr thruExpr2 = new EntityExpr("thruDate", EntityOperator.EQUALS, null); 672 thruList.add(thruExpr2); 673 EntityConditionList thruExprList = new EntityConditionList(thruList, EntityOperator.OR); 674 exprList.add(thruExprList); 675 } else { 676 EntityExpr thruExpr2 = new EntityExpr("thruDate", EntityOperator.EQUALS, null); 677 exprList.add(thruExpr2); 678 } 679 EntityConditionList assocExprList = new EntityConditionList(exprList, EntityOperator.AND); 680 List relatedAssocs = delegator.findByCondition("ContentAssoc", assocExprList, new ArrayList (), UtilMisc.toList("-fromDate")); 682 return relatedAssocs; 687 } 688 689 public static void getContentTypeAncestry(GenericDelegator delegator, String contentTypeId, List contentTypes) throws GenericEntityException { 690 contentTypes.add(contentTypeId); 691 GenericValue contentTypeValue = delegator.findByPrimaryKey("ContentType", UtilMisc.toMap("contentTypeId", contentTypeId)); 692 if (contentTypeValue == null) 693 return; 694 String parentTypeId = (String ) contentTypeValue.get("parentTypeId"); 695 if (parentTypeId != null) { 696 getContentTypeAncestry(delegator, parentTypeId, contentTypes); 697 } 698 return; 699 } 700 701 public static void getContentAncestry(GenericDelegator delegator, String contentId, String contentAssocTypeId, String direction, List contentAncestorList) throws GenericEntityException { 702 String contentIdField = null; 703 String contentIdOtherField = null; 704 if (direction != null && direction.equalsIgnoreCase("to")) { 705 contentIdField = "contentId"; 706 contentIdOtherField = "contentIdTo"; 707 } else { 708 contentIdField = "contentIdTo"; 709 contentIdOtherField = "contentId"; 710 } 711 712 if (Debug.infoOn()) Debug.logInfo("getContentAncestry, contentId:" + contentId, ""); 713 if (Debug.infoOn()) Debug.logInfo("getContentAncestry, contentAssocTypeId:" + contentAssocTypeId, ""); 714 Map andMap = null; 715 if (UtilValidate.isEmpty(contentAssocTypeId)) { 716 andMap = UtilMisc.toMap(contentIdField, contentId); 717 } else { 718 andMap = UtilMisc.toMap(contentIdField, contentId, "contentAssocTypeId", contentAssocTypeId); 719 } 720 try { 721 List lst = delegator.findByAndCache("ContentAssoc", andMap); 722 List lst2 = EntityUtil.filterByDate(lst); 724 if (lst2.size() > 0) { 726 GenericValue contentAssoc = (GenericValue)lst2.get(0); 727 getContentAncestry(delegator, contentAssoc.getString(contentIdOtherField), contentAssocTypeId, direction, contentAncestorList); 728 contentAncestorList.add(contentAssoc.getString(contentIdOtherField)); 729 } 730 } catch(GenericEntityException e) { 731 Debug.logError(e,module); 732 return; 733 } 734 return; 735 } 736 737 public static void getContentAncestryAll(GenericDelegator delegator, String contentId, String passedContentTypeId, String direction, List contentAncestorList) { 738 String contentIdField = null; 739 String contentIdOtherField = null; 740 if (direction != null && direction.equalsIgnoreCase("to")) { 741 contentIdField = "contentId"; 742 contentIdOtherField = "contentIdTo"; 743 } else { 744 contentIdField = "contentIdTo"; 745 contentIdOtherField = "contentId"; 746 } 747 748 if (Debug.infoOn()) Debug.logInfo("getContentAncestry, contentId:" + contentId, ""); 749 Map andMap = UtilMisc.toMap(contentIdField, contentId); 750 try { 751 List lst = delegator.findByAndCache("ContentAssoc", andMap); 752 List lst2 = EntityUtil.filterByDate(lst); 754 Iterator iter = lst2.iterator(); 756 while (iter.hasNext()) { 757 GenericValue contentAssoc = (GenericValue)iter.next(); 758 String contentIdOther = contentAssoc.getString(contentIdOtherField); 759 if (!contentAncestorList.contains(contentIdOther)) { 760 getContentAncestryAll(delegator, contentIdOther, passedContentTypeId, direction, contentAncestorList); 761 if (!contentAncestorList.contains(contentIdOther)) { 762 GenericValue contentTo = delegator.findByPrimaryKeyCache("Content", UtilMisc.toMap("contentId", contentIdOther)); 763 String contentTypeId = contentTo.getString("contentTypeId"); 764 if (contentTypeId != null && contentTypeId.equals(passedContentTypeId)) 765 contentAncestorList.add(contentIdOther ); 766 } 767 } 768 } 769 } catch(GenericEntityException e) { 770 Debug.logError(e,module); 771 return; 772 } 773 return; 774 } 775 776 public static List getContentAncestryNodeTrail(GenericDelegator delegator, String contentId, String contentAssocTypeId, String direction) throws GenericEntityException { 777 778 List contentAncestorList = new ArrayList (); 779 List nodeTrail = new ArrayList (); 780 getContentAncestry(delegator, contentId, contentAssocTypeId, direction, contentAncestorList); 781 Iterator contentAncestorListIter = contentAncestorList.iterator(); 782 while (contentAncestorListIter.hasNext()) { 783 GenericValue value = (GenericValue) contentAncestorListIter.next(); 784 Map thisNode = ContentWorker.makeNode(value); 785 nodeTrail.add(thisNode); 786 } 787 return nodeTrail; 788 } 789 790 public static String getContentAncestryNodeTrailCsv(GenericDelegator delegator, String contentId, String contentAssocTypeId, String direction) throws GenericEntityException { 791 792 List contentAncestorList = new ArrayList (); 793 getContentAncestry(delegator, contentId, contentAssocTypeId, direction, contentAncestorList); 794 String csv = StringUtil.join(contentAncestorList, ","); 795 return csv; 796 } 797 798 public static void getContentAncestryValues(GenericDelegator delegator, String contentId, String contentAssocTypeId, String direction, List contentAncestorList) throws GenericEntityException { 799 String contentIdField = null; 800 String contentIdOtherField = null; 801 if (direction != null && direction.equalsIgnoreCase("to")) { 802 contentIdField = "contentId"; 803 contentIdOtherField = "contentIdTo"; 804 } else { 805 contentIdField = "contentIdTo"; 806 contentIdOtherField = "contentId"; 807 } 808 809 try { 811 List lst = delegator.findByAndCache("ContentAssoc", UtilMisc.toMap(contentIdField, contentId, "contentAssocTypeId", contentAssocTypeId)); 812 List lst2 = EntityUtil.filterByDate(lst); 814 if (lst2.size() > 0) { 816 GenericValue contentAssoc = (GenericValue)lst2.get(0); 817 getContentAncestryValues(delegator, contentAssoc.getString(contentIdOtherField), contentAssocTypeId, direction, contentAncestorList); 818 GenericValue content = delegator.findByPrimaryKeyCache("Content", UtilMisc.toMap("contentId", contentAssoc.getString(contentIdOtherField))); 819 820 contentAncestorList.add(content); 821 } 822 } catch(GenericEntityException e) { 823 Debug.logError(e,module); 824 return; 825 } 826 return; 827 } 828 829 public static Map pullEntityValues(GenericDelegator delegator, String entityName, Map context) { 830 GenericValue entOut = delegator.makeValue(entityName, null); 831 entOut.setPKFields(context); 832 entOut.setNonPKFields(context); 833 return (Map ) entOut; 834 } 835 836 839 public static String callContentPermissionCheck(GenericDelegator delegator, LocalDispatcher dispatcher, Map context) { 840 Map permResults = callContentPermissionCheckResult(delegator, dispatcher, context); 841 String permissionStatus = (String ) permResults.get("permissionStatus"); 842 return permissionStatus; 843 } 844 845 public static Map callContentPermissionCheckResult(GenericDelegator delegator, LocalDispatcher dispatcher, Map context) { 846 847 Map permResults = new HashMap (); 848 String skipPermissionCheck = (String ) context.get("skipPermissionCheck"); 849 850 if (skipPermissionCheck == null 851 || skipPermissionCheck.length() == 0 852 || (!skipPermissionCheck.equalsIgnoreCase("true") && !skipPermissionCheck.equalsIgnoreCase("granted"))) { 853 GenericValue userLogin = (GenericValue) context.get("userLogin"); 854 Map serviceInMap = new HashMap (); 855 serviceInMap.put("userLogin", userLogin); 856 serviceInMap.put("targetOperationList", context.get("targetOperationList")); 857 serviceInMap.put("contentPurposeList", context.get("contentPurposeList")); 858 serviceInMap.put("targetOperationString", context.get("targetOperationString")); 859 serviceInMap.put("contentPurposeString", context.get("contentPurposeString")); 860 serviceInMap.put("entityOperation", context.get("entityOperation")); 861 serviceInMap.put("currentContent", context.get("currentContent")); 862 serviceInMap.put("displayFailCond", context.get("displayFailCond")); 863 864 try { 865 permResults = dispatcher.runSync("checkContentPermission", serviceInMap); 866 } catch (GenericServiceException e) { 867 Debug.logError(e, "Problem checking permissions", "ContentServices"); 868 } 869 } else { 870 permResults.put("permissionStatus", "granted"); 871 } 872 return permResults; 873 } 874 875 public static GenericValue getSubContent(GenericDelegator delegator, String contentId, String mapKey, String subContentId, GenericValue userLogin, List assocTypes, Timestamp fromDate) throws IOException { 876 GenericValue view = null; 878 try { 879 if (subContentId == null) { 880 if (contentId == null) { 881 throw new GenericEntityException("contentId and subContentId are null."); 882 } 883 Map results = null; 884 results = ContentServicesComplex.getAssocAndContentAndDataResourceMethod(delegator, contentId, mapKey, "From", fromDate, null, null, null, assocTypes, null); 885 List entityList = (List ) results.get("entityList"); 886 if (entityList == null || entityList.size() == 0) { 887 } else { 889 view = (GenericValue) entityList.get(0); 890 } 891 } else { 892 List lst = delegator.findByAnd("ContentDataResourceView", UtilMisc.toMap("contentId", subContentId)); 893 if (lst == null || lst.size() == 0) { 894 throw new IOException ("No subContent found for subContentId=." + subContentId); 895 } 896 view = (GenericValue) lst.get(0); 897 } 898 } catch (GenericEntityException e) { 899 throw new IOException (e.getMessage()); 900 } 901 return view; 902 } 903 904 public static GenericValue getSubContentCache(GenericDelegator delegator, String contentId, String mapKey, String subContentId, GenericValue userLogin, List assocTypes, Timestamp fromDate, Boolean nullThruDatesOnly, String contentAssocPredicateId) throws GenericEntityException { 905 GenericValue view = null; 907 if (UtilValidate.isEmpty(subContentId)) { 908 view = getSubContentCache(delegator, contentId, mapKey, userLogin, assocTypes, fromDate, nullThruDatesOnly, contentAssocPredicateId); 909 } else { 910 view = getContentCache(delegator, subContentId); 911 } 912 return view; 913 } 914 915 public static GenericValue getSubContentCache(GenericDelegator delegator, String contentId, String mapKey, GenericValue userLogin, List assocTypes, Timestamp fromDate, Boolean nullThruDatesOnly, String contentAssocPredicateId) throws GenericEntityException { 916 GenericValue view = null; 918 if (contentId == null) { 919 Debug.logError("ContentId is null", module); 920 return view; 921 } 922 Map results = null; 923 List contentTypes = null; 924 try { 925 results = ContentServicesComplex.getAssocAndContentAndDataResourceCacheMethod(delegator, contentId, mapKey, "From", fromDate, null, assocTypes, contentTypes, nullThruDatesOnly, contentAssocPredicateId); 926 } catch(MiniLangException e) { 927 throw new RuntimeException (e.getMessage()); 928 } 929 List entityList = (List ) results.get("entityList"); 930 if (entityList == null || entityList.size() == 0) { 931 } else { 933 view = (GenericValue) entityList.get(0); 934 } 935 return view; 936 } 937 938 public static GenericValue getContentCache(GenericDelegator delegator, String contentId) throws GenericEntityException { 939 940 GenericValue view = null; 941 List lst = delegator.findByAndCache("ContentDataResourceView", UtilMisc.toMap("contentId", contentId)); 942 if (lst != null && lst.size() > 0) { 944 view = (GenericValue) lst.get(0); 945 } 946 return view; 947 } 948 949 public static GenericValue getCurrentContent(GenericDelegator delegator, List trail, GenericValue userLogin, Map ctx, Boolean nullThruDatesOnly, String contentAssocPredicateId) throws GeneralException { 950 951 String contentId = (String )ctx.get("contentId"); 952 String subContentId = (String )ctx.get("subContentId"); 953 String mapKey = (String )ctx.get("mapKey"); 954 Timestamp fromDate = UtilDateTime.nowTimestamp(); 955 List assocTypes = null; 956 List passedGlobalNodeTrail = null; 957 GenericValue currentContent = null; 958 String viewContentId = null; 959 if (trail != null && trail.size() > 0) { 960 passedGlobalNodeTrail = new ArrayList (trail); 961 } else { 962 passedGlobalNodeTrail = new ArrayList (); 963 } 964 int sz = passedGlobalNodeTrail.size(); 966 if (sz > 0) { 967 Map nd = (Map )passedGlobalNodeTrail.get(sz - 1); 968 if (nd != null) 969 currentContent = (GenericValue)nd.get("value"); 970 if (currentContent != null) 971 viewContentId = (String )currentContent.get("contentId"); 972 } 973 974 if (UtilValidate.isNotEmpty(subContentId)) { 979 ctx.put("subContentId", subContentId); 980 ctx.put("contentId", null); 981 if (viewContentId != null && viewContentId.equals(subContentId) ) { 982 return currentContent; 983 } 984 } else { 985 ctx.put("contentId", contentId); 986 ctx.put("subContentId", null); 987 if (viewContentId != null && viewContentId.equals(contentId) ) { 988 return currentContent; 989 } 990 } 991 if (UtilValidate.isNotEmpty(contentId) || UtilValidate.isNotEmpty(subContentId)) { 993 try { 994 currentContent = ContentWorker.getSubContentCache(delegator, contentId, mapKey, subContentId, userLogin, assocTypes, fromDate, nullThruDatesOnly, contentAssocPredicateId); 995 Map node = ContentWorker.makeNode(currentContent); 996 passedGlobalNodeTrail.add(node); 997 } catch (GenericEntityException e) { 998 throw new GeneralException(e.getMessage()); 999 } 1000 } 1001 ctx.put("globalNodeTrail", passedGlobalNodeTrail); 1002 ctx.put("indent", new Integer (sz)); 1003 return currentContent; 1005 } 1006 1007 public static GenericValue getContentFromView(GenericValue view) { 1008 GenericValue content = null; 1009 if (view == null) { 1010 return content; 1011 } 1012 GenericDelegator delegator = view.getDelegator(); 1013 content = delegator.makeValue("Content", null); 1014 content.setPKFields(view); 1015 content.setNonPKFields(view); 1016 String dataResourceId = null; 1017 try { 1018 dataResourceId = (String ) view.get("drDataResourceId"); 1019 } catch (Exception e) { 1020 dataResourceId = (String ) view.get("dataResourceId"); 1021 } 1022 content.set("dataResourceId", dataResourceId); 1023 return content; 1024 } 1025 1026 public static Map renderSubContentAsText(GenericDelegator delegator, String contentId, Writer out, String mapKey, String subContentId, GenericValue subContentDataResourceView, 1027 Map templateContext, Locale locale, String mimeTypeId, GenericValue userLogin, Timestamp fromDate) throws GeneralException, IOException { 1028 1029 Map results = new HashMap (); 1030 if (subContentDataResourceView == null) { 1032 subContentDataResourceView = getSubContentCache(delegator, contentId, mapKey, subContentId, userLogin, null, fromDate, new Boolean (false), null); 1033 } 1034 1035 results.put("view", subContentDataResourceView); 1036 if (subContentDataResourceView == null) { 1037 return results; 1039 } 1040 1041 contentId = (String ) subContentDataResourceView.get("contentId"); 1043 1045 if (templateContext == null) { 1046 templateContext = new HashMap (); 1047 } 1048 1049 renderContentAsText(delegator, contentId, out, templateContext, subContentDataResourceView, locale, mimeTypeId); 1050 1051 return results; 1052 } 1053 1054 public static String renderSubContentAsTextCache(GenericDelegator delegator, String contentId, String mapKey, GenericValue subContentDataResourceView, 1055 Map templateRoot, Locale locale, String mimeTypeId, GenericValue userLogin, Timestamp fromDate) throws GeneralException, IOException { 1056 Boolean nullThruDatesOnly = new Boolean (false); 1057 Writer outWriter = new StringWriter (); 1058 Map map = renderSubContentAsTextCache(delegator, contentId, outWriter, mapKey, subContentDataResourceView, templateRoot, locale, mimeTypeId, userLogin, fromDate, nullThruDatesOnly); 1059 return outWriter.toString(); 1060 } 1061 1062 public static Map renderSubContentAsTextCache(GenericDelegator delegator, String contentId, Writer out, String mapKey, GenericValue subContentDataResourceView, 1063 Map templateRoot, Locale locale, String mimeTypeId, GenericValue userLogin, Timestamp fromDate) throws GeneralException, IOException { 1064 Boolean nullThruDatesOnly = new Boolean (false); 1065 return renderSubContentAsTextCache(delegator, contentId, out, mapKey, subContentDataResourceView, 1066 templateRoot, locale, mimeTypeId, userLogin, fromDate, nullThruDatesOnly); 1067 } 1068 1069 1071 public static Map renderSubContentAsTextCache(GenericDelegator delegator, String contentId, Writer out, String mapKey, GenericValue subContentDataResourceView, 1072 Map templateRoot, Locale locale, String mimeTypeId, GenericValue userLogin, Timestamp fromDate, Boolean nullThruDatesOnly) throws GeneralException, IOException { 1073 1074 Map results = new HashMap (); 1075 if (subContentDataResourceView == null) { 1077 String contentAssocPredicateId = null; 1078 try { 1079 subContentDataResourceView = getSubContentCache(delegator, contentId, mapKey, userLogin, null, fromDate, nullThruDatesOnly, contentAssocPredicateId ); 1080 } catch(GenericEntityException e) { 1081 throw new GeneralException(e.getMessage()); 1082 } 1083 } 1084 results.put("view", subContentDataResourceView); 1085 if (subContentDataResourceView == null) { 1086 return results; 1088 } 1089 1090 String contentIdSub = (String ) subContentDataResourceView.get("contentId"); 1091 1092 if (templateRoot == null) { 1093 templateRoot = new HashMap (); 1094 } 1095 1096 1099 renderContentAsTextCache(delegator, contentIdSub, out, templateRoot, subContentDataResourceView, locale, mimeTypeId); 1100 1101 return results; 1102 } 1103 1104 public static Map renderContentAsText(GenericDelegator delegator, String contentId, Writer out, Map templateContext, GenericValue view, Locale locale, String mimeTypeId) throws GeneralException, IOException { 1105 Map results = new HashMap (); 1107 GenericValue content = null; 1108 1109 if (view == null) { 1110 if (contentId == null) { 1111 throw new IOException ("ContentId is null"); 1112 } 1113 try { 1114 List lst = delegator.findByAnd("SubContentDataResourceView", UtilMisc.toMap("contentId", contentId)); 1115 if (lst != null && lst.size() > 0) { 1116 view = (GenericValue) lst.get(0); 1117 } else { 1118 throw new IOException ("SubContentDataResourceView not found in renderSubContentAsText" + " for contentId=" + contentId); 1119 } 1120 } catch (GenericEntityException e) { 1121 throw new IOException (e.getMessage()); 1122 } 1123 } 1124 if (view != null) { 1125 Map contentMap = new HashMap (); 1126 try { 1127 SimpleMapProcessor.runSimpleMapProcessor("org/ofbiz/content/ContentManagementMapProcessors.xml", "contentIn", view, contentMap, new ArrayList (), locale); 1128 } catch (MiniLangException e) { 1129 throw new IOException (e.getMessage()); 1130 } 1131 content = delegator.makeValue("Content", contentMap); 1132 } 1133 1134 results.put("view", view); 1135 results.put("content", content); 1136 1137 if (locale != null) { 1138 String targetLocaleString = locale.toString(); 1139 String thisLocaleString = (String ) view.get("localeString"); 1140 thisLocaleString = (thisLocaleString != null) ? thisLocaleString : ""; 1141 if (targetLocaleString != null && !targetLocaleString.equalsIgnoreCase(thisLocaleString)) { 1142 view = findAlternateLocaleContent(delegator, view, locale); 1143 } 1144 } 1145 1146 String dataResourceId = null; 1148 try { 1149 dataResourceId = (String ) view.get("drDataResourceId"); 1150 } catch (Exception e) { 1151 dataResourceId = (String ) view.get("dataResourceId"); 1152 } 1153 if (templateContext == null) { 1154 templateContext = new HashMap (); 1155 } 1156 1157 if (UtilValidate.isNotEmpty(dataResourceId) || view != null) 1160 DataResourceWorker.renderDataResourceAsText(delegator, dataResourceId, out, templateContext, view, locale, mimeTypeId); 1161 1165 return results; 1166 } 1167 1168 public static String renderContentAsTextCache(GenericDelegator delegator, String contentId, Map templateContext, GenericValue view, Locale locale, String mimeTypeId) throws GeneralException, IOException { 1169 Writer outWriter = new StringWriter (); 1170 renderContentAsTextCache(delegator, contentId, outWriter, templateContext, view, locale, mimeTypeId); 1171 return outWriter.toString(); 1172 } 1173 1174 public static Map renderContentAsTextCache(GenericDelegator delegator, String contentId, Writer out, Map templateContext, GenericValue view, Locale locale, String mimeTypeId) throws GeneralException, IOException { 1175 1176 Map results = new HashMap (); 1177 1178 GenericValue content = null; 1179 1180 if (view == null) { 1181 if (contentId == null) { 1182 throw new GeneralException("ContentId is null"); 1183 } 1184 try { 1185 List lst = delegator.findByAndCache("SubContentDataResourceView", UtilMisc.toMap("contentId", contentId), null); 1186 if (lst != null && lst.size() > 0) { 1187 view = (GenericValue) lst.get(0); 1188 } else { 1189 throw new GeneralException("SubContentDataResourceView not found in renderSubContentAsText" + " for contentId=" + contentId); 1190 } 1191 } catch (GenericEntityException e) { 1192 throw new GeneralException(e.getMessage()); 1193 } 1194 } 1195 if (view != null) { 1196 Map contentMap = new HashMap (); 1197 try { 1198 SimpleMapProcessor.runSimpleMapProcessor("org/ofbiz/content/ContentManagementMapProcessors.xml", "contentIn", view, contentMap, new ArrayList (), locale); 1199 } catch (MiniLangException e) { 1200 throw new IOException (e.getMessage()); 1201 } 1202 content = delegator.makeValue("Content", contentMap); 1203 } 1204 1205 results.put("view", view); 1206 results.put("content", content); 1207 1208 if (locale != null) { 1209 String targetLocaleString = locale.toString(); 1210 String thisLocaleString = (String ) view.get("localeString"); 1211 thisLocaleString = (thisLocaleString != null) ? thisLocaleString : ""; 1212 if (UtilValidate.isNotEmpty(targetLocaleString) && !targetLocaleString.equalsIgnoreCase(thisLocaleString)) { 1215 GenericValue localeView = findAlternateLocaleContent(delegator, view, locale); 1216 if (localeView != null) 1217 view = localeView; 1218 } 1219 } 1220 1221 String templateDataResourceId = (String )view.get("templateDataResourceId"); 1222 1223 String dataResourceId = null; 1225 try { 1226 dataResourceId = (String ) view.get("drDataResourceId"); 1227 } catch (Exception e) { 1228 dataResourceId = (String ) view.get("dataResourceId"); 1229 view = null; } 1231 1232 if (templateContext == null) { 1233 templateContext = new HashMap (); 1234 } 1235 1236 1238 if (UtilValidate.isEmpty(templateDataResourceId)) { 1239 if (UtilValidate.isNotEmpty(dataResourceId) || view != null) 1240 DataResourceWorker.renderDataResourceAsTextCache(delegator, dataResourceId, out, templateContext, view, locale, mimeTypeId); 1241 1242 } else { 1243 if (UtilValidate.isNotEmpty(dataResourceId) || view != null) { 1244 StringWriter sw = new StringWriter (); 1245 DataResourceWorker.renderDataResourceAsTextCache(delegator, dataResourceId, sw, templateContext, view, locale, mimeTypeId); 1246 String s = sw.toString(); 1247 if (UtilValidate.isNotEmpty(s)) 1248 s = s.trim(); 1249 1251 if (Debug.infoOn()) Debug.logInfo("renderContentAsTextCache, dataResourceId(2):" + dataResourceId, ""); 1252 String reqdType = null; 1254 try { 1255 reqdType = DataResourceWorker.getDataResourceMimeType(delegator, dataResourceId, view); 1256 } catch(GenericEntityException e) { 1257 throw new GeneralException(e.getMessage()); 1258 } 1259 if (Debug.infoOn()) Debug.logInfo("renderContentAsTextCache, reqdType(2):" + reqdType, ""); 1260 if (UtilValidate.isNotEmpty(reqdType)) { 1261 if (reqdType.toLowerCase().indexOf("xml") >= 0) { 1262 StringReader sr = new StringReader (s); 1263 try { 1264 NodeModel nodeModel = NodeModel.parse(new InputSource (sr)); 1265 if (Debug.infoOn()) Debug.logInfo("renderTextAsStringCache, doc:" + nodeModel, ""); 1266 templateContext.put("doc", nodeModel); 1267 } catch(SAXException e) { 1268 throw new GeneralException(e.getMessage()); 1269 } catch(ParserConfigurationException e2) { 1270 throw new GeneralException(e2.getMessage()); 1271 } 1272 } else { 1273 templateContext.put("textData", sw.toString()); 1275 } 1276 } else { 1277 templateContext.put("textData", sw.toString()); 1278 } 1279 } 1280 DataResourceWorker.renderDataResourceAsTextCache(delegator, templateDataResourceId, out, templateContext, null, locale, mimeTypeId); 1281 } 1282 return results; 1283 } 1284 1285 public static Map buildPickContext(GenericDelegator delegator, String contentAssocTypeId, String assocContentId, String direction, GenericValue thisContent) throws GenericEntityException { 1286 1287 Map ctx = new HashMap (); 1288 ctx.put("contentAssocTypeId", contentAssocTypeId); 1289 ctx.put("contentId", assocContentId); 1290 String assocRelation = null; 1291 if (direction != null && direction.equalsIgnoreCase("From")) { 1293 ctx.put("contentIdFrom", assocContentId); 1294 assocRelation = "FromContent"; 1295 } else { 1296 ctx.put("contentIdTo", assocContentId); 1297 assocRelation = "ToContent"; 1298 } 1299 if (thisContent == null) 1300 thisContent = delegator.findByPrimaryKeyCache("Content", 1301 UtilMisc.toMap("contentId", assocContentId)); 1302 ctx.put("content", thisContent); 1303 List purposes = getPurposes(thisContent); 1304 ctx.put("purposes", purposes); 1305 List contentTypeAncestry = new ArrayList (); 1306 String contentTypeId = (String )thisContent.get("contentTypeId"); 1307 getContentTypeAncestry(delegator, contentTypeId, contentTypeAncestry); 1308 ctx.put("typeAncestry", contentTypeAncestry); 1309 List sections = getSections(thisContent); 1310 ctx.put("sections", sections); 1311 List topics = getTopics(thisContent); 1312 ctx.put("topics", topics); 1313 return ctx; 1315 } 1316 1317 public static void checkConditions(GenericDelegator delegator, Map trailNode, Map contentAssoc, Map whenMap) { 1318 1319 Map context = new HashMap (); 1320 GenericValue content = (GenericValue)trailNode.get("value"); 1321 String contentId = (String )trailNode.get("contentId"); 1322 if (contentAssoc == null && content != null && (content.getEntityName().indexOf("Assoc") >= 0)) { 1323 contentAssoc = delegator.makeValue("ContentAssoc", null); 1324 try { 1325 SimpleMapProcessor.runSimpleMapProcessor("org/ofbiz/content/ContentManagementMapProcessors.xml", "contentAssocIn", content, contentAssoc, new ArrayList (), Locale.getDefault()); 1327 context.put("contentAssocTypeId", contentAssoc.get("contentAssocTypeId")); 1328 context.put("contentAssocPredicateId", contentAssoc.get("contentAssocPredicateId")); 1329 context.put("mapKey", contentAssoc.get("mapKey")); 1330 } catch (MiniLangException e) { 1331 Debug.logError(e.getMessage(), module); 1332 } 1334 } else { 1335 context.put("contentAssocTypeId", null); 1336 context.put("contentAssocPredicateId", null); 1337 context.put("mapKey", null); 1338 } 1339 context.put("content", content); 1340 List purposes = getPurposes(content); 1341 context.put("purposes", purposes); 1342 List sections = getSections(content); 1343 context.put("sections", sections); 1344 List topics = getTopics(content); 1345 context.put("topics", topics); 1346 String contentTypeId = (String )content.get("contentTypeId"); 1347 List contentTypeAncestry = new ArrayList (); 1348 try { 1349 getContentTypeAncestry(delegator, contentTypeId, contentTypeAncestry); 1350 } catch(GenericEntityException e) { 1351 } 1352 context.put("typeAncestry", contentTypeAncestry); 1353 boolean isReturnBefore = checkReturnWhen(context, (String )whenMap.get("returnBeforePickWhen")); 1354 trailNode.put("isReturnBefore", new Boolean (isReturnBefore)); 1355 boolean isPick = checkWhen(context, (String )whenMap.get("pickWhen")); 1356 trailNode.put("isPick", new Boolean (isPick)); 1357 boolean isFollow = checkWhen(context, (String )whenMap.get("followWhen")); 1358 trailNode.put("isFollow", new Boolean (isFollow)); 1359 boolean isReturnAfter = checkReturnWhen(context, (String )whenMap.get("returnAfterPickWhen")); 1360 trailNode.put("isReturnAfter", new Boolean (isReturnAfter)); 1361 trailNode.put("checked", new Boolean (true)); 1362 1363 return; 1364 } 1365 1366 public static boolean booleanDataType(Object boolObj) { 1367 boolean bool = false; 1368 if (boolObj != null && ((Boolean )boolObj).booleanValue()) { 1369 bool = true; 1370 } 1371 return bool; 1372 } 1373 1374 1375 public static List prepTargetOperationList(Map context, String md) { 1376 1377 List targetOperationList = (List )context.get("targetOperationList"); 1378 String targetOperationString = (String )context.get("targetOperationString"); 1379 if (Debug.infoOn()) Debug.logInfo("in prepTargetOperationList, targetOperationString(0):" + targetOperationString, ""); 1380 if (UtilValidate.isNotEmpty(targetOperationString) ) { 1381 List opsFromString = StringUtil.split(targetOperationString, "|"); 1382 if (targetOperationList == null || targetOperationList.size() == 0) { 1383 targetOperationList = new ArrayList (); 1384 } 1385 targetOperationList.addAll(opsFromString); 1386 } 1387 if (targetOperationList == null || targetOperationList.size() == 0) { 1388 targetOperationList = new ArrayList (); 1389 if (UtilValidate.isEmpty(md)) 1390 md ="_CREATE"; 1391 targetOperationList.add("CONTENT" + md); 1392 } 1393 if (Debug.infoOn()) Debug.logInfo("in prepTargetOperationList, targetOperationList(0):" + targetOperationList, ""); 1394 return targetOperationList; 1395 } 1396 1397 1403 public static List prepContentPurposeList(Map context) { 1404 1405 List contentPurposeList = (List )context.get("contentPurposeList"); 1406 String contentPurposeString = (String )context.get("contentPurposeString"); 1407 if (Debug.infoOn()) Debug.logInfo("in prepContentPurposeList, contentPurposeString(0):" + contentPurposeString, ""); 1408 if (UtilValidate.isNotEmpty(contentPurposeString) ) { 1409 List purposesFromString = StringUtil.split(contentPurposeString, "|"); 1410 if (contentPurposeList == null || contentPurposeList.size() == 0) { 1411 contentPurposeList = new ArrayList (); 1412 } 1413 contentPurposeList.addAll(purposesFromString); 1414 } 1415 if (contentPurposeList == null || contentPurposeList.size() == 0) { 1416 contentPurposeList = new ArrayList (); 1417 } 1418 if (Debug.infoOn()) Debug.logInfo("in prepContentPurposeList, contentPurposeList(0):" + contentPurposeList, ""); 1419 return contentPurposeList; 1420 } 1421 1422 public static String prepPermissionErrorMsg(Map permResults) { 1423 1424 String permissionStatus = (String )permResults.get("permissionStatus"); 1425 String errorMessage = "Permission is denied." + permissionStatus; 1426 errorMessage += ServiceUtil.getErrorMessage(permResults); 1427 PermissionRecorder recorder = (PermissionRecorder)permResults.get("permissionRecorder"); 1428 Debug.logInfo("recorder(0):" + recorder, ""); 1429 if (recorder != null && recorder.isOn()) { 1430 String permissionMessage = recorder.toHtml(); 1431 errorMessage += " \n " + permissionMessage; 1433 } 1434 return errorMessage; 1435 } 1436 1437 public static List getContentAssocViewList(GenericDelegator delegator, String contentIdTo, String contentId, String contentAssocTypeId, String statusId, String privilegeEnumId) throws GenericEntityException { 1438 1439 List exprListAnd = new ArrayList (); 1440 1441 if (UtilValidate.isNotEmpty(contentIdTo)) { 1442 EntityExpr expr = new EntityExpr("caContentIdTo", EntityOperator.EQUALS, contentIdTo); 1443 exprListAnd.add(expr); 1444 } 1445 1446 if (UtilValidate.isNotEmpty(contentId)) { 1447 EntityExpr expr = new EntityExpr("contentId", EntityOperator.EQUALS, contentId); 1448 exprListAnd.add(expr); 1449 } 1450 1451 if (UtilValidate.isNotEmpty(contentAssocTypeId)) { 1452 EntityExpr expr = new EntityExpr("caContentAssocTypeId", EntityOperator.EQUALS, contentAssocTypeId); 1453 exprListAnd.add(expr); 1454 } 1455 1456 if (UtilValidate.isNotEmpty(statusId)) { 1457 EntityExpr expr = new EntityExpr("statusId", EntityOperator.EQUALS, statusId); 1458 exprListAnd.add(expr); 1459 } 1460 1461 if (UtilValidate.isNotEmpty(privilegeEnumId)) { 1462 EntityExpr expr = new EntityExpr("privilegeEnumId", EntityOperator.EQUALS, privilegeEnumId); 1463 exprListAnd.add(expr); 1464 } 1465 1466 EntityConditionList contentCondList = new EntityConditionList(exprListAnd, EntityOperator.AND); 1467 List contentList = delegator.findByCondition("ContentAssocDataResourceViewFrom", contentCondList, null, null); 1468 List filteredList = EntityUtil.filterByDate(contentList, UtilDateTime.nowTimestamp(), "caFromDate", "caThruDate", true); 1469 return filteredList; 1470 } 1471 1472 public static GenericValue getContentAssocViewFrom(GenericDelegator delegator, String contentIdTo, String contentId, String contentAssocTypeId, String statusId, String privilegeEnumId) throws GenericEntityException { 1473 1474 List filteredList = getContentAssocViewList(delegator, contentIdTo, contentId, contentAssocTypeId, statusId, privilegeEnumId); 1475 1476 GenericValue val = null; 1477 if (filteredList.size() > 0 ) { 1478 val = (GenericValue)filteredList.get(0); 1479 } 1480 return val; 1481 } 1482 1483 public static Map makeNode(GenericValue thisContent) { 1484 Map thisNode = null; 1485 if (thisContent == null) 1486 return thisNode; 1487 1488 thisNode = new HashMap (); 1489 thisNode.put("value", thisContent); 1490 String contentId = (String )thisContent.get("contentId"); 1491 thisNode.put("contentId", contentId); 1492 thisNode.put("contentTypeId", thisContent.get("contentTypeId")); 1493 thisNode.put("isReturnBeforePick", new Boolean (false)); 1494 thisNode.put("isReturnAfterPick", new Boolean (false)); 1495 thisNode.put("isPick", new Boolean (true)); 1496 thisNode.put("isFollow", new Boolean (true)); 1497 try { 1498 thisNode.put("contentAssocTypeId", thisContent.get("caContentAssocTypeId")); 1499 thisNode.put("mapKey", thisContent.get("caMapKey")); 1500 thisNode.put("fromDate", thisContent.get("caFromDate")); 1501 thisNode.put("contentAssocTypeId", thisContent.get("caContentAssocTypeId")); 1502 } catch(Exception e) { 1503 } 1505 return thisNode; 1506 } 1507 1508 1509 public static String nodeTrailToCsv(List nodeTrail) { 1510 1511 if (nodeTrail == null) 1512 return ""; 1513 StringBuffer csv = new StringBuffer (); 1514 Iterator it = nodeTrail.iterator(); 1515 while (it.hasNext()) { 1516 if (csv.length() > 0) 1517 csv.append(","); 1518 Map node = (Map )it.next(); 1519 if (node == null) 1520 break; 1521 1522 String contentId = (String )node.get("contentId"); 1523 csv.append(contentId); 1524 } 1525 return csv.toString(); 1526 } 1527 1528 public static List csvToList(String csv, GenericDelegator delegator) { 1529 1530 ArrayList outList = new ArrayList (); 1531 List contentIdList = StringUtil.split(csv, ","); 1532 GenericValue content = null; 1533 String contentId = null; 1534 String contentName = null; 1535 ArrayList values = null; 1536 Iterator it = contentIdList.iterator(); 1537 while (it.hasNext()) { 1538 contentId = (String )it.next(); 1539 try { 1540 content = delegator.findByPrimaryKeyCache("Content", UtilMisc.toMap("contentId", contentId)); 1541 } catch(GenericEntityException e) { 1542 Debug.logError(e.getMessage(), module); 1543 return new ArrayList (); 1544 } 1545 contentName = (String )content.get("contentName"); 1546 values = new ArrayList (); 1547 values.add(contentId); 1548 values.add(contentName); 1549 outList.add(values); 1550 } 1551 return outList; 1552 } 1553 1554 public static List csvToContentList(String csv, GenericDelegator delegator) { 1555 1556 List trail = new ArrayList (); 1557 if (csv == null) 1558 return trail; 1559 1560 ArrayList outList = new ArrayList (); 1561 List contentIdList = StringUtil.split(csv, ","); 1562 GenericValue content = null; 1563 String contentId = null; 1564 Iterator it = contentIdList.iterator(); 1565 while (it.hasNext()) { 1566 contentId = (String )it.next(); 1567 try { 1568 content = delegator.findByPrimaryKeyCache("Content", UtilMisc.toMap("contentId", contentId)); 1569 } catch(GenericEntityException e) { 1570 Debug.logError(e.getMessage(), module); 1571 return new ArrayList (); 1572 } 1573 trail.add(content); 1574 } 1575 return trail; 1576 } 1577 1578 public static List csvToTrail(String csv, GenericDelegator delegator) { 1579 1580 ArrayList trail = new ArrayList (); 1581 if (csv == null) 1582 return trail; 1583 1584 List contentList = csvToContentList(csv, delegator); 1585 GenericValue content = null; 1586 Iterator it = contentList.iterator(); 1587 while (it.hasNext()) { 1588 content = (GenericValue)it.next(); 1589 Map node = makeNode(content); 1590 trail.add(node); 1591 } 1592 return trail; 1593 } 1594 1595 public static String getMimeTypeId(GenericDelegator delegator, GenericValue view, Map ctx) { 1596 String mimeTypeId = (String )ctx.get("mimeTypeId"); 1598 if (UtilValidate.isEmpty(mimeTypeId) && view != null) { 1599 mimeTypeId = (String ) view.get("mimeTypeId"); 1600 String parentContentId = (String )ctx.get("contentId"); 1601 if (UtilValidate.isEmpty(mimeTypeId) && UtilValidate.isNotEmpty(parentContentId)) { try { 1603 GenericValue parentContent = delegator.findByPrimaryKey("Content", UtilMisc.toMap("contentId", parentContentId)); 1604 if (parentContent != null) { 1605 mimeTypeId = (String ) parentContent.get("mimeTypeId"); 1606 ctx.put("parentContent", parentContent); 1607 } 1608 } catch (GenericEntityException e) { 1609 Debug.logError(e.getMessage(), module); 1610 } 1612 } 1613 1614 } 1615 return mimeTypeId; 1616 } 1617 1618 1628 public static String determineMimeType(GenericDelegator delegator, 1629 GenericValue view, GenericValue parentContent, String contentId, 1630 String dataResourceId, String parentContentId) 1631 throws GenericEntityException { 1632 String mimeTypeId = null; 1633 1634 if (view != null) { 1635 mimeTypeId = (String ) view.get("mimeTypeId"); 1636 String drMimeTypeId = (String ) view.get("drMimeTypeId"); 1637 if (UtilValidate.isNotEmpty(drMimeTypeId)) { 1638 mimeTypeId = drMimeTypeId; 1639 } 1640 } 1641 1642 if (UtilValidate.isEmpty(mimeTypeId)) { 1643 if (UtilValidate.isNotEmpty(contentId) 1644 && UtilValidate.isNotEmpty(dataResourceId)) { 1645 view = delegator.findByPrimaryKey("SubContentDataResourceView", 1646 UtilMisc.toMap("contentId", contentId, 1647 "drDataResourceId", dataResourceId)); 1648 if (view != null) { 1649 mimeTypeId = (String ) view.get("mimeTypeId"); 1650 String drMimeTypeId = (String ) view.get("drMimeTypeId"); 1651 if (UtilValidate.isNotEmpty(drMimeTypeId)) { 1652 mimeTypeId = drMimeTypeId; 1653 } 1654 } 1655 } 1656 } 1657 1658 if (UtilValidate.isEmpty(mimeTypeId)) { 1659 if (parentContent != null) { 1660 mimeTypeId = (String ) parentContent.get("mimeTypeId"); 1661 } 1662 } 1663 1664 if (UtilValidate.isEmpty(mimeTypeId)) { 1665 if (UtilValidate.isNotEmpty(parentContentId)) { 1666 parentContent = delegator.findByPrimaryKey("Content", UtilMisc 1667 .toMap("contentId", contentId)); 1668 if (parentContent != null) { 1669 mimeTypeId = (String ) parentContent.get("mimeTypeId"); 1670 } 1671 } 1672 } 1673 1674 return mimeTypeId; 1675 } 1676 1677 public static String logMap(String lbl, Map map, int indent) { 1678 String sep = ":"; 1679 String eol = "\n"; 1680 String spc = ""; 1681 for (int i=0; i<indent; i++) { 1682 spc += " "; 1683 } 1684 String s = (lbl != null) ? lbl : ""; 1685 s += "=" + indent + "==>" + eol; 1686 Set keySet = map.keySet(); 1687 Iterator it = keySet.iterator(); 1688 while (it.hasNext()) { 1689 String key = (String )it.next(); 1690 if ("request response session".indexOf(key) < 0) { 1691 Object obj = map.get(key); 1692 s += spc + key + sep;; 1693 if (obj instanceof GenericValue) { 1694 GenericValue gv = (GenericValue)obj; 1695 GenericPK pk = gv.getPrimaryKey(); 1696 s += logMap("GMAP[" + key + " name:" + pk.getEntityName()+ "]", pk, indent + 1); 1697 } else if (obj instanceof List ) { 1698 s += logList("LIST[" + ((List )obj).size() + "]", (List )obj, indent + 1); 1699 } else if (obj instanceof Map ) { 1700 s += logMap("MAP[" + key + "]", (Map )obj, indent + 1); 1701 } else if (obj != null) { 1702 s += obj + sep + obj.getClass() + eol; 1703 } else { 1704 s += eol; 1705 } 1706 } 1707 } 1708 return s + eol + eol; 1709 } 1710 1711 public static String logList(String lbl, List lst, int indent) { 1712 1713 String sep = ":"; 1714 String eol = "\n"; 1715 String spc = ""; 1716 if (lst == null) 1717 return ""; 1718 int sz = lst.size(); 1719 for (int i=0; i<indent; i++) 1720 spc += " "; 1721 String s = (lbl != null) ? lbl : ""; 1722 s += "=" + indent + "==> sz:" + sz + eol; 1723 Iterator it = lst.iterator(); 1724 while (it.hasNext()) { 1725 Object obj = it.next(); 1726 s += spc; 1727 if (obj instanceof GenericValue) { 1728 GenericValue gv = (GenericValue)obj; 1729 GenericPK pk = gv.getPrimaryKey(); 1730 s += logMap("MAP[name:" + pk.getEntityName() + "]", pk, indent + 1); 1731 } else if (obj instanceof List ) { 1732 s += logList("LIST[" + ((List )obj).size() + "]", (List )obj, indent + 1); 1733 } else if (obj instanceof Map ) { 1734 s += logMap("MAP[]", (Map )obj, indent + 1); 1735 } else if (obj != null) { 1736 s += obj + sep + obj.getClass() + eol; 1737 } else { 1738 s += eol; 1739 } 1740 } 1741 return s + eol + eol; 1742 } 1743 1744 public static void traceNodeTrail(String lbl, List nodeTrail) { 1745 1772 return; 1773 } 1774} 1775 | Popular Tags |