1 24 package org.ofbiz.content; 25 26 import java.sql.Timestamp ; 27 import java.util.Calendar ; 28 import java.util.ArrayList ; 29 import java.util.HashMap ; 30 import java.util.HashSet ; 31 import java.util.Iterator ; 32 import java.util.List ; 33 import java.util.Map ; 34 import java.util.Set ; 35 36 import javax.servlet.http.HttpServletRequest ; 37 import javax.servlet.http.HttpSession ; 38 39 import org.ofbiz.base.util.Debug; 40 import org.ofbiz.base.util.UtilDateTime; 41 import org.ofbiz.base.util.UtilMisc; 42 import org.ofbiz.base.util.UtilValidate; 43 import org.ofbiz.base.util.StringUtil; 44 import org.ofbiz.base.util.cache.UtilCache; 45 import org.ofbiz.content.content.ContentServices; 46 import org.ofbiz.content.content.ContentWorker; 47 import org.ofbiz.entity.GenericDelegator; 48 import org.ofbiz.entity.GenericEntityException; 49 import org.ofbiz.entity.GenericPK; 50 import org.ofbiz.entity.GenericValue; 51 import org.ofbiz.entity.condition.EntityCondition; 52 import org.ofbiz.entity.condition.EntityConditionList; 53 import org.ofbiz.entity.condition.EntityExpr; 54 import org.ofbiz.entity.condition.EntityOperator; 55 import org.ofbiz.entity.model.ModelUtil; 56 import org.ofbiz.entity.util.ByteWrapper; 57 import org.ofbiz.entity.util.EntityUtil; 58 import org.ofbiz.security.Security; 59 import org.ofbiz.service.DispatchContext; 60 import org.ofbiz.service.GenericServiceException; 61 import org.ofbiz.service.LocalDispatcher; 62 import org.ofbiz.service.ModelService; 63 import org.ofbiz.service.ServiceAuthException; 64 import org.ofbiz.service.ServiceUtil; 65 66 73 public class ContentManagementServices { 74 75 public static final String module = ContentManagementServices.class.getName(); 76 77 82 public static Map getSubContent(DispatchContext dctx, Map context) { 83 GenericDelegator delegator = dctx.getDelegator(); 85 String contentId = (String ) context.get("contentId"); 87 String subContentId = (String ) context.get("subContentId"); 88 String mapKey = (String ) context.get("mapKey"); 89 GenericValue userLogin = (GenericValue)context.get("userLogin"); 90 Timestamp fromDate = (Timestamp )context.get("fromDate"); 91 List assocTypes = (List ) context.get("assocTypes"); 92 String assocTypesString = (String )context.get("assocTypesString"); 93 if (UtilValidate.isNotEmpty(assocTypesString)) { 94 List lst = StringUtil.split(assocTypesString, "|"); 95 if (assocTypes == null) { 96 assocTypes = new ArrayList (); 97 } 98 assocTypes.addAll(lst); 99 } 100 GenericValue content = null; 101 GenericValue view = null; 102 103 try { 104 view = ContentWorker.getSubContentCache( delegator, contentId, mapKey, subContentId, userLogin, assocTypes, fromDate, new Boolean (false), null); 105 content = ContentWorker.getContentFromView(view); 106 } catch(GenericEntityException e) { 107 return ServiceUtil.returnError(e.getMessage()); 108 } 109 110 Map results = ServiceUtil.returnSuccess(); 111 results.put("view", view); 112 results.put("content", content); 113 return results; 114 } 115 116 120 public static Map getContent(DispatchContext dctx, Map context) { 121 GenericDelegator delegator = dctx.getDelegator(); 123 String contentId = (String ) context.get("contentId"); 124 GenericValue view = null; 126 127 try { 128 view = ContentWorker.getContentCache( delegator, contentId); 129 } catch(GenericEntityException e) { 130 return ServiceUtil.returnError(e.getMessage()); 131 } 132 133 Map results = ServiceUtil.returnSuccess(); 134 results.put("view", view); 135 return results; 136 } 137 138 144 public static Map addMostRecent(DispatchContext dctx, Map context) { 145 GenericValue val = (GenericValue)context.get("pk"); 151 GenericPK pk = val.getPrimaryKey(); 152 HttpSession session = (HttpSession )context.get("session"); 153 154 ContentManagementWorker.mruAdd(session, pk); 155 return ServiceUtil.returnSuccess(); 156 } 157 158 159 170 public static Map persistContentAndAssoc(DispatchContext dctx, Map context) throws GenericServiceException { 171 GenericDelegator delegator = dctx.getDelegator(); 172 LocalDispatcher dispatcher = dctx.getDispatcher(); 173 174 Boolean bDisplayFailCond = (Boolean )context.get("displayFailCond"); 178 String mapKey = (String ) context.get("mapKey"); 179 180 String deactivateExisting = (String ) context.get("deactivateExisting"); 183 if (UtilValidate.isEmpty(deactivateExisting)) { 184 if (UtilValidate.isEmpty(mapKey)) 185 deactivateExisting = "false"; 186 else 187 deactivateExisting = "true"; 188 } 189 if (Debug.infoOn()) Debug.logInfo("in persist... mapKey(0):" + mapKey, null); 190 191 List contentPurposeList = (List )context.get("contentPurposeList"); 193 if (contentPurposeList == null) 194 contentPurposeList = new ArrayList (); 195 String contentPurposeString = (String ) context.get("contentPurposeString"); 196 if (UtilValidate.isNotEmpty(contentPurposeString)) { 197 List tmpPurposes = StringUtil.split(contentPurposeString, "|"); 198 contentPurposeList.addAll(tmpPurposes); 199 } 200 if (contentPurposeList != null ) { 201 context.put("contentPurposeList", contentPurposeList); 202 context.put("contentPurposeString", null); 203 } 204 if (Debug.infoOn()) Debug.logInfo("in persist... contentPurposeList(0):" + contentPurposeList, null); 205 if (Debug.infoOn()) Debug.logInfo("in persist... textData(0):" + context.get("textData"), null); 206 207 208 GenericValue content = delegator.makeValue("Content", null); 209 content.setPKFields(context); 210 content.setNonPKFields(context); 211 String contentId = (String ) content.get("contentId"); 212 String contentTypeId = (String ) content.get("contentTypeId"); 213 String origContentId = (String ) content.get("contentId"); 214 String origDataResourceId = (String ) content.get("dataResourceId"); 215 if (Debug.infoOn()) Debug.logInfo("in persist... contentId(0):" + contentId, null); 216 217 GenericValue dataResource = delegator.makeValue("DataResource", null); 218 dataResource.setPKFields(context); 219 dataResource.setNonPKFields(context); 220 dataResource.setAllFields(context, false, "dr", null); 221 context.putAll(dataResource); 222 String dataResourceId = (String ) dataResource.get("dataResourceId"); 223 String dataResourceTypeId = (String ) dataResource.get("dataResourceTypeId"); 224 if (Debug.infoOn()) Debug.logInfo("in persist... dataResourceId(0):" + dataResourceId, null); 225 226 GenericValue contentAssoc = delegator.makeValue("ContentAssoc", null); 227 contentAssoc.setPKFields(context); 228 contentAssoc.setNonPKFields(context); 229 contentAssoc.setAllFields(context, false, "ca", null); 230 context.putAll(contentAssoc); 231 232 GenericValue electronicText = delegator.makeValue("ElectronicText", null); 233 electronicText.setPKFields(context); 234 electronicText.setNonPKFields(context); 235 236 Map results = ServiceUtil.returnSuccess(); 238 results.put("contentId", content.get("contentId")); 239 results.put("dataResourceId", dataResource.get("dataResourceId")); 240 results.put("contentIdTo", contentAssoc.get("contentIdTo")); 241 results.put("fromDate", contentAssoc.get("fromDate")); 242 results.put("contentAssocTypeId", contentAssoc.get("contentAssocTypeId")); 243 results.put("drDataResourceId", dataResource.get("dataResourceId")); 244 results.put("caContentIdTo", contentAssoc.get("contentIdTo")); 245 results.put("caFromDate", contentAssoc.get("fromDate")); 246 results.put("caContentAssocTypeId", contentAssoc.get("contentAssocTypeId")); 247 248 GenericValue userLogin = (GenericValue) context.get("userLogin"); 250 251 254 256 261 265 266 boolean dataResourceExists = true; 267 if (Debug.infoOn()) Debug.logInfo("in persist... dataResourceTypeId(0):" + dataResourceTypeId, null); 268 if (UtilValidate.isNotEmpty(dataResourceTypeId) ) { 269 Map dataResourceResult = new HashMap (); 270 try { 271 dataResourceResult = persistDataResourceAndDataMethod(dctx, context); 272 } catch (GenericServiceException e) { 273 return ServiceUtil.returnError(e.getMessage()); 274 } catch (GenericEntityException e) { 275 return ServiceUtil.returnError(e.getMessage()); 276 } catch (Exception e) { 277 return ServiceUtil.returnError(e.getMessage()); 278 } 279 String errorMsg = ServiceUtil.getErrorMessage(dataResourceResult); 280 if (UtilValidate.isNotEmpty(errorMsg)) { 281 return ServiceUtil.returnError(errorMsg); 282 } 283 dataResourceId = (String )dataResourceResult.get("dataResourceId"); 284 results.put("dataResourceId", dataResourceId); 285 results.put("drDataResourceId", dataResourceId); 286 context.put("dataResourceId", dataResourceId); 287 content.put("dataResourceId", dataResourceId); 288 context.put("drDataResourceId", dataResourceId); 289 } 290 292 context.put("skipPermissionCheck", null); boolean contentExists = true; 294 if (Debug.infoOn()) Debug.logInfo("in persist... contentTypeId" + contentTypeId + " dataResourceTypeId:" + dataResourceTypeId + " contentId:" + contentId + " dataResourceId:" + dataResourceId, null); 295 if (UtilValidate.isNotEmpty(contentTypeId) ) { 296 if (UtilValidate.isEmpty(contentId)) { 297 contentExists = false; 298 } else { 299 try { 300 GenericValue val = delegator.findByPrimaryKey("Content", UtilMisc.toMap("contentId", contentId)); 301 if (val == null) contentExists = false; 302 } catch(GenericEntityException e) { 303 return ServiceUtil.returnError(e.getMessage()); 304 } 305 } 306 context.putAll(content); 309 if (contentExists) { 310 Map contentContext = new HashMap (); 312 ModelService contentModel = dispatcher.getDispatchContext().getModelService("updateContent"); 313 contentContext.putAll(contentModel.makeValid(context, "IN")); 314 contentContext.put("userLogin", userLogin); 315 contentContext.put("displayFailCond", bDisplayFailCond); 316 contentContext.put("skipPermissionCheck", context.get("skipPermissionCheck")); 317 Map thisResult = dispatcher.runSync("updateContent", contentContext); 318 if (ServiceUtil.isError(thisResult) || ServiceUtil.isFailure(thisResult)) { 319 return ServiceUtil.returnError("Error updating content (updateContent) in persistContentAndAssoc", null, null, thisResult); 320 } 321 } else { 323 Map contentContext = new HashMap (); 325 ModelService contentModel = dispatcher.getDispatchContext().getModelService("createContent"); 326 contentContext.putAll(contentModel.makeValid(context, "IN")); 327 contentContext.put("userLogin", userLogin); 328 contentContext.put("displayFailCond", bDisplayFailCond); 329 contentContext.put("skipPermissionCheck", context.get("skipPermissionCheck")); 330 Debug.logInfo("In persistContentAndAssoc calling createContent with content: " + contentContext, module); 331 Map thisResult = dispatcher.runSync("createContent", contentContext); 332 if (ServiceUtil.isError(thisResult) || ServiceUtil.isFailure(thisResult)) { 333 return ServiceUtil.returnError("Error creating content (createContent) in persistContentAndAssoc", null, null, thisResult); 334 } 335 337 contentId = (String ) thisResult.get("contentId"); 338 } 339 results.put("contentId", contentId); 340 context.put("contentId", contentId); 341 context.put("caContentId", contentId); 342 343 344 345 if (contentId != null && !contentExists) { 347 try { 348 if (contentPurposeList != null) { 349 for (int i=0; i < contentPurposeList.size(); i++) { 350 String contentPurposeTypeId = (String )contentPurposeList.get(i); 351 GenericValue contentPurpose = delegator.makeValue("ContentPurpose", 352 UtilMisc.toMap("contentId", contentId, 353 "contentPurposeTypeId", contentPurposeTypeId) ); 354 contentPurpose.create(); 355 } 356 } 357 } catch(GenericEntityException e) { 358 return ServiceUtil.returnError(e.getMessage()); 359 } 360 } 361 362 } else if (UtilValidate.isNotEmpty(dataResourceTypeId) && UtilValidate.isNotEmpty(contentId)) { 363 if (UtilValidate.isNotEmpty(dataResourceId) && !dataResourceExists) { 365 Map map = new HashMap (); 366 map.put("userLogin", userLogin); 367 map.put("dataResourceId", dataResourceId); 368 map.put("contentId", contentId); 369 if (Debug.infoOn()) Debug.logInfo("in persist... context:" + context, module); 370 Map r = ContentServices.updateContentMethod(dctx, map); 371 boolean isError = ModelService.RESPOND_ERROR.equals(r.get(ModelService.RESPONSE_MESSAGE)); 372 if (isError) 373 return ServiceUtil.returnError( (String )r.get(ModelService.ERROR_MESSAGE)); 374 } 375 } 376 377 String contentAssocTypeId = (String )context.get("contentAssocTypeId"); 379 if (UtilValidate.isEmpty(contentAssocTypeId)) 380 contentAssocTypeId = (String )context.get("caContentAssocTypeId"); 381 382 if (Debug.infoOn()) Debug.logInfo("CREATING contentASSOC contentAssocTypeId:" + contentAssocTypeId, null); 383 if (contentAssocTypeId != null && contentAssocTypeId.length() > 0 ) { 384 if (Debug.infoOn()) Debug.logInfo("in persistContentAndAssoc, deactivateExistin:" + deactivateExisting, null); 385 Map contentAssocContext = new HashMap (); 386 contentAssocContext.put("userLogin", userLogin); 387 contentAssocContext.put("displayFailCond", bDisplayFailCond); 388 contentAssocContext.put("skipPermissionCheck", context.get("skipPermissionCheck")); 389 Map thisResult = null; 390 try { 391 contentAssoc.setPKFields(context); 392 contentAssoc.setNonPKFields(context); 393 GenericValue contentAssocPK = (GenericValue)contentAssoc.clone(); 394 GenericValue contentAssocExisting = null; 395 if (contentAssocPK.isPrimaryKey()) 396 contentAssocExisting = delegator.findByPrimaryKeyCache("ContentAssoc", contentAssocPK); 397 398 if (contentAssocExisting == null) { 399 ModelService contentAssocModel = dispatcher.getDispatchContext().getModelService("createContentAssoc"); 400 Map ctx = contentAssocModel.makeValid(contentAssoc, "IN"); 401 contentAssocContext.putAll(ctx); 402 thisResult = dispatcher.runSync("createContentAssoc", contentAssocContext); 403 String errMsg = ServiceUtil.getErrorMessage(thisResult); 404 if (ServiceUtil.isError(thisResult) || ServiceUtil.isFailure(thisResult) || UtilValidate.isNotEmpty(errMsg)) { 405 return ServiceUtil.returnError(errMsg); 406 } 407 results.put("contentIdTo", thisResult.get("contentIdTo")); 408 results.put("contentIdFrom", thisResult.get("contentIdFrom")); 409 results.put("contentAssocTypeId", thisResult.get("contentAssocTypeId")); 411 results.put("fromDate", thisResult.get("fromDate")); 412 results.put("sequenceNum", thisResult.get("sequenceNum")); 413 414 results.put("caContentIdTo", thisResult.get("contentIdTo")); 415 results.put("caContentAssocTypeId", thisResult.get("contentAssocTypeId")); 416 results.put("caFromDate", thisResult.get("fromDate")); 417 results.put("caSequenceNum", thisResult.get("sequenceNum")); 418 } else { 419 if ("true".equalsIgnoreCase(deactivateExisting)) { 420 } 423 ModelService contentAssocModel = dispatcher.getDispatchContext().getModelService("updateContentAssoc"); 424 Map ctx = contentAssocModel.makeValid(contentAssoc, "IN"); 425 contentAssocContext.putAll(ctx); 426 thisResult = dispatcher.runSync("updateContentAssoc", contentAssocContext); 427 String errMsg = ServiceUtil.getErrorMessage(thisResult); 428 if (ServiceUtil.isError(thisResult) || ServiceUtil.isFailure(thisResult) || UtilValidate.isNotEmpty(errMsg)) { 429 return ServiceUtil.returnError(errMsg); 430 } 431 } 432 } catch (GenericEntityException e) { 433 throw new GenericServiceException(e.getMessage()); 434 } catch (Exception e2) { 435 throw new GenericServiceException(e2.getMessage()); 436 } 437 String errMsg = ServiceUtil.getErrorMessage(thisResult); 438 if (UtilValidate.isNotEmpty(errMsg)) { 439 return ServiceUtil.returnError(errMsg); 440 } 441 } 442 context.remove("skipPermissionCheck"); 443 context.put("contentId", origContentId); 444 context.put("dataResourceId", origDataResourceId); 445 context.remove("dataResource"); 446 Debug.logInfo("results:" + results, module); 447 return results; 448 } 449 450 454 public static Map updateSiteRoles(DispatchContext dctx, Map context) { 455 LocalDispatcher dispatcher = dctx.getDispatcher(); 456 GenericDelegator delegator = dctx.getDelegator(); 457 GenericValue userLogin = (GenericValue)context.get("userLogin"); 458 Map results = new HashMap (); 460 String siteContentId = (String )context.get("contentId"); 466 String partyId = (String )context.get("partyId"); 467 468 if (UtilValidate.isEmpty(siteContentId) || UtilValidate.isEmpty(partyId)) 469 return results; 470 471 473 List siteRoles = null; 474 try { 475 siteRoles = delegator.findByAndCache("RoleType", UtilMisc.toMap("parentTypeId", "BLOG")); 476 } catch(GenericEntityException e) { 477 return ServiceUtil.returnError( e.getMessage()); 478 } 479 480 Iterator siteRoleIter = siteRoles.iterator(); 481 while (siteRoleIter.hasNext()) { 482 Map serviceContext = new HashMap (); 483 serviceContext.put("partyId", partyId); 484 serviceContext.put("contentId", siteContentId); 485 serviceContext.put("userLogin", userLogin); 486 Debug.logInfo("updateSiteRoles, serviceContext(0):" + serviceContext, module); 487 GenericValue roleType = (GenericValue)siteRoleIter.next(); 488 String siteRole = (String )roleType.get("roleTypeId"); String cappedSiteRole = ModelUtil.dbNameToVarName(siteRole); 490 if (Debug.infoOn()) Debug.logInfo("updateSiteRoles, cappediteRole(1):" + cappedSiteRole, module); 491 492 String siteRoleVal = (String )context.get(cappedSiteRole); 493 if (Debug.infoOn()) Debug.logInfo("updateSiteRoles, siteRoleVal(1):" + siteRoleVal, module); 494 if (Debug.infoOn()) Debug.logInfo("updateSiteRoles, context(1):" + context, module); 495 Object fromDate = context.get(cappedSiteRole + "FromDate"); 496 if (Debug.infoOn()) Debug.logInfo("updateSiteRoles, fromDate(1):" + fromDate, module); 497 serviceContext.put("roleTypeId", siteRole); 498 if (siteRoleVal != null && siteRoleVal.equalsIgnoreCase("Y")) { 499 if (fromDate == null ) { 502 try { 503 Map newContext = new HashMap (); 504 newContext.put("contentId", serviceContext.get("contentId")); 505 newContext.put("partyId", serviceContext.get("partyId")); 506 newContext.put("roleTypeId", serviceContext.get("roleTypeId")); 507 newContext.put("userLogin", userLogin); 508 Map permResults = dispatcher.runSync("deactivateAllContentRoles", newContext); 509 serviceContext.put("fromDate", UtilDateTime.nowTimestamp()); 510 if (Debug.infoOn()) Debug.logInfo("updateSiteRoles, serviceContext(1):" + serviceContext, module); 511 permResults = dispatcher.runSync("createContentRole", serviceContext); 512 String errMsg = ServiceUtil.getErrorMessage(permResults); 513 if (UtilValidate.isNotEmpty(errMsg)) 514 return ServiceUtil.returnError(errMsg); 515 } catch (GenericServiceException e) { 517 Debug.logError(e, e.getMessage(), module); 518 return ServiceUtil.returnError( e.getMessage()); 519 } catch (Exception e2) { 520 Debug.logError(e2, e2.getMessage(), module); 521 return ServiceUtil.returnError( e2.getMessage()); 522 } 523 } 524 } else { 525 if (fromDate != null ) { 526 try { 529 Debug.logInfo("updateSiteRoles, serviceContext(2):" + serviceContext, module); 530 Map newContext = new HashMap (); 534 newContext.put("contentId", serviceContext.get("contentId")); 535 newContext.put("partyId", serviceContext.get("partyId")); 536 newContext.put("roleTypeId", serviceContext.get("roleTypeId")); 537 newContext.put("userLogin", userLogin); 538 Map permResults = dispatcher.runSync("deactivateAllContentRoles", newContext); 539 String errMsg = ServiceUtil.getErrorMessage(permResults); 540 if (UtilValidate.isNotEmpty(errMsg)) 541 return ServiceUtil.returnError(errMsg); 542 } catch (GenericServiceException e) { 543 Debug.logError(e, e.getMessage(), module); 544 return ServiceUtil.returnError( e.getMessage()); 545 } catch (Exception e2) { 546 Debug.logError(e2, e2.getMessage(), module); 547 return ServiceUtil.returnError( e2.getMessage()); 548 } 549 } 550 } 551 } 552 return results; 553 } 554 555 public static Map persistDataResourceAndData(DispatchContext dctx, Map context) { 556 LocalDispatcher dispatcher = dctx.getDispatcher(); 558 Map result = new HashMap (); 560 try { 561 ModelService checkPermModel = dispatcher.getDispatchContext().getModelService("checkContentPermission"); 563 Map ctx = checkPermModel.makeValid(context, "IN"); 564 Map thisResult = dispatcher.runSync("checkContentPermission", ctx); 565 String permissionStatus = (String )thisResult.get("permissionStatus"); 566 if (UtilValidate.isNotEmpty(permissionStatus) && permissionStatus.equalsIgnoreCase("granted")) { 567 result = persistDataResourceAndDataMethod(dctx, context); 568 } 569 } catch (GenericServiceException e) { 570 return ServiceUtil.returnError(e.getMessage()); 571 } catch (GenericEntityException e) { 572 return ServiceUtil.returnError(e.getMessage()); 573 } catch (Exception e) { 574 return ServiceUtil.returnError(e.getMessage()); 575 } 576 String errorMsg = ServiceUtil.getErrorMessage(result); 577 if (UtilValidate.isNotEmpty(errorMsg)) { 578 return ServiceUtil.returnError(errorMsg); 579 } 580 return result; 581 } 582 583 public static Map persistDataResourceAndDataMethod(DispatchContext dctx, Map context) throws GenericServiceException, GenericEntityException, Exception { 584 GenericDelegator delegator = dctx.getDelegator(); 585 LocalDispatcher dispatcher = dctx.getDispatcher(); 586 Map result = new HashMap (); 587 Map newDrContext = new HashMap (); 588 GenericValue dataResource = delegator.makeValue("DataResource", null); 589 dataResource.setPKFields(context); 590 dataResource.setNonPKFields(context); 591 dataResource.setAllFields(context, false, "dr", null); 592 context.putAll(dataResource); 593 594 GenericValue electronicText = delegator.makeValue("ElectronicText", null); 595 electronicText.setPKFields(context); 596 electronicText.setNonPKFields(context); 597 String textData = (String )electronicText.get("textData"); 598 599 600 String dataResourceId = (String )dataResource.get("dataResourceId"); 601 String dataResourceTypeId = (String )dataResource.get("dataResourceTypeId"); 602 if (Debug.infoOn()) Debug.logInfo("in persist... dataResourceId(0):" + dataResourceId, null); 603 context.put("skipPermissionCheck", "granted"); boolean dataResourceExists = true; 605 if (UtilValidate.isEmpty(dataResourceId)) { 606 dataResourceExists = false; 607 } else { 608 try { 609 GenericValue val = delegator.findByPrimaryKey("DataResource", UtilMisc.toMap("dataResourceId", dataResourceId)); 610 if (val == null) 611 dataResourceExists = false; 612 } catch(GenericEntityException e) { 613 return ServiceUtil.returnError(e.getMessage()); 614 } 615 } 616 GenericValue userLogin = (GenericValue) context.get("userLogin"); 617 ModelService dataResourceModel = dispatcher.getDispatchContext().getModelService("updateDataResource"); 619 Map ctx = dataResourceModel.makeValid(dataResource, "IN"); 620 newDrContext.putAll(ctx); 621 newDrContext.put("userLogin", userLogin); 622 newDrContext.put("skipPermissionCheck", context.get("skipPermissionCheck")); 623 ByteWrapper byteWrapper = (ByteWrapper)context.get("imageData"); 624 String mimeTypeId = (String ) newDrContext.get("mimeTypeId"); 625 if (byteWrapper != null && (mimeTypeId == null || (mimeTypeId.indexOf("image") >= 0) || (mimeTypeId.indexOf("application") >= 0))) { 626 mimeTypeId = (String ) context.get("_imageData_contentType"); 627 String fileName = (String ) context.get("_imageData_fileName"); 628 newDrContext.put("objectInfo", fileName); 629 newDrContext.put("mimeTypeId", mimeTypeId); 630 } 631 632 if (!dataResourceExists) { 633 Map thisResult = dispatcher.runSync("createDataResource", newDrContext); 634 String errorMsg = ServiceUtil.getErrorMessage(thisResult); 635 if (UtilValidate.isNotEmpty(errorMsg)) { 636 throw(new Exception (errorMsg)); 637 } 638 dataResourceId = (String )thisResult.get("dataResourceId"); 639 if (Debug.infoOn()) Debug.logInfo("in persist... dataResourceId(0):" + dataResourceId, null); 640 dataResource = (GenericValue)thisResult.get("dataResource"); 641 Map fileContext = new HashMap (); 642 fileContext.put("userLogin", userLogin); 643 if ( dataResourceTypeId.indexOf("_FILE") >=0) { 644 boolean hasData = false; 645 if (textData != null) { 646 fileContext.put("textData", textData); 647 hasData = true; 648 } 649 if (byteWrapper != null) { 650 fileContext.put("binData", byteWrapper); 651 hasData = true; 652 } 653 if (hasData) { 654 fileContext.put("rootDir", context.get("rootDir")); 655 fileContext.put("dataResourceTypeId", dataResourceTypeId); 656 fileContext.put("objectInfo", dataResource.get("objectInfo")); 657 thisResult = dispatcher.runSync("createFile", fileContext); 658 errorMsg = ServiceUtil.getErrorMessage(thisResult); 659 if (UtilValidate.isNotEmpty(errorMsg)) { 660 return ServiceUtil.returnError(errorMsg); 661 } 662 } 663 } else if (dataResourceTypeId.equals("IMAGE_OBJECT")) { 664 if (byteWrapper != null) { 665 fileContext.put("dataResourceId", dataResourceId); 666 fileContext.put("imageData", byteWrapper); 667 thisResult = dispatcher.runSync("createImage", fileContext); 668 errorMsg = ServiceUtil.getErrorMessage(thisResult); 669 if (UtilValidate.isNotEmpty(errorMsg)) { 670 return ServiceUtil.returnError(errorMsg); 671 } 672 } else { 673 } 675 } else if (dataResourceTypeId.equals("SHORT_TEXT")) { 676 } else if (dataResourceTypeId.startsWith("SURVEY")) { 677 } else { 678 if (UtilValidate.isNotEmpty(textData)) { 680 fileContext.put("dataResourceId", dataResourceId); 681 fileContext.put("textData", textData); 682 thisResult = dispatcher.runSync("createElectronicText", fileContext); 683 errorMsg = ServiceUtil.getErrorMessage(thisResult); 684 if (UtilValidate.isNotEmpty(errorMsg)) { 685 return ServiceUtil.returnError(errorMsg); 686 } 687 } 688 } 689 } else { 690 Map thisResult = dispatcher.runSync("updateDataResource", newDrContext); 691 String errorMsg = ServiceUtil.getErrorMessage(thisResult); 692 if (UtilValidate.isNotEmpty(errorMsg)) { 693 return ServiceUtil.returnError(errorMsg); 694 } 695 if (Debug.infoOn()) Debug.logInfo("in persist... thisResult.permissionStatus(0):" + thisResult.get("permissionStatus"), null); 697 Map fileContext = new HashMap (); 699 fileContext.put("userLogin", userLogin); 700 String forceElectronicText = (String )context.get("forceElectronicText"); 701 if (dataResourceTypeId.indexOf("_FILE") >=0) { 702 boolean hasData = false; 703 if (textData != null) { 704 fileContext.put("textData", textData); 705 hasData = true; 706 } 707 if (byteWrapper != null) { 708 fileContext.put("binData", byteWrapper); 709 hasData = true; 710 } 711 if (hasData || "true".equalsIgnoreCase(forceElectronicText)) { 712 fileContext.put("rootDir", context.get("rootDir")); 713 fileContext.put("dataResourcetype", dataResourceTypeId); 714 fileContext.put("objectInfo", dataResource.get("objectInfo")); 715 thisResult = dispatcher.runSync("updateFile", fileContext); 716 errorMsg = ServiceUtil.getErrorMessage(thisResult); 717 if (UtilValidate.isNotEmpty(errorMsg)) { 718 return ServiceUtil.returnError(errorMsg); 719 } 720 } 721 } else if (dataResourceTypeId.equals("IMAGE_OBJECT")) { 722 if (byteWrapper != null || "true".equalsIgnoreCase(forceElectronicText)) { 723 fileContext.put("dataResourceId", dataResourceId); 724 fileContext.put("imageData", byteWrapper); 725 thisResult = dispatcher.runSync("updateImage", fileContext); 726 errorMsg = ServiceUtil.getErrorMessage(thisResult); 727 if (UtilValidate.isNotEmpty(errorMsg)) { 728 return ServiceUtil.returnError(errorMsg); 729 } 730 } else { 731 } 733 } else if (dataResourceTypeId.equals("SHORT_TEXT")) { 734 } else if (dataResourceTypeId.startsWith("SURVEY")) { 735 } else { 736 if (UtilValidate.isNotEmpty(textData) || "true".equalsIgnoreCase(forceElectronicText)) { 737 fileContext.put("dataResourceId", dataResourceId); 738 fileContext.put("textData", textData); 739 thisResult = dispatcher.runSync("updateElectronicText", fileContext); 740 errorMsg = ServiceUtil.getErrorMessage(thisResult); 741 if (UtilValidate.isNotEmpty(errorMsg)) { 742 return ServiceUtil.returnError(errorMsg); 743 } 744 } 745 } 746 } 747 748 result.put("dataResourceId", dataResourceId); 749 result.put("drDataResourceId", dataResourceId); 750 context.put("dataResourceId", dataResourceId); 751 return result; 752 } 753 754 public static void addRoleToUser(GenericDelegator delegator, LocalDispatcher dispatcher, Map serviceContext) throws GenericServiceException, GenericEntityException { 755 String partyId = (String )serviceContext.get("partyId"); 756 Map findMap = UtilMisc.toMap("partyId", partyId); 757 List userLoginList = delegator.findByAnd("UserLogin", findMap); 758 Iterator iter = userLoginList.iterator(); 759 while (iter.hasNext()) { 760 GenericValue partyUserLogin = (GenericValue)iter.next(); 761 String partyUserLoginId = partyUserLogin.getString("userLoginId"); 762 serviceContext.put("contentId", partyUserLoginId); dispatcher.runSync("createContentRole", serviceContext); 764 } 765 } 766 767 public static Map updateSiteRolesDyn(DispatchContext dctx, Map context) { 768 769 LocalDispatcher dispatcher = dctx.getDispatcher(); 770 GenericDelegator delegator = dctx.getDelegator(); 771 Map results = new HashMap (); 772 Map serviceContext = new HashMap (); 773 String siteContentId = (String )context.get("contentId"); 779 String partyId = (String )context.get("partyId"); 780 serviceContext.put("partyId", partyId); 781 serviceContext.put("contentId", siteContentId); 782 785 List siteRoles = null; 786 try { 787 siteRoles = delegator.findByAndCache("RoleType", UtilMisc.toMap("parentTypeId", "BLOG")); 788 } catch(GenericEntityException e) { 789 return ServiceUtil.returnError( e.getMessage()); 790 } 791 Iterator siteRoleIter = siteRoles.iterator(); 792 while (siteRoleIter.hasNext()) { 793 GenericValue roleType = (GenericValue)siteRoleIter.next(); 794 String siteRole = (String )roleType.get("roleTypeId"); String cappedSiteRole = ModelUtil.dbNameToVarName(siteRole); 796 798 String siteRoleVal = (String )context.get(cappedSiteRole); 799 Object fromDate = context.get(cappedSiteRole + "FromDate"); 800 serviceContext.put("roleTypeId", siteRole); 801 if (siteRoleVal != null && siteRoleVal.equalsIgnoreCase("Y")) { 802 if (fromDate == null ) { 805 try { 806 serviceContext.put("fromDate", UtilDateTime.nowTimestamp()); 807 if (Debug.infoOn()) Debug.logInfo("updateSiteRoles, serviceContext(1):" + serviceContext, module); 808 addRoleToUser(delegator, dispatcher, serviceContext); 809 Map permResults = dispatcher.runSync("createContentRole", serviceContext); 810 } catch (GenericServiceException e) { 811 Debug.logError(e, e.getMessage(), module); 812 } catch (Exception e2) { 813 Debug.logError(e2, e2.getMessage(), module); 814 } 815 } 816 } else { 817 if (fromDate != null ) { 818 try { 821 Debug.logInfo("updateSiteRoles, serviceContext(2):" + serviceContext, module); 822 Map newContext = new HashMap (); 826 newContext.put("contentId", serviceContext.get("contentId")); 827 newContext.put("partyId", serviceContext.get("partyId")); 828 newContext.put("roleTypeId", serviceContext.get("roleTypeId")); 829 Map permResults = dispatcher.runSync("deactivateAllContentRoles", newContext); 830 } catch (GenericServiceException e) { 831 Debug.logError(e, e.getMessage(), module); 832 } catch (Exception e2) { 833 Debug.logError(e2, e2.getMessage(), module); 834 } 835 } 836 } 837 } 838 return results; 839 } 840 841 public static Map updateOrRemove(DispatchContext dctx, Map context) { 842 843 Map results = new HashMap (); 844 GenericDelegator delegator = dctx.getDelegator(); 845 String entityName = (String )context.get("entityName"); 846 String action = (String )context.get("action"); 847 String pkFieldCount = (String )context.get("pkFieldCount"); 848 Map pkFields = new HashMap (); 849 int fieldCount = Integer.parseInt(pkFieldCount); 850 for (int i=0; i<fieldCount; i++) { 851 String fieldName = (String )context.get("fieldName" + i); 852 String fieldValue = (String )context.get("fieldValue" + i); 853 if (UtilValidate.isEmpty(fieldValue)) { 854 return results; 858 } 859 pkFields.put(fieldName, fieldValue); 860 } 861 boolean doLink = (action != null && action.equalsIgnoreCase("Y")) ? true : false; 862 if (Debug.infoOn()) Debug.logInfo("in updateOrRemove, context:" + context, module); 863 try { 864 GenericValue entityValuePK = delegator.makeValue(entityName, pkFields); 865 if (Debug.infoOn()) Debug.logInfo("in updateOrRemove, entityValuePK:" + entityValuePK, module); 866 GenericValue entityValueExisting = delegator.findByPrimaryKeyCache(entityName, entityValuePK); 867 if (Debug.infoOn()) Debug.logInfo("in updateOrRemove, entityValueExisting:" + entityValueExisting, module); 868 if (entityValueExisting == null) { 869 if (doLink) { 870 entityValuePK.create(); 871 if (Debug.infoOn()) Debug.logInfo("in updateOrRemove, entityValuePK: CREATED", module); 872 } 873 } else { 874 if (!doLink) { 875 entityValueExisting.remove(); 876 if (Debug.infoOn()) Debug.logInfo("in updateOrRemove, entityValueExisting: REMOVED", module); 877 } 878 } 879 880 } catch (GenericEntityException e) { 881 Debug.logError(e, module); 882 return ServiceUtil.returnError(e.getMessage()); 883 } 884 return results; 885 } 886 887 public static Map resequence(DispatchContext dctx, Map context) throws GenericServiceException{ 888 889 HashMap result = new HashMap (); 890 GenericDelegator delegator = dctx.getDelegator(); 891 String contentIdTo = (String )context.get("contentIdTo"); 892 Integer seqInc = (Integer )context.get("seqInc"); 893 if (seqInc == null) 894 seqInc = new Integer (100); 895 int seqIncrement = seqInc.intValue(); 896 List typeList = (List )context.get("typeList"); 897 if (typeList == null) typeList = new ArrayList (); 898 String contentAssocTypeId = (String )context.get("contentAssocTypeId"); 899 if (UtilValidate.isNotEmpty(contentAssocTypeId)) typeList.add(contentAssocTypeId); 900 if (UtilValidate.isEmpty(typeList)) typeList = UtilMisc.toList("PUBLISH_LINK", "SUB_CONTENT"); 901 List condList = new ArrayList (); 902 Iterator iterType = typeList.iterator(); 903 while (iterType.hasNext()) { 904 String type = (String )iterType.next(); 905 condList.add(new EntityExpr("contentAssocTypeId", EntityOperator.EQUALS, type)); 906 } 907 908 EntityCondition conditionType = new EntityConditionList(condList, EntityOperator.OR); 909 EntityCondition conditionMain = new EntityConditionList(UtilMisc.toList( new EntityExpr("contentIdTo", EntityOperator.EQUALS, contentIdTo), conditionType), EntityOperator.AND); 910 try { 911 List listAll = delegator.findByCondition("ContentAssoc", conditionMain, null, UtilMisc.toList("sequenceNum", "fromDate", "createdDate")); 912 List listFiltered = EntityUtil.filterByDate(listAll); 913 String contentId = (String )context.get("contentId"); 914 String dir = (String )context.get("dir"); 915 int seqNum = seqIncrement; 916 String thisContentId = null; 917 for (int i=0; i < listFiltered.size(); i++) { 918 GenericValue contentAssoc = (GenericValue)listFiltered.get(i); 919 if (UtilValidate.isNotEmpty(contentId) && UtilValidate.isNotEmpty(dir)) { 920 thisContentId = contentAssoc.getString("contentId"); 922 if (contentId.equals(thisContentId)) { 923 if (dir.startsWith("up")) { 924 if (i > 0) { 925 try { 927 GenericValue prevValue = (GenericValue)listFiltered.get(i-1); 928 Long prevSeqNum = (Long )prevValue.get("sequenceNum"); 929 prevValue.put("sequenceNum", new Long (seqNum)); 930 prevValue.store(); 931 contentAssoc.put("sequenceNum", prevSeqNum); 932 contentAssoc.store(); 933 } catch (Exception e) { 934 return ServiceUtil.returnError(e.getMessage()); 935 } 936 } 937 } else { 938 if (i < listFiltered.size()) { 939 GenericValue nextValue = (GenericValue)listFiltered.get(i+1); 941 nextValue.put("sequenceNum", new Long (seqNum)); 942 nextValue.store(); 943 seqNum += seqIncrement; 944 contentAssoc.put("sequenceNum", new Long (seqNum)); 945 contentAssoc.store(); 946 i++; } 948 } 949 } else { 950 contentAssoc.put("sequenceNum", new Long (seqNum)); 951 contentAssoc.store(); 952 } 953 } else { 954 contentAssoc.put("sequenceNum", new Long (seqNum)); 955 contentAssoc.store(); 956 } 957 seqNum += seqIncrement; 958 } 959 } catch(GenericEntityException e) { 960 Debug.logError(e, module); 961 return ServiceUtil.returnError(e.getMessage()); 962 } 963 964 965 return result; 966 } 967 968 public static Map changeLeafToNode(DispatchContext dctx, Map context) throws GenericServiceException{ 969 Map result = new HashMap (); 970 GenericDelegator delegator = dctx.getDelegator(); 971 LocalDispatcher dispatcher = dctx.getDispatcher(); 972 String contentId = (String )context.get("contentId"); 973 GenericValue userLogin = (GenericValue)context.get("userLogin"); 974 String userLoginId = userLogin.getString("userLoginId"); 975 try { 977 GenericValue content = delegator.findByPrimaryKey("Content", UtilMisc.toMap("contentId", contentId)); 978 if (content == null) { 979 Debug.logError("content was null", module); 980 return ServiceUtil.returnError("content was null"); 981 } 982 String dataResourceId = content.getString("dataResourceId"); 983 994 995 content.set("dataResourceId", null); 996 content.set("lastModifiedDate", UtilDateTime.nowTimestamp()); 997 content.set("lastModifiedByUserLogin", userLoginId); 998 content.store(); 999 1000 if (UtilValidate.isNotEmpty(dataResourceId)) { 1001 GenericValue contentClone = (GenericValue)content.clone(); 1003 contentClone.set("dataResourceId", dataResourceId); 1004 content.set("lastModifiedDate", UtilDateTime.nowTimestamp()); 1005 content.set("lastModifiedByUserLogin", userLoginId); 1006 content.set("createdDate", UtilDateTime.nowTimestamp()); 1007 content.set("createdByUserLogin", userLoginId); 1008 1009 contentClone.set("contentId", null); 1010 ModelService modelService = dctx.getModelService("persistContentAndAssoc"); 1011 Map serviceIn = modelService.makeValid(contentClone, "IN"); 1012 serviceIn.put("userLogin", userLogin); 1013 serviceIn.put("contentIdTo", contentId); 1014 serviceIn.put("contentAssocTypeId", "SUB_CONTENT"); 1015 serviceIn.put("sequenceNum", new Long (50)); 1016 try { 1017 Map thisResult = dispatcher.runSync("persistContentAndAssoc", serviceIn); 1018 } catch(ServiceAuthException e) { 1019 return ServiceUtil.returnError(e.getMessage()); 1020 } 1021 1022 List typeList = UtilMisc.toList("SUB_CONTENT"); 1023 int leafCount = ContentManagementWorker.updateStatsTopDown(delegator, contentId, typeList); 1024 } 1025 1026 } catch(GenericEntityException e) { 1027 Debug.logError(e, module); 1028 return ServiceUtil.returnError(e.getMessage()); 1029 } 1030 1031 1032 return result; 1033 } 1034 1035 public static Map updateLeafCount(DispatchContext dctx, Map context) throws GenericServiceException{ 1036 1037 Map result = new HashMap (); 1038 GenericDelegator delegator = dctx.getDelegator(); 1039 List typeList = (List )context.get("typeList"); 1040 if (typeList == null) 1041 typeList = UtilMisc.toList("PUBLISH_LINK", "SUB_CONTENT"); 1042 String startContentId = (String )context.get("contentId"); 1043 try { 1044 int leafCount = ContentManagementWorker.updateStatsTopDown(delegator, startContentId, typeList); 1045 result.put("leafCount", new Integer (leafCount)); 1046 } catch(GenericEntityException e) { 1047 Debug.logError(e, module); 1048 return ServiceUtil.returnError(e.getMessage()); 1049 } 1050 return result; 1051 } 1052 1053 1116 1117 1127 public static Map updatePageType(DispatchContext dctx, Map context) throws GenericServiceException{ 1128 1129 GenericDelegator delegator = dctx.getDelegator(); 1130 Map results = new HashMap (); 1131 Set visitedSet = (Set )context.get("visitedSet"); 1132 if (visitedSet == null) { 1133 visitedSet = new HashSet (); 1134 context.put("visitedSet", visitedSet); 1135 } 1136 String pageMode = (String )context.get("pageMode"); 1137 String contentId = (String )context.get("contentId"); 1138 visitedSet.add(contentId); 1139 String contentTypeId = "PAGE_NODE"; 1140 if (pageMode != null && pageMode.toLowerCase().indexOf("outline") >= 0) 1141 contentTypeId = "OUTLINE_NODE"; 1142 GenericValue thisContent = null; 1143 try { 1144 thisContent = delegator.findByPrimaryKey("Content", UtilMisc.toMap("contentId", contentId)); 1145 if (thisContent == null) 1146 ServiceUtil.returnError("No entity found for id=" + contentId); 1147 thisContent.set("contentTypeId", contentTypeId); 1148 thisContent.store(); 1149 List kids = ContentWorker.getAssociatedContent(thisContent, "from", UtilMisc.toList("SUB_CONTENT"), null, null, null); 1150 Iterator iter = kids.iterator(); 1151 while (iter.hasNext()) { 1152 GenericValue kidContent = (GenericValue)iter.next(); 1153 if (contentTypeId.equals("OUTLINE_NODE")) { 1154 updateOutlineNodeChildren(kidContent, false, context); 1155 } else { 1156 updatePageNodeChildren(kidContent, context); 1157 } 1158 } 1159 } catch(GenericEntityException e) { 1160 Debug.logError(e, module); 1161 return ServiceUtil.returnError(e.getMessage()); 1162 } 1163 1164 return results; 1165 } 1166 1167 public static Map resetToOutlineMode(DispatchContext dctx, Map context) throws GenericServiceException{ 1168 1169 GenericDelegator delegator = dctx.getDelegator(); 1170 Map results = new HashMap (); 1171 Set visitedSet = (Set )context.get("visitedSet"); 1172 if (visitedSet == null) { 1173 visitedSet = new HashSet (); 1174 context.put("visitedSet", visitedSet); 1175 } 1176 String contentId = (String )context.get("contentId"); 1177 String pageMode = (String )context.get("pageMode"); 1178 String contentTypeId = "OUTLINE_NODE"; 1179 if (pageMode != null && pageMode.toLowerCase().indexOf("page") >= 0) 1180 contentTypeId = "PAGE_NODE"; 1181 GenericValue thisContent = null; 1182 try { 1183 thisContent = delegator.findByPrimaryKey("Content", UtilMisc.toMap("contentId", contentId)); 1184 if (thisContent == null) 1185 ServiceUtil.returnError("No entity found for id=" + contentId); 1186 thisContent.set("contentTypeId", "OUTLINE_NODE"); 1187 thisContent.store(); 1188 List kids = ContentWorker.getAssociatedContent(thisContent, "from", UtilMisc.toList("SUB_CONTENT"), null, null, null); 1189 Iterator iter = kids.iterator(); 1190 while (iter.hasNext()) { 1191 GenericValue kidContent = (GenericValue)iter.next(); 1192 if (contentTypeId.equals("OUTLINE_NODE")) { 1193 updateOutlineNodeChildren(kidContent, true, context); 1194 } else { 1195 kidContent.put("contentTypeId", "PAGE_NODE"); 1196 kidContent.store(); 1197 List kids2 = ContentWorker.getAssociatedContent(kidContent, "from", UtilMisc.toList("SUB_CONTENT"), null, null, null); 1198 Iterator iter2 = kids.iterator(); 1199 while (iter2.hasNext()) { 1200 GenericValue kidContent2 = (GenericValue)iter2.next(); 1201 updatePageNodeChildren(kidContent2, context); 1202 } 1203 } 1204 } 1205 } catch(GenericEntityException e) { 1206 Debug.logError(e, module); 1207 return ServiceUtil.returnError(e.getMessage()); 1208 } 1209 1210 return results; 1211 } 1212 1213 public static Map clearContentAssocViewCache(DispatchContext dctx, Map context) throws GenericServiceException{ 1214 Map results = new HashMap (); 1215 1216 UtilCache utilCache = (UtilCache) UtilCache.utilCacheTable.get("entitycache.entity-list.default.ContentAssocViewFrom"); 1217 1218 if (utilCache != null) { 1219 utilCache.clear(); 1220 } 1221 1222 utilCache = (UtilCache) UtilCache.utilCacheTable.get("entitycache.entity-list.default.ContentAssocViewTo"); 1223 if (utilCache != null) { 1224 utilCache.clear(); 1225 } 1226 1227 return results; 1228 } 1229 1230 public static Map clearContentAssocDataResourceViewCache(DispatchContext dctx, Map context) throws GenericServiceException{ 1231 1232 Map results = new HashMap (); 1233 1234 UtilCache utilCache = (UtilCache) UtilCache.utilCacheTable.get("entitycache.entity-list.default.ContentAssocViewDataResourceFrom"); 1235 if (utilCache != null) { 1236 utilCache.clear(); 1237 } 1238 1239 utilCache = (UtilCache) UtilCache.utilCacheTable.get("entitycache.entity-list.default.ContentAssocViewDataResourceTo"); 1240 if (utilCache != null) { 1241 utilCache.clear(); 1242 } 1243 1244 return results; 1245 } 1246 1247 public static void updatePageNodeChildren(GenericValue content, Map context) throws GenericEntityException { 1248 1249 String contentId = content.getString("contentId"); 1250 Set visitedSet = (Set )context.get("visitedSet"); 1251 if (visitedSet == null) { 1252 visitedSet = new HashSet (); 1253 context.put("visitedSet", visitedSet); 1254 } else { 1255 if (visitedSet.contains(contentId)) { 1256 Debug.logWarning("visitedSet already contains:" + contentId, module); 1257 return; 1258 } else { 1259 visitedSet.add(contentId); 1260 } 1261 } 1262 String contentTypeId = content.getString("contentTypeId"); 1263 String newContentTypeId = "SUBPAGE_NODE"; 1264 1270 content.put("contentTypeId", newContentTypeId); 1271 content.store(); 1272 1273 List kids = ContentWorker.getAssociatedContent(content, "from", UtilMisc.toList("SUB_CONTENT"), null, null, null); 1275 Iterator iter = kids.iterator(); 1276 while (iter.hasNext()) { 1277 GenericValue kidContent = (GenericValue)iter.next(); 1278 updatePageNodeChildren(kidContent, context); 1279 } 1280 return; 1282 } 1283 1284 public static void updateOutlineNodeChildren(GenericValue content, boolean forceOutline, Map context) throws GenericEntityException { 1285 1286 String contentId = content.getString("contentId"); 1287 Set visitedSet = (Set )context.get("visitedSet"); 1288 if (visitedSet == null) { 1289 visitedSet = new HashSet (); 1290 context.put("visitedSet", visitedSet); 1291 } else { 1292 if (visitedSet.contains(contentId)) { 1293 Debug.logWarning("visitedSet already contains:" + contentId, module); 1294 return; 1295 } else { 1296 visitedSet.add(contentId); 1297 } 1298 } 1299 String contentTypeId = content.getString("contentTypeId"); 1300 String newContentTypeId = contentTypeId; 1301 String dataResourceId = content.getString("dataResourceId"); 1302 Long branchCount = (Long )content.get("childBranchCount"); 1303 if (forceOutline) { 1304 newContentTypeId = "OUTLINE_NODE"; 1305 } else if (contentTypeId == null || contentTypeId.equals("DOCUMENT")) { 1306 if (UtilValidate.isEmpty(dataResourceId) || (branchCount != null && branchCount.intValue() > 0)) 1307 newContentTypeId = "OUTLINE_NODE"; 1308 else 1309 newContentTypeId = "PAGE_NODE"; 1310 } else if (contentTypeId.equals("SUBPAGE_NODE")) { 1311 newContentTypeId = "PAGE_NODE"; 1312 } 1313 1314 content.put("contentTypeId", newContentTypeId); 1315 content.store(); 1316 1317 if (contentTypeId == null || contentTypeId.equals("DOCUMENT") || contentTypeId.equals("OUTLINE_NODE")) { 1318 List kids = ContentWorker.getAssociatedContent(content, "from", UtilMisc.toList("SUB_CONTENT"), null, null, null); 1320 Iterator iter = kids.iterator(); 1321 while (iter.hasNext()) { 1322 GenericValue kidContent = (GenericValue)iter.next(); 1323 updateOutlineNodeChildren(kidContent, forceOutline, context); 1324 } 1325 } 1326 return; 1327 } 1328 1329 public static Map findSubNodes(DispatchContext dctx, Map context) throws GenericServiceException{ 1330 Map results = null; 1331 GenericDelegator delegator = dctx.getDelegator(); 1332 String contentIdTo = (String )context.get("contentId"); 1333 List condList = new ArrayList (); 1334 EntityExpr expr = new EntityExpr("caContentIdTo", EntityOperator.EQUALS, contentIdTo); 1335 condList.add(expr); 1336 expr = new EntityExpr("caContentAssocTypeId", EntityOperator.EQUALS, "SUB_CONTENT"); 1337 condList.add(expr); 1338 expr = new EntityExpr("caThruDate", EntityOperator.EQUALS, null); 1339 condList.add(expr); 1340 EntityConditionList entityCondList = new EntityConditionList(condList, EntityOperator.AND); 1341 try { 1342 List lst = delegator.findByCondition("ContentAssocDataResourceViewFrom", entityCondList, null, UtilMisc.toList("caSequenceNum", "caFromDate", "createdDate")); 1343 results.put("_LIST_", lst); 1344 } catch(GenericEntityException e) { 1345 Debug.logError(e, module); 1346 return ServiceUtil.returnError(e.getMessage()); 1347 } 1348 return results; 1349 } 1350 1351 public static String updateTypeAndFile(GenericValue dataResource, Map context) { 1352 String retVal = null; 1353 String mimeTypeId = (String ) context.get("_imageData_contentType"); 1354 String fileName = (String ) context.get("_imageData_fileName"); 1355 try { 1356 if (UtilValidate.isNotEmpty(fileName)) 1357 dataResource.set("objectInfo", fileName); 1358 if (UtilValidate.isNotEmpty(mimeTypeId)) 1359 dataResource.set("mimeTypeId", mimeTypeId); 1360 dataResource.store(); 1361 } catch (GenericEntityException e) { 1362 retVal = "Unable to update the DataResource record"; 1363 } 1364 return retVal; 1365 } 1366 1367 public static Map initContentChildCounts(DispatchContext dctx, Map context) throws GenericServiceException{ 1368 Map result = new HashMap (); 1369 GenericDelegator delegator = dctx.getDelegator(); 1370 1371 GenericValue content = (GenericValue)context.get("content"); 1372 if (content == null) { 1373 return ServiceUtil.returnError("No Content found."); 1374 } 1375 Long leafCount = (Long )content.get("childLeafCount"); 1376 if (leafCount == null) { 1377 content.set("childLeafCount", new Long (0)); 1378 } 1379 Long branchCount = (Long )content.get("childBranchCount"); 1380 if (branchCount == null) { 1381 content.set("childBranchCount", new Long (0)); 1382 } 1383 1384 1386 return result; 1387 } 1388 1389 public static Map incrementContentChildStats(DispatchContext dctx, Map context) throws GenericServiceException{ 1390 Map result = new HashMap (); 1391 GenericDelegator delegator = dctx.getDelegator(); 1392 1393 String contentId = (String )context.get("contentId"); 1394 String contentAssocTypeId = (String )context.get("contentAssocTypeId"); 1395 1396 try { 1397 GenericValue content = delegator.findByPrimaryKeyCache("Content", UtilMisc.toMap("contentId", contentId)); 1398 if (content == null) { 1399 return ServiceUtil.returnError("No Content found."); 1400 } 1401 Long leafCount = (Long )content.get("childLeafCount"); 1402 if (leafCount == null) { 1403 leafCount = new Long (0); 1404 } 1405 int changeLeafCount = leafCount.intValue() + 1; 1406 int changeBranchCount = 1; 1407 1408 ContentManagementWorker.updateStatsBottomUp(delegator, contentId, UtilMisc.toList(contentAssocTypeId), changeBranchCount, changeLeafCount); 1409 } catch(GenericEntityException e) { 1410 return ServiceUtil.returnError(e.getMessage()); 1411 } 1412 return result; 1413 } 1414 1415 public static Map decrementContentChildStats(DispatchContext dctx, Map context) throws GenericServiceException{ 1416 Map result = new HashMap (); 1417 GenericDelegator delegator = dctx.getDelegator(); 1418 1419 String contentId = (String )context.get("contentId"); 1420 String contentAssocTypeId = (String )context.get("contentAssocTypeId"); 1421 1422 try { 1423 GenericValue content = delegator.findByPrimaryKeyCache("Content", UtilMisc.toMap("contentId", contentId)); 1424 if (content == null) { 1425 return ServiceUtil.returnError("No Content found."); 1426 } 1427 Long leafCount = (Long )content.get("childLeafCount"); 1428 if (leafCount == null) { 1429 leafCount = new Long (0); 1430 } 1431 int changeLeafCount = -1 * leafCount.intValue() - 1; 1432 int changeBranchCount = -1; 1433 1434 ContentManagementWorker.updateStatsBottomUp(delegator, contentId, UtilMisc.toList(contentAssocTypeId), changeBranchCount, changeLeafCount); 1435 } catch(GenericEntityException e) { 1436 return ServiceUtil.returnError(e.getMessage()); 1437 } 1438 return result; 1439 } 1440 1441 public static Map updateContentChildStats(DispatchContext dctx, Map context) throws GenericServiceException{ 1442 Map result = new HashMap (); 1443 GenericDelegator delegator = dctx.getDelegator(); 1444 1445 String contentId = (String )context.get("contentId"); 1446 String contentAssocTypeId = (String )context.get("contentAssocTypeId"); 1447 List typeList = new ArrayList (); 1448 if (UtilValidate.isNotEmpty(contentAssocTypeId)) { 1449 typeList.add(contentAssocTypeId); 1450 } else { 1451 typeList = UtilMisc.toList("PUBLISH_LINK", "SUB_CONTENT"); 1452 } 1453 1454 try { 1455 ContentManagementWorker.updateStatsTopDown(delegator, contentId, typeList); 1456 } catch(GenericEntityException e) { 1457 return ServiceUtil.returnError(e.getMessage()); 1458 } 1459 return result; 1460 } 1461 1462 public static Map updateSubscription(DispatchContext dctx, Map context) throws GenericServiceException{ 1463 Map result = new HashMap (); 1464 GenericDelegator delegator = dctx.getDelegator(); 1465 LocalDispatcher dispatcher = dctx.getDispatcher(); 1466 Timestamp nowTimestamp = UtilDateTime.nowTimestamp(); 1467 1468 String partyId = (String ) context.get("partyId"); 1469 String webPubPt = (String ) context.get("contentId"); 1470 String roleTypeId = (String ) context.get("useRoleTypeId"); 1471 GenericValue userLogin = (GenericValue) context.get("userLogin"); 1472 Integer useTime = (Integer ) context.get("useTime"); 1473 String useTimeUomId = (String ) context.get("useTimeUomId"); 1474 boolean hasExistingContentRole = false; 1475 GenericValue contentRole = null; 1476 try { 1477 List contentRoleList = delegator.findByAndCache("ContentRole", UtilMisc.toMap("partyId", partyId, "contentId", webPubPt, "roleTypeId", roleTypeId)); 1478 List listFiltered = EntityUtil.filterByDate(contentRoleList, true); 1479 List listOrdered = EntityUtil.orderBy(listFiltered, UtilMisc.toList("fromDate DESC")); 1480 if (listOrdered.size() > 0) { 1481 contentRole = (GenericValue) listOrdered.get(0); 1482 hasExistingContentRole = true; 1483 } 1484 } catch (GenericEntityException e) { 1485 return ServiceUtil.returnError(e.getMessage()); 1486 } 1487 1488 if (contentRole == null) { 1489 contentRole = delegator.makeValue("ContentRole", null); 1490 contentRole.set("contentId", webPubPt); 1491 contentRole.set("partyId", partyId); 1492 contentRole.set("roleTypeId", roleTypeId); 1493 contentRole.set("fromDate", nowTimestamp); 1494 } 1495 1496 Timestamp thruDate = (Timestamp ) contentRole.get("thruDate"); 1497 if (thruDate == null) { 1498 thruDate = nowTimestamp; 1500 } else { 1501 if (thruDate.before(nowTimestamp)) { 1504 thruDate = nowTimestamp; 1505 } 1506 } 1507 Calendar calendar = Calendar.getInstance(); 1508 calendar.setTime(thruDate); 1509 int field = Calendar.MONTH; 1510 if ("TF_day".equals(useTimeUomId)) { 1511 field = Calendar.DAY_OF_YEAR; 1512 } else if ("TF_wk".equals(useTimeUomId)) { 1513 field = Calendar.WEEK_OF_YEAR; 1514 } else if ("TF_mon".equals(useTimeUomId)) { 1515 field = Calendar.MONTH; 1516 } else if ("TF_yr".equals(useTimeUomId)) { 1517 field = Calendar.YEAR; 1518 } else { 1519 Debug.logWarning("Don't know anything about useTimeUomId [" + useTimeUomId + "], defaulting to month", module); 1520 } 1521 calendar.add(field, useTime.intValue()); 1522 thruDate = new Timestamp (calendar.getTimeInMillis()); 1523 contentRole.set("thruDate", thruDate); 1524 try { 1525 if (hasExistingContentRole) { 1526 contentRole.store(); 1527 } else { 1528 Map map = new HashMap (); 1529 map.put("partyId", partyId); 1530 map.put("roleTypeId", roleTypeId); 1531 map.put("userLogin", userLogin); 1532 Map thisResult = dispatcher.runSync("createPartyRole", map); 1533 contentRole.create(); 1534 } 1535 } catch (GenericEntityException e) { 1536 return ServiceUtil.returnError(e.getMessage()); 1537 } 1538 return result; 1539 } 1540 1541 public static Map updateSubscriptionByProduct(DispatchContext dctx, Map context) throws GenericServiceException{ 1542 Map result = new HashMap (); 1543 GenericDelegator delegator = dctx.getDelegator(); 1544 LocalDispatcher dispatcher = dctx.getDispatcher(); 1545 String productId = (String ) context.get("productId"); 1546 Integer qty = (Integer ) context.get("quantity"); 1547 if (qty == null) { 1548 qty = new Integer (1); 1549 } 1550 1551 Timestamp orderCreatedDate = (Timestamp ) context.get("orderCreatedDate"); 1552 if (orderCreatedDate == null) { 1553 orderCreatedDate = UtilDateTime.nowTimestamp(); 1554 } 1555 GenericValue productContent = null; 1556 try { 1557 List lst = delegator.findByAndCache("ProductContent", UtilMisc.toMap("productId", productId, "productContentTypeId", "ONLINE_ACCESS")); 1558 List listFiltered = EntityUtil.filterByDate(lst, orderCreatedDate, "purchaseFromDate", "purchaseThruDate", true); 1559 List listOrdered = EntityUtil.orderBy(listFiltered, UtilMisc.toList("purchaseFromDate", "purchaseThruDate")); 1560 List listThrusOnly = EntityUtil.filterOutByCondition(listOrdered, new EntityExpr("purchaseThruDate", EntityOperator.EQUALS, null)); 1561 if (listThrusOnly.size() > 0) { 1562 productContent = (GenericValue) listThrusOnly.get(0); 1563 } else { 1564 productContent = (GenericValue) listOrdered.get(0); 1565 } 1566 } catch(GenericEntityException e) { 1567 Debug.logError(e.getMessage(), module); 1568 return ServiceUtil.returnError(e.getMessage()); 1569 } 1570 if (productContent == null) { 1571 String msg = "No ProductContent found for productId:" + productId; 1572 Debug.logError(msg, module); 1573 return ServiceUtil.returnError(msg); 1574 } 1575 Long useTime = (Long )productContent.get("useTime"); 1576 Integer newUseTime = new Integer (useTime.intValue() * qty.intValue()); 1577 context.put("useTime", newUseTime); 1578 context.put("useTimeUomId", productContent.get("useTimeUomId")); 1579 context.put("useRoleTypeId", productContent.get("useRoleTypeId")); 1580 context.put("contentId", productContent.get("contentId")); 1581 ModelService subscriptionModel = dispatcher.getDispatchContext().getModelService("updateSubscription"); 1582 Map ctx = subscriptionModel.makeValid(context, "IN"); 1583 result = dispatcher.runSync("updateSubscription", ctx); 1584 return result; 1585 } 1586 1587 public static Map updateSubscriptionByOrder(DispatchContext dctx, Map context) throws GenericServiceException{ 1588 Map result = new HashMap (); 1589 GenericDelegator delegator = dctx.getDelegator(); 1590 LocalDispatcher dispatcher = dctx.getDispatcher(); 1591 String orderId = (String ) context.get("orderId"); 1592 1593 Debug.logInfo("In updateSubscriptionByOrder service with orderId: " + orderId, module); 1594 1595 GenericValue orderHeader = null; 1596 try { 1597 List orderRoleList = delegator.findByAnd("OrderRole", UtilMisc.toMap("orderId", orderId, "roleTypeId", "END_USER_CUSTOMER")); 1598 if (orderRoleList.size() > 0 ) { 1599 GenericValue orderRole = (GenericValue)orderRoleList.get(0); 1600 String partyId = (String ) orderRole.get("partyId"); 1601 context.put("partyId", partyId); 1602 } else { 1603 String msg = "No OrderRole found for orderId:" + orderId; 1604 Debug.logError(msg, module); 1605 return ServiceUtil.returnError(msg); 1606 1607 } 1608 orderHeader = delegator.findByPrimaryKeyCache("OrderHeader", UtilMisc.toMap("orderId", orderId)); 1609 if (orderHeader == null) { 1610 String msg = "No OrderHeader found for orderId:" + orderId; 1611 Debug.logError(msg, module); 1612 return ServiceUtil.returnError(msg); 1613 } 1614 Timestamp orderCreatedDate = (Timestamp ) orderHeader.get("orderDate"); 1615 context.put("orderCreatedDate", orderCreatedDate); 1616 List orderItemList = orderHeader.getRelated("OrderItem"); 1617 Iterator orderItemIter = orderItemList.iterator(); 1618 ModelService subscriptionModel = dispatcher.getDispatchContext().getModelService("updateSubscriptionByProduct"); 1619 while (orderItemIter.hasNext()) { 1620 GenericValue orderItem = (GenericValue)orderItemIter.next(); 1621 Double qty = (Double ) orderItem.get("quantity"); 1622 String productId = (String ) orderItem.get("productId"); 1623 List productContentList = delegator.findByAnd("ProductContent", UtilMisc.toMap("productId", productId, "productContentTypeId", "ONLINE_ACCESS")); 1624 List productContentListFiltered = EntityUtil.filterByDate(productContentList); 1625 if (productContentListFiltered.size() > 0) { 1626 context.put("productId", productId); 1627 context.put("quantity", new Integer (qty.intValue())); 1628 Map ctx = subscriptionModel.makeValid(context, "IN"); 1629 Map thisResult = dispatcher.runSync("updateSubscriptionByProduct", ctx); 1630 } 1631 } 1632 } catch(GenericEntityException e) { 1633 Debug.logError(e.getMessage(), module); 1634 return ServiceUtil.returnError(e.getMessage()); 1635 } 1636 return result; 1637 } 1638 1639 public static Map followNodeChildren(DispatchContext dctx, Map context) throws GenericServiceException{ 1640 1641 Map result = null; 1642 GenericDelegator delegator = dctx.getDelegator(); 1643 LocalDispatcher dispatcher = dctx.getDispatcher(); 1644 Security security = dctx.getSecurity(); 1645 GenericValue userLogin = (GenericValue)context.get("userLogin"); 1646 if (!security.hasEntityPermission("CONTENTMGR", "_ADMIN", userLogin)) { 1647 return ServiceUtil.returnError("Permission denied."); 1648 } 1649 String contentId = (String )context.get("contentId"); 1650 String serviceName = (String )context.get("serviceName"); 1651 String contentAssocTypeId = (String )context.get("contentAssocTypeId"); 1652 List contentAssocTypeIdList = new ArrayList (); 1653 if (UtilValidate.isNotEmpty(contentAssocTypeId)) { 1654 contentAssocTypeIdList = StringUtil.split(contentAssocTypeId, "|"); 1655 } 1656 if (contentAssocTypeIdList.size() == 0) { 1657 contentAssocTypeIdList.add("SUB_CONTENT"); 1658 } 1659 Map ctx = new HashMap (); 1660 ctx.put("userLogin", userLogin); 1661 ctx.put("contentAssocTypeIdList", contentAssocTypeIdList); 1662 try { 1663 1664 GenericValue content = delegator.findByPrimaryKey("Content", UtilMisc.toMap("contentId", contentId)); 1665 result = followNodeChildrenMethod(content, dispatcher, serviceName, ctx); 1666 } catch(GenericEntityException e) { 1667 Debug.logError(e.getMessage(), module); 1668 return ServiceUtil.returnError(e.getMessage()); 1669 } 1670 return result; 1671 } 1672 public static Map followNodeChildrenMethod(GenericValue content, LocalDispatcher dispatcher, String serviceName, Map context) 1673 throws GenericEntityException, GenericServiceException { 1674 1675 Map result = null; 1676 String contentId = content.getString("contentId"); 1677 List contentAssocTypeIdList = (List )context.get("contentAssocTypeIdList" ); 1678 Set visitedSet = (Set )context.get("visitedSet"); 1679 if (visitedSet == null) { 1680 visitedSet = new HashSet (); 1681 context.put("visitedSet", visitedSet); 1682 } else { 1683 if (visitedSet.contains(contentId)) { 1684 Debug.logWarning("visitedSet already contains:" + contentId, module); 1685 return ServiceUtil.returnError("visitedSet already contains:" + contentId); 1686 } else { 1687 visitedSet.add(contentId); 1688 } 1689 } 1690 1691 GenericValue userLogin = (GenericValue)context.get("userLogin"); 1692 result = dispatcher.runSync(serviceName, UtilMisc.toMap("content", content, "userLogin", userLogin)); 1693 1694 List kids = ContentWorker.getAssociatedContent(content, "from", contentAssocTypeIdList, null, null, null); 1695 Iterator iter = kids.iterator(); 1696 while (iter.hasNext()) { 1697 GenericValue kidContent = (GenericValue)iter.next(); 1698 followNodeChildrenMethod(kidContent, dispatcher, serviceName, context); 1699 } 1700 return result; 1701 } 1702 1703 1705 public static Map persistContentWithRevision(DispatchContext dctx, Map context) { 1706 Map result = null; 1707 boolean dataResourceExists = false; 1708 GenericDelegator delegator = dctx.getDelegator(); 1709 LocalDispatcher dispatcher = dctx.getDispatcher(); 1710 GenericValue dataResource = null; 1711 String masterRevisionContentId = (String )context.get("masterRevisionContentId"); 1712 String oldDataResourceId = (String )context.get("drDataResourceId"); 1713 if (UtilValidate.isEmpty(oldDataResourceId)) { 1714 oldDataResourceId = (String )context.get("dataResourceId"); 1715 } 1716 if (UtilValidate.isNotEmpty(oldDataResourceId)) { 1717 try { 1718 dataResource = delegator.findByPrimaryKey("DataResource", UtilMisc.toMap("dataResourceId", oldDataResourceId)); 1719 } catch(GenericEntityException e) { 1720 Debug.logError(e.getMessage(), module); 1721 return ServiceUtil.returnError(e.getMessage()); 1722 } 1723 } 1724 1725 try { 1726 ModelService persistContentAndAssocModel = dispatcher.getDispatchContext().getModelService("persistContentAndAssoc"); 1727 Map ctx = persistContentAndAssocModel.makeValid(context, "IN"); 1728 if (dataResource != null) { 1729 ctx.remove("dataResourceId"); 1730 ctx.remove("drDataResourceId"); 1731 } 1732 result = dispatcher.runSync("persistContentAndAssoc", ctx); 1733 String errorMsg = ServiceUtil.getErrorMessage(result); 1734 if (UtilValidate.isNotEmpty(errorMsg)) { 1735 return ServiceUtil.returnError(errorMsg); 1736 } 1737 String contentId = (String )result.get("contentId"); 1738 List parentList = new ArrayList (); 1739 if (UtilValidate.isEmpty(masterRevisionContentId)) { 1740 Map traversMap = new HashMap (); 1741 traversMap.put("contentId", contentId); 1742 traversMap.put("direction", "To"); 1743 traversMap.put("contentAssocTypeId", "COMPDOC_PART"); 1744 Map traversResult = dispatcher.runSync("traverseContent", traversMap); 1745 parentList = (List )traversResult.get("parentList"); 1746 } else { 1747 parentList.add(masterRevisionContentId); 1748 } 1749 1750 Map contentRevisionMap = new HashMap (); 1752 contentRevisionMap.put("itemContentId", contentId); 1753 contentRevisionMap.put("newDataResourceId", result.get("dataResourceId")); 1754 contentRevisionMap.put("oldDataResourceId", oldDataResourceId); 1755 for (int i=0; i < parentList.size(); i++) { 1757 String thisContentId = (String )parentList.get(i); 1758 contentRevisionMap.put("contentId", thisContentId); 1759 result = dispatcher.runSync("persistContentRevisionAndItem", contentRevisionMap); 1760 errorMsg = ServiceUtil.getErrorMessage(result); 1761 if (UtilValidate.isNotEmpty(errorMsg)) { 1762 return ServiceUtil.returnError(errorMsg); 1763 } 1764 } 1765 1766 } catch (GenericServiceException e) { 1767 Debug.logError(e.getMessage(), module); 1768 return ServiceUtil.returnError(e.getMessage()); 1769 } 1770 return result; 1771 } 1772 1773} 1774 | Popular Tags |