1 24 package org.ofbiz.content.content; 25 26 import java.io.ByteArrayOutputStream ; 27 import java.io.FileNotFoundException ; 28 import java.io.IOException ; 29 import java.io.StringWriter ; 30 import java.io.Writer ; 31 import java.sql.Timestamp ; 32 import java.util.ArrayList ; 33 import java.util.HashMap ; 34 import java.util.Iterator ; 35 import java.util.List ; 36 import java.util.Locale ; 37 import java.util.Map ; 38 import java.util.Set ; 39 40 import javax.xml.parsers.ParserConfigurationException ; 41 42 import org.apache.avalon.framework.logger.Log4JLogger; 43 import org.apache.avalon.framework.logger.Logger; 44 import org.apache.fop.apps.Driver; 45 import org.apache.fop.image.FopImageFactory; 46 import org.apache.fop.messaging.MessageHandler; 47 import org.apache.fop.tools.DocumentInputSource; 48 import org.ofbiz.base.util.Debug; 49 import org.ofbiz.base.util.GeneralException; 50 import org.ofbiz.base.util.StringUtil; 51 import org.ofbiz.base.util.UtilDateTime; 52 import org.ofbiz.base.util.UtilFormatOut; 53 import org.ofbiz.base.util.UtilHttp; 54 import org.ofbiz.base.util.UtilMisc; 55 import org.ofbiz.base.util.UtilValidate; 56 import org.ofbiz.base.util.UtilXml; 57 import org.ofbiz.entity.GenericDelegator; 58 import org.ofbiz.entity.GenericEntityException; 59 import org.ofbiz.entity.GenericValue; 60 import org.ofbiz.entity.condition.EntityConditionList; 61 import org.ofbiz.entity.condition.EntityExpr; 62 import org.ofbiz.entity.condition.EntityOperator; 63 import org.ofbiz.entity.util.ByteWrapper; 64 import org.ofbiz.entity.util.EntityUtil; 65 import org.ofbiz.security.Security; 66 import org.ofbiz.service.DispatchContext; 67 import org.ofbiz.service.GenericServiceException; 68 import org.ofbiz.service.LocalDispatcher; 69 import org.ofbiz.service.ModelService; 70 import org.ofbiz.service.ServiceUtil; 71 import org.w3c.dom.Document ; 72 import org.xml.sax.InputSource ; 73 import org.xml.sax.SAXException ; 74 75 84 public class ContentServices { 85 86 public static final String module = ContentServices.class.getName(); 87 88 91 public static Map findRelatedContent(DispatchContext dctx, Map context) { 92 LocalDispatcher dispatcher = dctx.getDispatcher(); 93 Map results = new HashMap (); 94 95 GenericValue currentContent = (GenericValue) context.get("currentContent"); 96 String fromDate = (String ) context.get("fromDate"); 97 String thruDate = (String ) context.get("thruDate"); 98 String toFrom = (String ) context.get("toFrom"); 99 if (toFrom == null) { 100 toFrom = "TO"; 101 } else { 102 toFrom = toFrom.toUpperCase(); 103 } 104 105 List assocTypes = (List ) context.get("contentAssocTypeList"); 106 List targetOperations = (List ) context.get("targetOperationList"); 107 List contentList = null; 108 List contentTypes = (List ) context.get("contentTypeList"); 109 110 try { 111 contentList = ContentWorker.getAssociatedContent(currentContent, toFrom, assocTypes, contentTypes, fromDate, thruDate); 112 } catch (GenericEntityException e) { 113 return ServiceUtil.returnError("Error getting associated content: " + e.toString()); 114 } 115 116 if (targetOperations == null || targetOperations.isEmpty()) { 117 results.put("contentList", contentList); 118 return results; 119 } 120 121 Map serviceInMap = new HashMap (); 122 serviceInMap.put("userLogin", context.get("userLogin")); 123 serviceInMap.put("targetOperationList", targetOperations); 124 serviceInMap.put("entityOperation", context.get("entityOperation")); 125 126 List permittedList = new ArrayList (); 127 Iterator it = contentList.iterator(); 128 Map permResults = null; 129 while (it.hasNext()) { 130 GenericValue content = (GenericValue) it.next(); 131 serviceInMap.put("currentContent", content); 132 try { 133 permResults = dispatcher.runSync("checkContentPermission", serviceInMap); 134 } catch (GenericServiceException e) { 135 Debug.logError(e, "Problem checking permissions", "ContentServices"); 136 return ServiceUtil.returnError("Problem checking permissions"); 137 } 138 139 String permissionStatus = (String ) permResults.get("permissionStatus"); 140 if (permissionStatus != null && permissionStatus.equalsIgnoreCase("granted")) { 141 permittedList.add(content); 142 } 143 144 } 145 146 results.put("contentList", permittedList); 147 return results; 148 } 149 152 public static Map findContentParents(DispatchContext dctx, Map context) { 153 HashMap results = new HashMap (); 154 List parentList = new ArrayList (); 155 results.put("parentList", parentList); 156 LocalDispatcher dispatcher = dctx.getDispatcher(); 157 String contentId = (String )context.get("contentId"); 158 String contentAssocTypeId = (String )context.get("contentAssocTypeId"); 159 String direction = (String )context.get("direction"); 160 if (UtilValidate.isEmpty(direction)) direction="To"; 161 Map traversMap = new HashMap (); 162 traversMap.put("contentId", contentId); 163 traversMap.put("direction", direction); 164 traversMap.put("contentAssocTypeId", contentAssocTypeId); 165 try { 166 Map thisResults = dispatcher.runSync("traverseContent", traversMap); 167 String errorMsg = ServiceUtil.getErrorMessage(thisResults); 168 if (UtilValidate.isNotEmpty(errorMsg) ) { 169 Debug.logError( "Problem in traverseContent. " + errorMsg, module); 170 return ServiceUtil.returnError(errorMsg); 171 } 172 Map nodeMap = (Map )thisResults.get("nodeMap"); 173 walkParentTree(nodeMap, parentList); 174 } catch (GenericServiceException e) { 175 return ServiceUtil.returnFailure(e.getMessage()); 176 } 177 return results; 178 } 179 180 private static void walkParentTree(Map nodeMap, List parentList) { 181 List kids = (List )nodeMap.get("kids"); 182 if (kids == null || kids.size() == 0) { 183 parentList.add(nodeMap.get("contentId")); 184 } else { 185 Iterator iter = kids.iterator(); 186 while (iter.hasNext()) { 187 Map node = (Map ) iter.next(); 188 walkParentTree(node, parentList); 189 } 190 } 191 } 192 195 public static Map traverseContent(DispatchContext dctx, Map context) { 196 GenericDelegator delegator = dctx.getDelegator(); 197 HashMap results = new HashMap (); 198 199 String contentId = (String ) context.get("contentId"); 200 String direction = (String ) context.get("direction"); 201 if (direction != null && direction.equalsIgnoreCase("From")) { 202 direction = "From"; 203 } else { 204 direction = "To"; 205 } 206 207 if (contentId == null) { 208 contentId = "PUBLISH_ROOT"; 209 } 210 211 GenericValue content = null; 212 try { 213 content = delegator.findByPrimaryKey("Content", UtilMisc.toMap("contentId", contentId)); 214 } catch (GenericEntityException e) { 215 System.out.println("Entity Error:" + e.getMessage()); 216 return ServiceUtil.returnError("Error in retrieving Content. " + e.getMessage()); 217 } 218 219 String fromDateStr = (String ) context.get("fromDateStr"); 220 String thruDateStr = (String ) context.get("thruDateStr"); 221 Timestamp fromDate = null; 222 if (fromDateStr != null && fromDateStr.length() > 0) { 223 fromDate = UtilDateTime.toTimestamp(fromDateStr); 224 } 225 226 Timestamp thruDate = null; 227 if (thruDateStr != null && thruDateStr.length() > 0) { 228 thruDate = UtilDateTime.toTimestamp(thruDateStr); 229 } 230 231 Map whenMap = new HashMap (); 232 whenMap.put("followWhen", context.get("followWhen")); 233 whenMap.put("pickWhen", context.get("pickWhen")); 234 whenMap.put("returnBeforePickWhen", context.get("returnBeforePickWhen")); 235 whenMap.put("returnAfterPickWhen", context.get("returnAfterPickWhen")); 236 237 String startContentAssocTypeId = (String ) context.get("contentAssocTypeId"); 238 if (startContentAssocTypeId != null) { 239 startContentAssocTypeId = "PUBLISH"; 240 } 241 242 Map nodeMap = new HashMap (); 243 List pickList = new ArrayList (); 244 ContentWorker.traverse(delegator, content, fromDate, thruDate, whenMap, 0, nodeMap, startContentAssocTypeId, pickList, direction); 245 246 results.put("nodeMap", nodeMap); 247 results.put("pickList", pickList); 248 return results; 249 } 250 251 255 public static Map createContent(DispatchContext dctx, Map context) { 256 265 266 Map result = createContentMethod(dctx, context); 267 return result; 268 } 269 270 274 public static Map createContentMethod(DispatchContext dctx, Map context) { 275 context.put("entityOperation", "_CREATE"); 276 List targetOperationList = ContentWorker.prepTargetOperationList(context, "_CREATE"); 277 if (Debug.infoOn()) Debug.logInfo("in createContentMethod, targetOperationList: " + targetOperationList, null); 278 279 List contentPurposeList = ContentWorker.prepContentPurposeList(context); 280 context.put("targetOperationList", targetOperationList); 281 context.put("contentPurposeList", contentPurposeList); 282 283 Timestamp nowTimestamp = UtilDateTime.nowTimestamp(); 284 Map result = new HashMap (); 285 GenericDelegator delegator = dctx.getDelegator(); 286 LocalDispatcher dispatcher = dctx.getDispatcher(); 287 String contentId = (String ) context.get("contentId"); 288 290 if (UtilValidate.isEmpty(contentId)) { 291 contentId = delegator.getNextSeqId("Content"); 292 } 293 294 GenericValue content = delegator.makeValue("Content", UtilMisc.toMap("contentId", contentId)); 295 content.setNonPKFields(context); 296 297 GenericValue userLogin = (GenericValue) context.get("userLogin"); 298 String userLoginId = (String ) userLogin.get("userLoginId"); 299 300 content.put("createdByUserLogin", userLoginId); 301 content.put("lastModifiedByUserLogin", userLoginId); 302 content.put("createdDate", nowTimestamp); 303 content.put("lastModifiedDate", nowTimestamp); 304 305 context.put("currentContent", content); 306 if (Debug.infoOn()) Debug.logInfo("in createContentMethod, context: " + context, null); 307 308 Map permResults = ContentWorker.callContentPermissionCheckResult(delegator, dispatcher, context); 309 String permissionStatus = (String ) permResults.get("permissionStatus"); 310 if (permissionStatus != null && permissionStatus.equalsIgnoreCase("granted")) { 311 try { 312 content.create(); 313 } catch (GenericEntityException e) { 314 return ServiceUtil.returnError(e.getMessage()); 315 } catch (Exception e2) { 316 return ServiceUtil.returnError(e2.getMessage()); 317 } 318 319 result.put("contentId", contentId); 320 } else { 321 String errorMsg = (String ) permResults.get(ModelService.ERROR_MESSAGE); 322 result.put(ModelService.ERROR_MESSAGE, errorMsg); 323 return ServiceUtil.returnFailure(errorMsg); 324 } 325 326 context.remove("currentContent"); 327 return result; 328 } 329 330 334 public static Map createContentAssoc(DispatchContext dctx, Map context) { 335 context.put("entityOperation", "_CREATE"); 336 List targetOperationList = ContentWorker.prepTargetOperationList(context, "_CREATE"); 337 338 List contentPurposeList = ContentWorker.prepContentPurposeList(context); 339 context.put("targetOperationList", targetOperationList); 340 context.put("contentPurposeList", contentPurposeList); 341 context.put("skipPermissionCheck", null); 342 343 Map result = null; 344 try { 345 result = createContentAssocMethod(dctx, context); 346 } catch (GenericServiceException e) { 347 return ServiceUtil.returnError(e.getMessage()); 348 } catch (GenericEntityException e2) { 349 return ServiceUtil.returnError(e2.getMessage()); 350 } catch (Exception e3) { 351 return ServiceUtil.returnError(e3.getMessage()); 352 } 353 return result; 354 } 355 356 360 public static Map createContentAssocMethod(DispatchContext dctx, Map context) throws GenericServiceException, GenericEntityException { 361 List targetOperationList = ContentWorker.prepTargetOperationList(context, "_CREATE"); 362 List contentPurposeList = ContentWorker.prepContentPurposeList(context); 363 context.put("targetOperationList", targetOperationList); 364 context.put("contentPurposeList", contentPurposeList); 365 366 GenericDelegator delegator = dctx.getDelegator(); 367 LocalDispatcher dispatcher = dctx.getDispatcher(); 368 Map result = new HashMap (); 369 370 String contentIdFrom = (String ) context.get("contentIdFrom"); 373 String contentIdTo = (String ) context.get("contentIdTo"); 374 String contentId = (String ) context.get("contentId"); 375 int contentIdCount = 0; 376 if (UtilValidate.isNotEmpty(contentIdFrom)) 377 contentIdCount++; 378 if (UtilValidate.isNotEmpty(contentIdTo)) 379 contentIdCount++; 380 if (UtilValidate.isNotEmpty(contentId)) 381 contentIdCount++; 382 if (contentIdCount < 2) { 383 Debug.logError("Not 2 out of ContentId/To/From.", "ContentServices"); 384 return ServiceUtil.returnError("Not 2 out of ContentId/To/From"); 385 } 386 387 if (UtilValidate.isNotEmpty(contentIdFrom)) { 388 if (UtilValidate.isEmpty(contentIdTo)) 389 contentIdTo = contentId; 390 } 391 if (UtilValidate.isNotEmpty(contentIdTo)) { 392 if (UtilValidate.isEmpty(contentIdFrom)) 393 contentIdFrom = contentId; 394 } 395 396 420 421 GenericValue contentAssoc = delegator.makeValue("ContentAssoc", new HashMap ()); 422 contentAssoc.put("contentId", contentIdFrom); 423 contentAssoc.put("contentIdTo", contentIdTo); 424 contentAssoc.put("contentAssocTypeId", context.get("contentAssocTypeId")); 425 contentAssoc.put("contentAssocPredicateId", context.get("contentAssocPredicateIdFrom")); 426 contentAssoc.put("dataSourceId", context.get("dataSourceId")); 427 428 Timestamp fromDate = (Timestamp ) context.get("fromDate"); 429 if (fromDate == null) { 430 contentAssoc.put("fromDate", UtilDateTime.nowTimestamp()); 431 } else { 432 contentAssoc.put("fromDate", fromDate); 433 } 434 435 Timestamp thruDate = (Timestamp ) context.get("thruDate"); 436 if (thruDate == null) { 437 contentAssoc.put("thruDate", null); 438 } else { 439 contentAssoc.put("thruDate", thruDate); 440 } 441 442 contentAssoc.put("sequenceNum", context.get("sequenceNum")); 443 contentAssoc.put("mapKey", context.get("mapKey")); 444 445 String upperCoordinateStr = (String ) context.get("upperCoordinate"); 446 if (UtilValidate.isEmpty(upperCoordinateStr)) { 447 contentAssoc.put("upperCoordinate", null); 448 } else { 449 contentAssoc.put("upperCoordinate", upperCoordinateStr); 450 } 451 452 String leftCoordinateStr = (String ) context.get("leftCoordinate"); 453 if (UtilValidate.isEmpty(leftCoordinateStr)) { 454 contentAssoc.put("leftCoordinate", null); 455 } else { 456 contentAssoc.put("leftCoordinate", leftCoordinateStr); 457 } 458 459 GenericValue userLogin = (GenericValue) context.get("userLogin"); 460 String userLoginId = (String ) userLogin.get("userLoginId"); 461 String createdByUserLogin = userLoginId; 462 String lastModifiedByUserLogin = userLoginId; 463 464 Timestamp createdDate = UtilDateTime.nowTimestamp(); 465 Timestamp lastModifiedDate = UtilDateTime.nowTimestamp(); 466 467 contentAssoc.put("createdByUserLogin", createdByUserLogin); 468 contentAssoc.put("lastModifiedByUserLogin", lastModifiedByUserLogin); 469 contentAssoc.put("createdDate", createdDate); 470 contentAssoc.put("lastModifiedDate", lastModifiedDate); 471 472 Map serviceInMap = new HashMap (); 473 String permissionStatus = null; 474 serviceInMap.put("userLogin", context.get("userLogin")); 475 serviceInMap.put("targetOperationList", targetOperationList); 476 serviceInMap.put("contentPurposeList", contentPurposeList); 477 serviceInMap.put("entityOperation", context.get("entityOperation")); 478 serviceInMap.put("contentAssocPredicateId", context.get("contentAssocPredicateId")); 479 serviceInMap.put("contentIdTo", contentIdTo); 480 serviceInMap.put("contentIdFrom", contentIdFrom); 481 serviceInMap.put("statusId", context.get("statusId")); 482 serviceInMap.put("privilegeEnumId", context.get("privilegeEnumId")); 483 serviceInMap.put("roleTypeList", context.get("roleTypeList")); 484 serviceInMap.put("displayFailCond", context.get("displayFailCond")); 485 486 Map permResults = null; 487 permResults = dispatcher.runSync("checkAssocPermission", serviceInMap); 488 permissionStatus = (String ) permResults.get("permissionStatus"); 489 490 if (permissionStatus != null && permissionStatus.equals("granted")) { 491 contentAssoc.create(); 492 } else { 493 String errorMsg = (String )permResults.get(ModelService.ERROR_MESSAGE); 494 result.put(ModelService.ERROR_MESSAGE, errorMsg); 495 return ServiceUtil.returnFailure(errorMsg); 496 } 497 498 result.put("contentIdTo", contentIdTo); 499 result.put("contentIdFrom", contentIdFrom); 500 result.put("fromDate", contentAssoc.get("fromDate")); 501 result.put("contentAssocTypeId", contentAssoc.get("contentAssocTypeId")); 502 505 return result; 506 } 507 508 511 public static Map updateContent(DispatchContext dctx, Map context) { 512 context.put("entityOperation", "_UPDATE"); 513 List targetOperationList = ContentWorker.prepTargetOperationList(context, "_UPDATE"); 514 515 List contentPurposeList = ContentWorker.prepContentPurposeList(context); 516 context.put("targetOperationList", targetOperationList); 517 context.put("contentPurposeList", contentPurposeList); 518 context.put("skipPermissionCheck", null); 519 520 Map result = updateContentMethod(dctx, context); 521 return result; 522 } 523 524 528 public static Map updateContentMethod(DispatchContext dctx, Map context) { 529 GenericDelegator delegator = dctx.getDelegator(); 530 LocalDispatcher dispatcher = dctx.getDispatcher(); 531 Map result = new HashMap (); 532 533 context.put("entityOperation", "_UPDATE"); 534 List targetOperationList = ContentWorker.prepTargetOperationList(context, "_UPDATE"); 535 536 List contentPurposeList = ContentWorker.prepContentPurposeList(context); 537 context.put("targetOperationList", targetOperationList); 538 context.put("contentPurposeList", contentPurposeList); 539 540 GenericValue content = null; 541 String contentId = (String ) context.get("contentId"); 543 try { 544 content = delegator.findByPrimaryKey("Content", UtilMisc.toMap("contentId", contentId)); 545 } catch (GenericEntityException e) { 546 Debug.logWarning(e, module); 547 return ServiceUtil.returnError("content.update.read_failure" + e.getMessage()); 548 } 549 context.put("currentContent", content); 550 551 Map permResults = ContentWorker.callContentPermissionCheckResult(delegator, dispatcher, context); 552 String permissionStatus = (String ) permResults.get("permissionStatus"); 553 if (permissionStatus != null && permissionStatus.equalsIgnoreCase("granted")) { 554 GenericValue userLogin = (GenericValue) context.get("userLogin"); 555 String userLoginId = (String ) userLogin.get("userLoginId"); 556 String lastModifiedByUserLogin = userLoginId; 557 Timestamp lastModifiedDate = UtilDateTime.nowTimestamp(); 558 559 content.setNonPKFields(context); 560 content.put("lastModifiedByUserLogin", lastModifiedByUserLogin); 561 content.put("lastModifiedDate", lastModifiedDate); 562 try { 563 content.store(); 564 } catch (GenericEntityException e) { 565 return ServiceUtil.returnError(e.getMessage()); 566 } 567 } else { 568 String errorMsg = ContentWorker.prepPermissionErrorMsg(permResults); 569 return ServiceUtil.returnError(errorMsg); 570 } 571 572 return result; 573 } 574 575 579 public static Map updateContentAssoc(DispatchContext dctx, Map context) { 580 context.put("entityOperation", "_UPDATE"); 581 List targetOperationList = ContentWorker.prepTargetOperationList(context, "_UPDATE"); 582 583 List contentPurposeList = ContentWorker.prepContentPurposeList(context); 584 context.put("targetOperationList", targetOperationList); 585 context.put("contentPurposeList", contentPurposeList); 586 context.put("skipPermissionCheck", null); 587 588 Map result = updateContentAssocMethod(dctx, context); 589 return result; 590 } 591 592 596 public static Map updateContentAssocMethod(DispatchContext dctx, Map context) { 597 GenericDelegator delegator = dctx.getDelegator(); 598 LocalDispatcher dispatcher = dctx.getDispatcher(); 599 Map result = new HashMap (); 600 601 context.put("entityOperation", "_UPDATE"); 602 List targetOperationList = ContentWorker.prepTargetOperationList(context, "_UPDATE"); 603 604 List contentPurposeList = ContentWorker.prepContentPurposeList(context); 605 context.put("targetOperationList", targetOperationList); 606 context.put("contentPurposeList", contentPurposeList); 607 608 String contentIdFrom = (String ) context.get("contentId"); 611 String contentIdTo = (String ) context.get("contentIdTo"); 612 String contentId = (String ) context.get("contentId"); 613 String contentAssocTypeId = (String ) context.get("contentAssocTypeId"); 614 Timestamp fromDate = (Timestamp ) context.get("fromDate"); 615 616 GenericValue contentAssoc = null; 617 try { 618 contentAssoc = delegator.findByPrimaryKey("ContentAssoc", UtilMisc.toMap("contentId", contentId, "contentIdTo", contentIdTo, "contentAssocTypeId", contentAssocTypeId, "fromDate", fromDate)); 619 } catch (GenericEntityException e) { 620 System.out.println("Entity Error:" + e.getMessage()); 621 return ServiceUtil.returnError("Error in retrieving Content. " + e.getMessage()); 622 } 623 if (contentAssoc == null) { 624 return ServiceUtil.returnError("Error in updating ContentAssoc. Entity is null."); 625 } 626 627 contentAssoc.put("contentAssocPredicateId", context.get("contentAssocPredicateId")); 628 contentAssoc.put("dataSourceId", context.get("dataSourceId")); 629 contentAssoc.set("thruDate", context.get("thruDate")); 630 contentAssoc.set("sequenceNum", context.get("sequenceNum")); 631 contentAssoc.put("mapKey", context.get("mapKey")); 632 633 String upperCoordinateStr = (String ) context.get("upperCoordinate"); 634 if (UtilValidate.isEmpty(upperCoordinateStr)) { 635 contentAssoc.put("upperCoordinate", null); 636 } else { 637 contentAssoc.setString("upperCoordinate", upperCoordinateStr); 638 } 639 640 String leftCoordinateStr = (String ) context.get("leftCoordinate"); 641 if (UtilValidate.isEmpty(leftCoordinateStr)) { 642 contentAssoc.put("leftCoordinate", null); 643 } else { 644 contentAssoc.setString("leftCoordinate", leftCoordinateStr); 645 } 646 647 GenericValue userLogin = (GenericValue) context.get("userLogin"); 648 String userLoginId = (String ) userLogin.get("userLoginId"); 649 String lastModifiedByUserLogin = userLoginId; 650 Timestamp lastModifiedDate = UtilDateTime.nowTimestamp(); 651 contentAssoc.put("lastModifiedByUserLogin", lastModifiedByUserLogin); 652 contentAssoc.put("lastModifiedDate", lastModifiedDate); 653 654 String permissionStatus = null; 655 Map serviceInMap = new HashMap (); 656 serviceInMap.put("userLogin", context.get("userLogin")); 657 serviceInMap.put("targetOperationList", targetOperationList); 658 serviceInMap.put("contentPurposeList", contentPurposeList); 659 serviceInMap.put("entityOperation", context.get("entityOperation")); 660 serviceInMap.put("contentIdTo", contentIdTo); 661 serviceInMap.put("contentIdFrom", contentIdFrom); 662 663 Map permResults = null; 664 try { 665 permResults = dispatcher.runSync("checkAssocPermission", serviceInMap); 666 } catch (GenericServiceException e) { 667 Debug.logError(e, "Problem checking permissions", "ContentServices"); 668 return ServiceUtil.returnError("Problem checking association permissions"); 669 } 670 permissionStatus = (String ) permResults.get("permissionStatus"); 671 672 if (permissionStatus != null && permissionStatus.equals("granted")) { 673 try { 674 contentAssoc.store(); 675 } catch (GenericEntityException e) { 676 return ServiceUtil.returnError(e.getMessage()); 677 } 678 } else { 679 String errorMsg = ContentWorker.prepPermissionErrorMsg(permResults); 680 return ServiceUtil.returnError(errorMsg); 681 } 682 683 return result; 684 } 685 686 690 public static Map deactivateContentAssoc(DispatchContext dctx, Map context) { 691 context.put("entityOperation", "_UPDATE"); 692 List targetOperationList = ContentWorker.prepTargetOperationList(context, "_UPDATE"); 693 694 List contentPurposeList = ContentWorker.prepContentPurposeList(context); 695 context.put("targetOperationList", targetOperationList); 696 context.put("contentPurposeList", contentPurposeList); 697 context.put("skipPermissionCheck", null); 698 699 Map result = deactivateContentAssocMethod(dctx, context); 700 return result; 701 } 702 703 707 public static Map deactivateContentAssocMethod(DispatchContext dctx, Map context) { 708 GenericDelegator delegator = dctx.getDelegator(); 709 LocalDispatcher dispatcher = dctx.getDispatcher(); 710 Map result = new HashMap (); 711 712 context.put("entityOperation", "_UPDATE"); 713 List targetOperationList = ContentWorker.prepTargetOperationList(context, "_UPDATE"); 714 715 List contentPurposeList = ContentWorker.prepContentPurposeList(context); 716 context.put("targetOperationList", targetOperationList); 717 context.put("contentPurposeList", contentPurposeList); 718 719 GenericValue pk = delegator.makeValue("ContentAssoc",null); 720 pk.setAllFields(context, false, null, new Boolean (true)); 721 pk.setAllFields(context, false, "ca", new Boolean (true)); 722 728 GenericValue contentAssoc = null; 729 try { 730 contentAssoc = delegator.findByPrimaryKey("ContentAssoc", pk); 732 } catch (GenericEntityException e) { 733 System.out.println("Entity Error:" + e.getMessage()); 734 return ServiceUtil.returnError("Error in retrieving Content. " + e.getMessage()); 735 } 736 737 if (contentAssoc == null) { 738 return ServiceUtil.returnError("Error in deactivating ContentAssoc. Entity is null."); 739 } 740 741 GenericValue userLogin = (GenericValue) context.get("userLogin"); 742 String userLoginId = (String ) userLogin.get("userLoginId"); 743 String lastModifiedByUserLogin = userLoginId; 744 Timestamp lastModifiedDate = UtilDateTime.nowTimestamp(); 745 contentAssoc.put("lastModifiedByUserLogin", lastModifiedByUserLogin); 746 contentAssoc.put("lastModifiedDate", lastModifiedDate); 747 contentAssoc.put("thruDate", UtilDateTime.nowTimestamp()); 748 749 String permissionStatus = null; 750 Map serviceInMap = new HashMap (); 751 serviceInMap.put("userLogin", context.get("userLogin")); 752 serviceInMap.put("targetOperationList", targetOperationList); 753 serviceInMap.put("contentPurposeList", contentPurposeList); 754 serviceInMap.put("entityOperation", context.get("entityOperation")); 755 serviceInMap.put("contentIdTo", contentAssoc.get("contentIdTo")); 756 serviceInMap.put("contentIdFrom", contentAssoc.get("contentId")); 757 758 Map permResults = null; 759 try { 760 permResults = dispatcher.runSync("checkAssocPermission", serviceInMap); 761 } catch (GenericServiceException e) { 762 Debug.logError(e, "Problem checking permissions", "ContentServices"); 763 return ServiceUtil.returnError("Problem checking association permissions"); 764 } 765 permissionStatus = (String ) permResults.get("permissionStatus"); 766 767 if (permissionStatus != null && permissionStatus.equals("granted")) { 768 try { 769 contentAssoc.store(); 770 } catch (GenericEntityException e) { 771 return ServiceUtil.returnError(e.getMessage()); 772 } 773 } else { 774 String errorMsg = ContentWorker.prepPermissionErrorMsg(permResults); 775 return ServiceUtil.returnError(errorMsg); 776 } 777 778 return result; 779 } 780 781 784 public static Map deactivateAssocs(DispatchContext dctx, Map context) { 785 GenericDelegator delegator = dctx.getDelegator(); 786 String contentIdTo = (String ) context.get("contentIdTo"); 787 String mapKey = (String ) context.get("mapKey"); 788 String contentAssocTypeId = (String ) context.get("contentAssocTypeId"); 789 String activeContentId = (String ) context.get("activeContentId"); 790 String contentId = (String ) context.get("contentId"); 791 Timestamp fromDate = (Timestamp ) context.get("fromDate"); 792 Timestamp nowTimestamp = UtilDateTime.nowTimestamp(); 793 String sequenceNum = null; 794 Map results = new HashMap (); 795 796 try { 797 GenericValue activeAssoc = null; 798 if (fromDate != null) { 799 activeAssoc = delegator.findByPrimaryKey("ContentAssoc", UtilMisc.toMap("contentId", activeContentId, "contentIdTo", contentIdTo, "fromDate", fromDate, "contentAssocTypeId", contentAssocTypeId)); 800 if (activeAssoc == null) { 801 return ServiceUtil.returnError("No association found for contentId=" + activeContentId + " and contentIdTo=" + contentIdTo 802 + " and contentAssocTypeId=" + contentAssocTypeId + " and fromDate=" + fromDate); 803 } 804 sequenceNum = (String ) activeAssoc.get("sequenceNum"); 805 } 806 807 List exprList = new ArrayList (); 808 exprList.add(new EntityExpr("mapKey", EntityOperator.EQUALS, mapKey)); 809 if (sequenceNum != null) { 810 exprList.add(new EntityExpr("sequenceNum", EntityOperator.EQUALS, sequenceNum)); 811 } 812 exprList.add(new EntityExpr("mapKey", EntityOperator.EQUALS, mapKey)); 813 exprList.add(new EntityExpr("thruDate", EntityOperator.EQUALS, null)); 814 exprList.add(new EntityExpr("contentIdTo", EntityOperator.EQUALS, contentIdTo)); 815 exprList.add(new EntityExpr("contentAssocTypeId", EntityOperator.EQUALS, contentAssocTypeId)); 816 817 if (UtilValidate.isNotEmpty(activeContentId)) { 818 exprList.add(new EntityExpr("contentId", EntityOperator.NOT_EQUAL, activeContentId)); 819 } 820 if (UtilValidate.isNotEmpty(contentId)) { 821 exprList.add(new EntityExpr("contentId", EntityOperator.EQUALS, contentId)); 822 } 823 824 EntityConditionList assocExprList = new EntityConditionList(exprList, EntityOperator.AND); 825 List relatedAssocs = delegator.findByCondition("ContentAssoc", assocExprList, new ArrayList (), UtilMisc.toList("fromDate")); 826 List filteredAssocs = EntityUtil.filterByDate(relatedAssocs); 828 830 Iterator it = filteredAssocs.iterator(); 831 while (it.hasNext()) { 832 GenericValue val = (GenericValue) it.next(); 833 val.set("thruDate", nowTimestamp); 834 val.store(); 835 } 837 results.put("deactivatedList", filteredAssocs); 838 } catch (GenericEntityException e) { 839 return ServiceUtil.returnError(e.getMessage()); 840 } 841 842 return results; 843 } 844 845 849 public static Map renderSubContentAsText(DispatchContext dctx, Map context) { 850 Map results = new HashMap (); 851 GenericDelegator delegator = dctx.getDelegator(); 852 854 Map templateContext = (Map ) context.get("templateContext"); 855 String contentId = (String ) context.get("contentId"); 856 Timestamp fromDate = (Timestamp ) context.get("fromDate"); 857 GenericValue userLogin = (GenericValue) context.get("userLogin"); 858 859 if (templateContext != null && UtilValidate.isEmpty(contentId)) { 860 contentId = (String ) templateContext.get("contentId"); 861 } 862 String mapKey = (String ) context.get("mapKey"); 863 if (templateContext != null && UtilValidate.isEmpty(mapKey)) { 864 mapKey = (String ) templateContext.get("mapKey"); 865 } 866 String mimeTypeId = (String ) context.get("mimeTypeId"); 871 if (templateContext != null && UtilValidate.isEmpty(mimeTypeId)) { 872 mimeTypeId = (String ) templateContext.get("mimeTypeId"); 873 } 874 Locale locale = (Locale ) context.get("locale"); 875 if (templateContext != null && locale == null) { 876 locale = (Locale ) templateContext.get("locale"); 877 } 878 GenericValue subContentDataResourceView = (GenericValue) context.get("subContentDataResourceView"); 879 if (subContentDataResourceView != null && subContentDataResourceView == null) { 880 subContentDataResourceView = (GenericValue) templateContext.get("subContentDataResourceView"); 881 } 882 883 Writer out = (Writer ) context.get("outWriter"); 884 Writer outWriter = new StringWriter (); 885 886 if (templateContext == null) { 887 templateContext = new HashMap (); 888 } 889 890 try { 891 results = ContentWorker.renderSubContentAsTextCache(delegator, contentId, outWriter, mapKey, subContentDataResourceView, templateContext, locale, mimeTypeId, userLogin, fromDate); 892 out.write(outWriter.toString()); 893 results.put("textData", outWriter.toString()); 894 } catch (GeneralException e) { 895 Debug.logError(e, "Error rendering sub-content text", module); 896 return ServiceUtil.returnError(e.toString()); 897 } catch (IOException e) { 898 Debug.logError(e, "Error rendering sub-content text", module); 899 return ServiceUtil.returnError(e.toString()); 900 } 901 902 return results; 903 904 } 905 906 910 public static Map renderContentAsText(DispatchContext dctx, Map context) { 911 Map results = new HashMap (); 912 GenericDelegator delegator = dctx.getDelegator(); 913 Writer out = (Writer ) context.get("outWriter"); 914 915 Map templateContext = (Map ) context.get("templateContext"); 916 String contentId = (String ) context.get("contentId"); 918 if (templateContext != null && UtilValidate.isEmpty(contentId)) { 919 contentId = (String ) templateContext.get("contentId"); 920 } 921 String mimeTypeId = (String ) context.get("mimeTypeId"); 922 if (templateContext != null && UtilValidate.isEmpty(mimeTypeId)) { 923 mimeTypeId = (String ) templateContext.get("mimeTypeId"); 924 } 925 Locale locale = (Locale ) context.get("locale"); 926 if (templateContext != null && locale == null) { 927 locale = (Locale ) templateContext.get("locale"); 928 } 929 930 if (templateContext == null) { 931 templateContext = new HashMap (); 932 } 933 934 Writer outWriter = new StringWriter (); 935 GenericValue view = (GenericValue)context.get("subContentDataResourceView"); 936 try { 937 Map thisResults = ContentWorker.renderContentAsTextCache(delegator, contentId, outWriter, templateContext, view, locale, mimeTypeId); 938 out.write(outWriter.toString()); 939 results.put("textData", outWriter.toString()); 940 } catch (GeneralException e) { 941 Debug.logError(e, "Error rendering sub-content text", module); 942 return ServiceUtil.returnError(e.toString()); 943 } catch (IOException e) { 944 Debug.logError(e, "Error rendering sub-content text", module); 945 return ServiceUtil.returnError(e.toString()); 946 } 947 return results; 948 } 949 950 public static Map linkContentToPubPt(DispatchContext dctx, Map context) { 951 Map results = new HashMap (); 952 GenericDelegator delegator = dctx.getDelegator(); 953 LocalDispatcher dispatcher = dctx.getDispatcher(); 954 955 String contentId = (String ) context.get("contentId"); 956 String contentIdTo = (String ) context.get("contentIdTo"); 957 String contentAssocTypeId = (String ) context.get("contentAssocTypeId"); 958 String statusId = (String ) context.get("statusId"); 959 String privilegeEnumId = (String ) context.get("privilegeEnumId"); 960 GenericValue userLogin = (GenericValue) context.get("userLogin"); 961 962 if (Debug.infoOn()) Debug.logInfo("in publishContent, statusId:" + statusId, module); 963 if (Debug.infoOn()) Debug.logInfo("in publishContent, userLogin:" + userLogin, module); 964 965 Map mapIn = new HashMap (); 966 mapIn.put("contentId", contentId); 967 mapIn.put("contentIdTo", contentIdTo); 968 mapIn.put("contentAssocTypeId", contentAssocTypeId); 969 String publish = (String ) context.get("publish"); 970 971 try { 972 boolean isPublished = false; 973 GenericValue contentAssocViewFrom = ContentWorker.getContentAssocViewFrom(delegator, contentIdTo, contentId, contentAssocTypeId, statusId, privilegeEnumId); 974 if (contentAssocViewFrom != null) 975 isPublished = true; 976 if (Debug.infoOn()) Debug.logInfo("in publishContent, contentId:" + contentId + " contentIdTo:" + contentIdTo + " contentAssocTypeId:" + contentAssocTypeId + " publish:" + publish + " isPublished:" + isPublished, module); 977 if (UtilValidate.isNotEmpty(publish) && publish.equalsIgnoreCase("Y")) { 978 GenericValue content = delegator.findByPrimaryKey("Content", UtilMisc.toMap("contentId", contentId)); 979 String contentStatusId = (String ) content.get("statusId"); 980 String contentPrivilegeEnumId = (String ) content.get("privilegeEnumId"); 981 982 if (Debug.infoOn()) Debug.logInfo("in publishContent, statusId:" + statusId + " contentStatusId:" + contentStatusId + " privilegeEnumId:" + privilegeEnumId + " contentPrivilegeEnumId:" + contentPrivilegeEnumId, module); 983 if (!isPublished) { 985 content.put("privilegeEnumId", privilegeEnumId); 992 content.put("statusId", statusId); 993 content.store(); 994 995 mapIn = new HashMap (); 996 mapIn.put("contentId", contentId); 997 mapIn.put("contentIdTo", contentIdTo); 998 mapIn.put("contentAssocTypeId", contentAssocTypeId); 999 mapIn.put("mapKey", context.get("mapKey")); 1000 mapIn.put("fromDate", UtilDateTime.nowTimestamp()); 1001 mapIn.put("createdDate", UtilDateTime.nowTimestamp()); 1002 mapIn.put("lastModifiedDate", UtilDateTime.nowTimestamp()); 1003 mapIn.put("createdByUserLogin", userLogin.get("userLoginId")); 1004 mapIn.put("lastModifiedByUserLogin", userLogin.get("userLoginId")); 1005 delegator.create("ContentAssoc", mapIn); 1006 } 1007 } else { 1008 if (isPublished) { 1010 Map thisResults = dispatcher.runSync("deactivateAssocs", mapIn); 1011 String errorMsg = ServiceUtil.getErrorMessage(thisResults); 1012 if (UtilValidate.isNotEmpty(errorMsg)) { 1013 Debug.logError("Problem running deactivateAssocs. " + errorMsg, "ContentServices"); 1014 return ServiceUtil.returnError(errorMsg); 1015 } 1016 } 1017 } 1018 } catch (GenericEntityException e) { 1019 Debug.logError(e, "Problem getting existing content", "ContentServices"); 1020 return ServiceUtil.returnError(e.getMessage()); 1021 } catch (GenericServiceException e) { 1022 Debug.logError(e, "Problem running deactivateAssocs", "ContentServices"); 1023 return ServiceUtil.returnError(e.getMessage()); 1024 } 1025 1026 return results; 1027 } 1028 1029 public static Map publishContent(DispatchContext dctx, Map context) throws GenericServiceException{ 1030 1031 Map result = new HashMap (); 1032 GenericDelegator delegator = dctx.getDelegator(); 1033 GenericValue content = (GenericValue)context.get("content"); 1034 GenericValue userLogin = (GenericValue)context.get("userLogin"); 1035 Security security = dctx.getSecurity(); 1036 if (!security.hasEntityPermission("CONTENTMGR", "_ADMIN", userLogin)) { 1037 return ServiceUtil.returnError("Permission denied."); 1038 } 1039 1040 try { 1041 content.put("statusId", "BLOG_PUBLISHED"); 1042 content.store(); 1043 } catch(GenericEntityException e) { 1044 Debug.logError(e.getMessage(), module); 1045 return ServiceUtil.returnError(e.getMessage()); 1046 } 1047 return result; 1048 } 1049 1050 public static Map getPrefixedMembers(DispatchContext dctx, Map context) throws GenericServiceException{ 1051 1052 Map result = new HashMap (); 1053 Map mapIn = (Map )context.get("mapIn"); 1054 String prefix = (String )context.get("prefix"); 1055 Map mapOut = new HashMap (); 1056 result.put("mapOut", mapOut); 1057 if (mapIn != null) { 1058 Set entrySet = mapIn.entrySet(); 1059 Iterator iter = entrySet.iterator(); 1060 while (iter.hasNext()) { 1061 Map.Entry entry = (Map.Entry )iter.next(); 1062 String key = (String )entry.getKey(); 1063 if (key.startsWith(prefix)) { 1064 String keyBase = key.substring(prefix.length()); 1065 Object value = entry.getValue(); 1066 mapOut.put(key, value); 1067 } 1068 } 1069 } 1070 return result; 1071 } 1072 1073 public static Map splitString(DispatchContext dctx, Map context) throws GenericServiceException{ 1074 Map result = new HashMap (); 1075 List outputList = new ArrayList (); 1076 String delimiter = UtilFormatOut.checkEmpty((String )context.get("delimiter"), "|"); 1077 String inputString = (String )context.get("inputString"); 1078 if (UtilValidate.isNotEmpty(inputString)) { 1079 outputList = StringUtil.split(inputString, delimiter); 1080 } 1081 result.put("outputList", outputList); 1082 return result; 1083 } 1084 1085 public static Map joinString(DispatchContext dctx, Map context) throws GenericServiceException{ 1086 Map result = new HashMap (); 1087 String outputString = null; 1088 String delimiter = UtilFormatOut.checkEmpty((String )context.get("delimiter"), "|"); 1089 List inputList = (List )context.get("inputList"); 1090 if (inputList != null) { 1091 outputString = StringUtil.join(inputList, delimiter); 1092 } 1093 result.put("outputString", outputString); 1094 return result; 1095 } 1096 1097 public static Map urlEncodeArgs(DispatchContext dctx, Map context) throws GenericServiceException{ 1098 1099 Map result = new HashMap (); 1100 Map mapFiltered = new HashMap (); 1101 Map mapIn = (Map )context.get("mapIn"); 1102 if (mapIn != null) { 1103 Set entrySet = mapIn.entrySet(); 1104 Iterator iter = entrySet.iterator(); 1105 while (iter.hasNext()) { 1106 Map.Entry entry = (Map.Entry )iter.next(); 1107 String key = (String )entry.getKey(); 1108 Object value = entry.getValue(); 1109 if (value instanceof String ) { 1110 if (UtilValidate.isNotEmpty((String )value)) { 1111 mapFiltered.put(key, value); 1112 } 1113 } else if (value != null) { 1114 mapFiltered.put(key, value); 1115 } 1116 } 1117 String outputString = UtilHttp.urlEncodeArgs(mapFiltered); 1118 result.put("outputString", outputString ); 1119 } 1120 return result; 1121 } 1122 1123 1148 public static Map foToPdf(DispatchContext dctx, Map context) throws GenericServiceException{ 1149 1150 LocalDispatcher dispatcher = dctx.getDispatcher(); 1151 GenericValue userLogin = (GenericValue)context.get("userLogin"); 1152 Map result = new HashMap (); 1153 String foFileIn = (String )context.get("foFileIn"); 1154 String pdfFileOut = (String )context.get("pdfFileOut"); 1155 String outputDataResourceTypeId = (String )context.get("outputDataResourceTypeId"); 1156 String inputDataResourceTypeId = (String )context.get("inputDataResourceTypeId"); 1157 if (UtilValidate.isEmpty(inputDataResourceTypeId)) { 1158 inputDataResourceTypeId = "LOCAL_FILE"; 1159 } 1160 String outputContentId = (String )context.get("outputContentId"); 1161 String foContentId = (String )context.get("foContentId"); 1162 String templateDataResourceId = (String )context.get("templateDataResourceId"); 1163 Map fmContext = (Map )context.get("fmContext"); 1164 Map fmPrefixMap = (Map )context.get("fmPrefixMap"); 1165 Logger logger = new Log4JLogger(Debug.getLogger(module)); 1167 MessageHandler.setScreenLogger(logger); 1168 1169 String processedFo = null; 1171 if (UtilValidate.isEmpty(foContentId)) { 1174 if (UtilValidate.isEmpty(foFileIn)) { 1175 return ServiceUtil.returnError("No FO file or contentId available."); 1176 } 1177 Map mapIn = new HashMap (); 1178 mapIn.put("drObjectInfo", foFileIn); 1179 mapIn.put("drDataResourceTypeId", inputDataResourceTypeId); 1180 mapIn.put("contentTypeId", "DOCUMENT"); 1181 mapIn.put("templateDataResourceId", templateDataResourceId); 1183 mapIn.put("drDataTemplateTypeId", "FTL"); 1184 mapIn.put("userLogin", userLogin); 1185 try { 1186 Map thisResult = dispatcher.runSync("persistContentAndAssoc", mapIn); 1187 foContentId = (String )thisResult.get("contentId"); 1188 if (UtilValidate.isEmpty(foContentId)) { 1189 Debug.logError("Could not add FO content - foContentId is null.", "ContentServices"); 1190 return ServiceUtil.returnError("Could not add FO conten - foContentId is null."); 1191 } 1192 result.put("foContentId", foContentId); 1193 } catch (GenericServiceException e) { 1194 Debug.logError(e, "Problem adding FO content.", "ContentServices"); 1195 return ServiceUtil.returnError("Problem adding FO content."); 1196 } 1197 1198 } else { 1199 if (UtilValidate.isNotEmpty(templateDataResourceId)) { 1200 try { 1201 GenericDelegator delegator = dctx.getDelegator(); 1202 GenericValue content = delegator.findByPrimaryKeyCache("Content", UtilMisc.toMap("contentId", foContentId)); 1203 String thisTemplateDataResourceId = content.getString("templateDataResourceId"); 1204 if (thisTemplateDataResourceId == null || !thisTemplateDataResourceId.equals(templateDataResourceId)) { 1205 content.put("templateDataResourceId", templateDataResourceId); 1206 content.store(); 1207 } 1208 } catch(GenericEntityException e) { 1209 Debug.logError(e, "ContentServices"); 1210 return ServiceUtil.returnError(e.getMessage()); 1211 } 1212 } 1213 } 1214 1215 Map mapIn = new HashMap (); 1218 mapIn.put("contentId", foContentId); 1219 if (fmContext != null) { 1220 mapIn.put("templateContext", fmContext); 1221 } else { 1222 mapIn.put("templateContext", fmPrefixMap); 1223 } 1224 StringWriter sw = new StringWriter (); 1225 mapIn.put("outWriter", sw); 1226 try { 1227 Map thisResult = dispatcher.runSync("renderContentAsText", mapIn); 1228 processedFo = (String )thisResult.get("textData"); 1229 if (UtilValidate.isEmpty(processedFo)) { 1230 Debug.logError("Could not get FO text", "ContentServices"); 1231 return ServiceUtil.returnError("Could not get FO text"); 1232 } 1233 } catch (GenericServiceException e) { 1234 Debug.logError(e, "Problem getting FO text", "ContentServices"); 1235 return ServiceUtil.returnError("Problem getting FO text"); 1236 } 1237 1238 Driver driver = new Driver(); 1240 driver.setRenderer(Driver.RENDER_PDF); 1241 driver.setLogger(logger); 1242 1243 Document xslfo = null; 1245 try { 1246 xslfo = UtilXml.readXmlDocument(processedFo); 1247 } catch (FileNotFoundException e) { 1248 return ServiceUtil.returnError("Error getting FO file: " + e.toString()); 1249 } catch (IOException e2) { 1250 return ServiceUtil.returnError("Error getting FO file: " + e2.toString()); 1251 } catch (ParserConfigurationException e3) { 1252 return ServiceUtil.returnError("Error getting FO file: " + e3.toString()); 1253 } catch (SAXException e4) { 1254 return ServiceUtil.returnError("Error getting FO file: " + e4.toString()); 1255 } 1256 1257 ByteArrayOutputStream out = new ByteArrayOutputStream (); 1259 driver.setOutputStream(out); 1260 1261 InputSource is = new DocumentInputSource(xslfo); 1263 driver.setInputSource(is); 1264 try { 1265 driver.run(); 1266 FopImageFactory.resetCache(); 1267 } catch (Throwable t) { 1268 Debug.logError("Error processing PDF." + t.getMessage(), "ContentServices"); 1269 return ServiceUtil.returnError("Error processing PDF." + t.getMessage()); 1270 } 1271 ByteWrapper pdfByteWrapper = new ByteWrapper(out.toByteArray()); 1272 result.put("pdfByteWrapper", pdfByteWrapper ); 1273 1274 if (UtilValidate.isNotEmpty(outputDataResourceTypeId)) { 1277 if (pdfByteWrapper != null) { 1278 Map mapIn2 = new HashMap (); 1279 mapIn2.put("contentId", outputContentId); 1280 mapIn2.put("drDataResourceTypeId", outputDataResourceTypeId); 1281 mapIn2.put("contentTypeId", "DOCUMENT"); 1282 mapIn2.put("imageData", pdfByteWrapper); 1283 mapIn2.put("_imageData_contentType", "application/pdf"); 1284 mapIn2.put("_imageData_fileName", pdfFileOut); 1285 mapIn2.put("drObjectInfo", pdfFileOut); 1286 mapIn2.put("userLogin", userLogin); 1287 try { 1288 Map thisResult = dispatcher.runSync("persistContentAndAssoc", mapIn2); 1289 outputContentId = (String )thisResult.get("contentId"); 1290 if (UtilValidate.isEmpty(foContentId)) { 1291 Debug.logError("Could not add PDF content - contentId is null.", "ContentServices"); 1292 return ServiceUtil.returnError("Could not add PDF conten - contentId is null."); 1293 } 1294 result.put("outputContentId", outputContentId); 1295 } catch (GenericServiceException e) { 1296 Debug.logError(e, "Problem adding FO content.", module); 1297 return ServiceUtil.returnError("Problem adding FO content."); 1298 } 1299 result.put("outputContentId", outputContentId); 1300 } 1301 } else { 1302 if (UtilValidate.isEmpty(pdfFileOut)) { 1303 String outputPath = null; 1304 String thisDataResourceTypeId = null; 1305 String ofbizHome = System.getProperty("ofbiz.home"); 1306 int pos = pdfFileOut.indexOf("${OFBIZ_HOME}"); 1307 if (pos > 0 ) { 1308 outputPath = pdfFileOut.substring(pos + 13); 1309 thisDataResourceTypeId = "OFBIZ_FILE"; 1310 } else { 1311 outputPath = pdfFileOut; 1312 thisDataResourceTypeId = "LOCAL_FILE"; 1313 } 1314 Map mapIn3 = new HashMap (); 1315 mapIn3.put("objectInfo", outputPath); 1316 mapIn3.put("drDataResourceTypeId", thisDataResourceTypeId); 1317 mapIn3.put("binData", pdfByteWrapper); 1318 try { 1319 Map thisResult = dispatcher.runSync("createFile", mapIn3); 1320 } catch (GenericServiceException e) { 1321 Debug.logError(e, "Problem writing FO content.", module); 1322 return ServiceUtil.returnError("Problem adding FO content."); 1323 } 1324 } 1325 } 1326 return result; 1327 } 1328} 1329 | Popular Tags |