001 /** 002 * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved. 003 * 004 * The contents of this file are subject to the terms of the Liferay Enterprise 005 * Subscription License ("License"). You may not use this file except in 006 * compliance with the License. You can obtain a copy of the License by 007 * contacting Liferay, Inc. See the License for the specific language governing 008 * permissions and limitations under the License, including but not limited to 009 * distribution rights of the Software. 010 * 011 * 012 * 013 */ 014 015 package com.liferay.portlet.dynamicdatamapping.service.impl; 016 017 import com.liferay.portal.kernel.exception.PortalException; 018 import com.liferay.portal.kernel.exception.SystemException; 019 import com.liferay.portal.kernel.log.Log; 020 import com.liferay.portal.kernel.log.LogFactoryUtil; 021 import com.liferay.portal.kernel.systemevent.SystemEvent; 022 import com.liferay.portal.kernel.template.TemplateConstants; 023 import com.liferay.portal.kernel.util.FileUtil; 024 import com.liferay.portal.kernel.util.LocaleUtil; 025 import com.liferay.portal.kernel.util.OrderByComparator; 026 import com.liferay.portal.kernel.util.PropsKeys; 027 import com.liferay.portal.kernel.util.StringPool; 028 import com.liferay.portal.kernel.util.StringUtil; 029 import com.liferay.portal.kernel.util.Validator; 030 import com.liferay.portal.model.Group; 031 import com.liferay.portal.model.Image; 032 import com.liferay.portal.model.ResourceConstants; 033 import com.liferay.portal.model.SystemEventConstants; 034 import com.liferay.portal.model.User; 035 import com.liferay.portal.security.auth.CompanyThreadLocal; 036 import com.liferay.portal.service.ServiceContext; 037 import com.liferay.portal.service.persistence.ImageUtil; 038 import com.liferay.portal.util.PortalUtil; 039 import com.liferay.portal.util.PrefsPropsUtil; 040 import com.liferay.portlet.dynamicdatamapping.NoSuchTemplateException; 041 import com.liferay.portlet.dynamicdatamapping.RequiredTemplateException; 042 import com.liferay.portlet.dynamicdatamapping.TemplateDuplicateTemplateKeyException; 043 import com.liferay.portlet.dynamicdatamapping.TemplateNameException; 044 import com.liferay.portlet.dynamicdatamapping.TemplateScriptException; 045 import com.liferay.portlet.dynamicdatamapping.TemplateSmallImageNameException; 046 import com.liferay.portlet.dynamicdatamapping.TemplateSmallImageSizeException; 047 import com.liferay.portlet.dynamicdatamapping.model.DDMStructure; 048 import com.liferay.portlet.dynamicdatamapping.model.DDMTemplate; 049 import com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants; 050 import com.liferay.portlet.dynamicdatamapping.service.base.DDMTemplateLocalServiceBaseImpl; 051 import com.liferay.portlet.dynamicdatamapping.util.DDMXMLUtil; 052 import com.liferay.portlet.journal.model.JournalArticle; 053 import com.liferay.portlet.journal.model.JournalArticleConstants; 054 import com.liferay.portlet.journal.service.persistence.JournalArticleUtil; 055 056 import java.io.File; 057 import java.io.IOException; 058 059 import java.util.ArrayList; 060 import java.util.Date; 061 import java.util.List; 062 import java.util.Locale; 063 import java.util.Map; 064 065 /** 066 * Provides the local service for accessing, adding, copying, deleting, and 067 * updating dynamic data mapping (DDM) templates. 068 * 069 * <p> 070 * DDM templates (templates) are used in Liferay to render templated content 071 * like applications, forms, dynamic data lists, or web contents. 072 * </p> 073 * 074 * <p> 075 * Templates support a variety of templating languages, like Velocity or 076 * FreeMarker. They also support multi-language names and descriptions. 077 * </p> 078 * 079 * <p> 080 * Templates can be related to many models in Liferay, such as those for 081 * structures, dynamic data lists, and applications. This relationship can be 082 * established via the class name ID and class PK. 083 * </p> 084 * 085 * @author Brian Wing Shun Chan 086 * @author Eduardo Lundgren 087 * @author Marcellus Tavares 088 */ 089 public class DDMTemplateLocalServiceImpl 090 extends DDMTemplateLocalServiceBaseImpl { 091 092 /** 093 * Adds a template. 094 * 095 * @param userId the primary key of the template's creator/owner 096 * @param groupId the primary key of the group 097 * @param classNameId the primary key of the class name for the template's 098 * related model 099 * @param classPK the primary key of the template's related entity 100 * @param nameMap the template's locales and localized names 101 * @param descriptionMap the template's locales and localized descriptions 102 * @param type the template's type. For more information, see {@link 103 * com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants}. 104 * @param mode the template's mode. For more information, see {@link 105 * com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants}. 106 * @param language the template's script language. For more information, 107 * see {@link 108 * com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants}. 109 * @param script the template's script 110 * @param serviceContext the service context to be applied. Can set the 111 * UUID, creation date, modification date, guest permissions, and 112 * group permissions for the template. 113 * @return the template 114 * @throws PortalException if a portal exception occurred 115 * @throws SystemException if a system exception occurred 116 */ 117 @Override 118 public DDMTemplate addTemplate( 119 long userId, long groupId, long classNameId, long classPK, 120 Map<Locale, String> nameMap, Map<Locale, String> descriptionMap, 121 String type, String mode, String language, String script, 122 ServiceContext serviceContext) 123 throws PortalException, SystemException { 124 125 return addTemplate( 126 userId, groupId, classNameId, classPK, null, nameMap, 127 descriptionMap, type, mode, language, script, false, false, null, 128 null, serviceContext); 129 } 130 131 /** 132 * Adds a template with additional parameters. 133 * 134 * @param userId the primary key of the template's creator/owner 135 * @param groupId the primary key of the group 136 * @param classNameId the primary key of the class name for the template's 137 * related model 138 * @param classPK the primary key of the template's related entity 139 * @param templateKey the unique string identifying the template 140 * (optionally <code>null</code>) 141 * @param nameMap the template's locales and localized names 142 * @param descriptionMap the template's locales and localized descriptions 143 * @param type the template's type. For more information, see {@link 144 * com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants}. 145 * @param mode the template's mode. For more information, see {@link 146 * com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants}. 147 * @param language the template's script language. For more information, 148 * see {@link 149 * com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants}. 150 * @param script the template's script 151 * @param cacheable whether the template is cacheable 152 * @param smallImage whether the template has a small image 153 * @param smallImageURL the template's small image URL (optionally 154 * <code>null</code>) 155 * @param smallImageFile the template's small image file (optionally 156 * <code>null</code>) 157 * @param serviceContext the service context to be applied. Can set the 158 * UUID, creation date, modification date, guest permissions, and 159 * group permissions for the template. 160 * @return the template 161 * @throws PortalException if a portal exception occurred 162 * @throws SystemException if a system exception occurred 163 */ 164 @Override 165 public DDMTemplate addTemplate( 166 long userId, long groupId, long classNameId, long classPK, 167 String templateKey, Map<Locale, String> nameMap, 168 Map<Locale, String> descriptionMap, String type, String mode, 169 String language, String script, boolean cacheable, 170 boolean smallImage, String smallImageURL, File smallImageFile, 171 ServiceContext serviceContext) 172 throws PortalException, SystemException { 173 174 // Template 175 176 User user = userPersistence.findByPrimaryKey(userId); 177 Date now = new Date(); 178 179 if (Validator.isNull(templateKey)) { 180 templateKey = String.valueOf(counterLocalService.increment()); 181 } 182 else { 183 templateKey = StringUtil.toUpperCase(templateKey.trim()); 184 } 185 186 script = formatScript(type, language, script); 187 188 byte[] smallImageBytes = null; 189 190 if (smallImage) { 191 try { 192 smallImageBytes = FileUtil.getBytes(smallImageFile); 193 } 194 catch (IOException ioe) { 195 } 196 197 if ((smallImageBytes == null) || Validator.isUrl(smallImageURL)) { 198 smallImage = false; 199 } 200 } 201 202 validate( 203 groupId, classNameId, templateKey, nameMap, script, smallImage, 204 smallImageURL, smallImageFile, smallImageBytes); 205 206 long templateId = counterLocalService.increment(); 207 208 DDMTemplate template = ddmTemplatePersistence.create(templateId); 209 210 template.setUuid(serviceContext.getUuid()); 211 template.setGroupId(groupId); 212 template.setCompanyId(user.getCompanyId()); 213 template.setUserId(user.getUserId()); 214 template.setUserName(user.getFullName()); 215 template.setCreateDate(serviceContext.getCreateDate(now)); 216 template.setModifiedDate(serviceContext.getModifiedDate(now)); 217 template.setClassNameId(classNameId); 218 template.setClassPK(classPK); 219 template.setTemplateKey(templateKey); 220 template.setNameMap(nameMap); 221 template.setDescriptionMap(descriptionMap); 222 template.setType(type); 223 template.setMode(mode); 224 template.setLanguage(language); 225 template.setScript(script); 226 template.setCacheable(cacheable); 227 template.setSmallImage(smallImage); 228 template.setSmallImageId(counterLocalService.increment()); 229 template.setSmallImageURL(smallImageURL); 230 231 ddmTemplatePersistence.update(template); 232 233 // Resources 234 235 if (serviceContext.isAddGroupPermissions() || 236 serviceContext.isAddGuestPermissions()) { 237 238 addTemplateResources( 239 template, serviceContext.isAddGroupPermissions(), 240 serviceContext.isAddGuestPermissions()); 241 } 242 else { 243 addTemplateResources( 244 template, serviceContext.getGroupPermissions(), 245 serviceContext.getGuestPermissions()); 246 } 247 248 // Small image 249 250 saveImages( 251 smallImage, template.getSmallImageId(), smallImageFile, 252 smallImageBytes); 253 254 return template; 255 } 256 257 /** 258 * Adds the resources to the template. 259 * 260 * @param template the template to add resources to 261 * @param addGroupPermissions whether to add group permissions 262 * @param addGuestPermissions whether to add guest permissions 263 * @throws PortalException if a portal exception occurred 264 * @throws SystemException if a system exception occurred 265 */ 266 @Override 267 public void addTemplateResources( 268 DDMTemplate template, boolean addGroupPermissions, 269 boolean addGuestPermissions) 270 throws PortalException, SystemException { 271 272 resourceLocalService.addResources( 273 template.getCompanyId(), template.getGroupId(), 274 template.getUserId(), DDMTemplate.class.getName(), 275 template.getTemplateId(), false, addGroupPermissions, 276 addGuestPermissions); 277 } 278 279 /** 280 * Adds the model resources with the permissions to the template. 281 * 282 * @param template the template to add resources to 283 * @param groupPermissions the group permissions to be added 284 * @param guestPermissions the guest permissions to be added 285 * @throws PortalException if a portal exception occurred 286 * @throws SystemException if a system exception occurred 287 */ 288 @Override 289 public void addTemplateResources( 290 DDMTemplate template, String[] groupPermissions, 291 String[] guestPermissions) 292 throws PortalException, SystemException { 293 294 resourceLocalService.addModelResources( 295 template.getCompanyId(), template.getGroupId(), 296 template.getUserId(), DDMTemplate.class.getName(), 297 template.getTemplateId(), groupPermissions, guestPermissions); 298 } 299 300 /** 301 * Copies the template, creating a new template with all the values 302 * extracted from the original one. This method supports defining a new name 303 * and description. 304 * 305 * @param userId the primary key of the template's creator/owner 306 * @param templateId the primary key of the template to be copied 307 * @param nameMap the new template's locales and localized names 308 * @param descriptionMap the new template's locales and localized 309 * descriptions 310 * @param serviceContext the service context to be applied. Can set the 311 * UUID, creation date, modification date, guest permissions, and 312 * group permissions for the template. 313 * @return the new template 314 * @throws PortalException if a portal exception occurred 315 * @throws SystemException if a system exception occurred 316 */ 317 @Override 318 public DDMTemplate copyTemplate( 319 long userId, long templateId, Map<Locale, String> nameMap, 320 Map<Locale, String> descriptionMap, ServiceContext serviceContext) 321 throws PortalException, SystemException { 322 323 DDMTemplate template = ddmTemplatePersistence.findByPrimaryKey( 324 templateId); 325 326 return copyTemplate( 327 userId, template, template.getClassPK(), nameMap, descriptionMap, 328 serviceContext); 329 } 330 331 @Override 332 public DDMTemplate copyTemplate( 333 long userId, long templateId, ServiceContext serviceContext) 334 throws PortalException, SystemException { 335 336 DDMTemplate template = ddmTemplatePersistence.findByPrimaryKey( 337 templateId); 338 339 return copyTemplate( 340 userId, template, template.getClassPK(), template.getNameMap(), 341 template.getDescriptionMap(), serviceContext); 342 } 343 344 /** 345 * Copies all the templates matching the class name ID, class PK, and type. 346 * This method creates new templates, extracting all the values from the old 347 * ones and updating their class PKs. 348 * 349 * @param userId the primary key of the template's creator/owner 350 * @param classNameId the primary key of the class name for the template's 351 * related model 352 * @param oldClassPK the primary key of the old template's related entity 353 * @param newClassPK the primary key of the new template's related entity 354 * @param type the template's type. For more information, see {@link 355 * com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants}. 356 * @param serviceContext the service context to be applied. Can set the 357 * creation date, modification date, guest permissions, and group 358 * permissions for the new templates. 359 * @return the new templates 360 * @throws PortalException if a portal exception occurred 361 * @throws SystemException if a system exception occurred 362 */ 363 @Override 364 public List<DDMTemplate> copyTemplates( 365 long userId, long classNameId, long oldClassPK, long newClassPK, 366 String type, ServiceContext serviceContext) 367 throws PortalException, SystemException { 368 369 List<DDMTemplate> newTemplates = new ArrayList<DDMTemplate>(); 370 371 List<DDMTemplate> oldTemplates = ddmTemplatePersistence.findByC_C_T( 372 classNameId, oldClassPK, type); 373 374 for (DDMTemplate oldTemplate : oldTemplates) { 375 DDMTemplate newTemplate = copyTemplate( 376 userId, oldTemplate, newClassPK, oldTemplate.getNameMap(), 377 oldTemplate.getDescriptionMap(), serviceContext); 378 379 newTemplates.add(newTemplate); 380 } 381 382 return newTemplates; 383 } 384 385 /** 386 * Deletes the template and its resources. 387 * 388 * @param template the template to be deleted 389 * @throws PortalException if a portal exception occurred 390 * @throws SystemException if a system exception occurred 391 */ 392 @Override 393 @SystemEvent(type = SystemEventConstants.TYPE_DELETE) 394 public void deleteTemplate(DDMTemplate template) 395 throws PortalException, SystemException { 396 397 // Template 398 399 if (!CompanyThreadLocal.isDeleteInProcess() && 400 (template.getClassNameId() == 401 PortalUtil.getClassNameId(DDMStructure.class.getName()))) { 402 403 DDMStructure structure = ddmStructureLocalService.fetchDDMStructure( 404 template.getClassPK()); 405 406 if ((structure != null) && 407 (structure.getClassNameId() == 408 PortalUtil.getClassNameId( 409 JournalArticle.class.getName()))) { 410 411 Group companyGroup = groupLocalService.getCompanyGroup( 412 template.getCompanyId()); 413 414 int count = 0; 415 416 if (template.getGroupId() == companyGroup.getGroupId()) { 417 count = JournalArticleUtil.countByC_T( 418 JournalArticleConstants.CLASSNAME_ID_DEFAULT, 419 template.getTemplateKey()); 420 } 421 else { 422 count = JournalArticleUtil.countByG_C_T( 423 template.getGroupId(), 424 JournalArticleConstants.CLASSNAME_ID_DEFAULT, 425 template.getTemplateKey()); 426 } 427 428 if (count > 0) { 429 throw new RequiredTemplateException( 430 "Template " + template.getName() + " cannot be " + 431 "deleted because it is used by " + count + 432 " journal articles"); 433 } 434 } 435 } 436 437 ddmTemplatePersistence.remove(template); 438 439 // Resources 440 441 resourceLocalService.deleteResource( 442 template.getCompanyId(), DDMTemplate.class.getName(), 443 ResourceConstants.SCOPE_INDIVIDUAL, template.getTemplateId()); 444 } 445 446 /** 447 * Deletes the template and its resources. 448 * 449 * @param templateId the primary key of the template to be deleted 450 * @throws PortalException if a portal exception occurred 451 * @throws SystemException if a system exception occurred 452 */ 453 @Override 454 public void deleteTemplate(long templateId) 455 throws PortalException, SystemException { 456 457 DDMTemplate template = ddmTemplatePersistence.findByPrimaryKey( 458 templateId); 459 460 ddmTemplateLocalService.deleteTemplate(template); 461 } 462 463 /** 464 * Deletes all the templates of the group. 465 * 466 * @param groupId the primary key of the group 467 * @throws PortalException if a portal exception occurred 468 * @throws SystemException if a system exception occurred 469 */ 470 @Override 471 public void deleteTemplates(long groupId) 472 throws PortalException, SystemException { 473 474 List<DDMTemplate> templates = ddmTemplatePersistence.findByGroupId( 475 groupId); 476 477 for (DDMTemplate template : templates) { 478 ddmTemplateLocalService.deleteTemplate(template); 479 } 480 } 481 482 @Override 483 public void deleteTemplates(long groupId, long classNameId) 484 throws PortalException, SystemException { 485 486 List<DDMTemplate> templates = ddmTemplatePersistence.findByG_C( 487 groupId, classNameId); 488 489 for (DDMTemplate template : templates) { 490 ddmTemplateLocalService.deleteTemplate(template); 491 } 492 } 493 494 /** 495 * Returns the template matching the group and template key. 496 * 497 * @param groupId the primary key of the group 498 * @param classNameId the primary key of the class name for the template's 499 * related model 500 * @param templateKey the unique string identifying the template 501 * @return the matching template, or <code>null</code> if a matching 502 * template could not be found 503 * @throws SystemException if a system exception occurred 504 */ 505 @Override 506 public DDMTemplate fetchTemplate( 507 long groupId, long classNameId, String templateKey) 508 throws SystemException { 509 510 templateKey = StringUtil.toUpperCase(templateKey.trim()); 511 512 return ddmTemplatePersistence.fetchByG_C_T( 513 groupId, classNameId, templateKey); 514 } 515 516 /** 517 * Returns the template matching the group and template key, optionally in 518 * the global scope. 519 * 520 * <p> 521 * This method first searches in the given group. If the template is still 522 * not found and <code>includeGlobalTemplates</code> is set to 523 * <code>true</code>, this method searches the global group. 524 * </p> 525 * 526 * @param groupId the primary key of the group 527 * @param classNameId the primary key of the class name for the template's 528 * related model 529 * @param templateKey the unique string identifying the template 530 * @param includeGlobalTemplates whether to include the global scope in the 531 * search 532 * @return the matching template, or <code>null</code> if a matching 533 * template could not be found 534 * @throws PortalException if a portal exception occurred 535 * @throws SystemException if a system exception occurred 536 */ 537 @Override 538 public DDMTemplate fetchTemplate( 539 long groupId, long classNameId, String templateKey, 540 boolean includeGlobalTemplates) 541 throws PortalException, SystemException { 542 543 templateKey = StringUtil.toUpperCase(templateKey.trim()); 544 545 DDMTemplate template = ddmTemplatePersistence.fetchByG_C_T( 546 groupId, classNameId, templateKey); 547 548 if ((template != null) || !includeGlobalTemplates) { 549 return template; 550 } 551 552 Group group = groupPersistence.findByPrimaryKey(groupId); 553 554 Group companyGroup = groupLocalService.getCompanyGroup( 555 group.getCompanyId()); 556 557 return ddmTemplatePersistence.fetchByG_C_T( 558 companyGroup.getGroupId(), classNameId, templateKey); 559 } 560 561 /** 562 * Returns the template with the ID. 563 * 564 * @param templateId the primary key of the template 565 * @return the template with the ID 566 * @throws PortalException if a matching template could not be found 567 * @throws SystemException if a system exception occurred 568 */ 569 @Override 570 public DDMTemplate getTemplate(long templateId) 571 throws PortalException, SystemException { 572 573 return ddmTemplatePersistence.findByPrimaryKey(templateId); 574 } 575 576 /** 577 * Returns the template matching the group and template key. 578 * 579 * @param groupId the primary key of the group 580 * @param classNameId the primary key of the class name for the template's 581 * related model 582 * @param templateKey the unique string identifying the template 583 * @return the matching template 584 * @throws PortalException if a matching template could not be found 585 * @throws SystemException if a system exception occurred 586 */ 587 @Override 588 public DDMTemplate getTemplate( 589 long groupId, long classNameId, String templateKey) 590 throws PortalException, SystemException { 591 592 templateKey = StringUtil.toUpperCase(templateKey.trim()); 593 594 return ddmTemplatePersistence.findByG_C_T( 595 groupId, classNameId, templateKey); 596 } 597 598 /** 599 * Returns the template matching the group and template key, optionally in 600 * the global scope. 601 * 602 * <p> 603 * This method first searches in the group. If the template is still not 604 * found and <code>includeGlobalTemplates</code> is set to 605 * <code>true</code>, this method searches the global group. 606 * </p> 607 * 608 * @param groupId the primary key of the group 609 * @param classNameId the primary key of the class name for the template's 610 * related model 611 * @param templateKey the unique string identifying the template 612 * @param includeGlobalTemplates whether to include the global scope in the 613 * search 614 * @return the matching template 615 * @throws PortalException if a matching template could not be found 616 * @throws SystemException if a system exception occurred 617 */ 618 @Override 619 public DDMTemplate getTemplate( 620 long groupId, long classNameId, String templateKey, 621 boolean includeGlobalTemplates) 622 throws PortalException, SystemException { 623 624 templateKey = StringUtil.toUpperCase(templateKey.trim()); 625 626 DDMTemplate template = ddmTemplatePersistence.fetchByG_C_T( 627 groupId, classNameId, templateKey); 628 629 if (template != null) { 630 return template; 631 } 632 633 if (!includeGlobalTemplates) { 634 throw new NoSuchTemplateException( 635 "No DDMTemplate exists with the template key " + templateKey); 636 } 637 638 Group group = groupPersistence.findByPrimaryKey(groupId); 639 640 Group companyGroup = groupLocalService.getCompanyGroup( 641 group.getCompanyId()); 642 643 return ddmTemplatePersistence.findByG_C_T( 644 companyGroup.getGroupId(), classNameId, templateKey); 645 } 646 647 @Override 648 public DDMTemplate getTemplateBySmallImageId(long smallImageId) 649 throws PortalException, SystemException { 650 651 return ddmTemplatePersistence.findBySmallImageId(smallImageId); 652 } 653 654 /** 655 * Returns all the templates with the class PK. 656 * 657 * @param classPK the primary key of the template's related entity 658 * @return the templates with the class PK 659 * @throws SystemException if a system exception occurred 660 */ 661 @Override 662 public List<DDMTemplate> getTemplates(long classPK) throws SystemException { 663 return ddmTemplatePersistence.findByClassPK(classPK); 664 } 665 666 /** 667 * Returns all the templates matching the group and class name ID. 668 * 669 * @param groupId the primary key of the group 670 * @param classNameId the primary key of the class name for the template's 671 * related model 672 * @return the matching templates 673 * @throws SystemException if a system exception occurred 674 */ 675 @Override 676 public List<DDMTemplate> getTemplates(long groupId, long classNameId) 677 throws SystemException { 678 679 return ddmTemplatePersistence.findByG_C(groupId, classNameId); 680 } 681 682 /** 683 * Returns all the templates matching the group, class name ID, and class 684 * PK. 685 * 686 * @param groupId the primary key of the group 687 * @param classNameId the primary key of the class name for the template's 688 * related model 689 * @param classPK the primary key of the template's related entity 690 * @return the matching templates 691 * @throws SystemException if a system exception occurred 692 */ 693 @Override 694 public List<DDMTemplate> getTemplates( 695 long groupId, long classNameId, long classPK) 696 throws SystemException { 697 698 return ddmTemplatePersistence.findByG_C_C( 699 groupId, classNameId, classPK); 700 } 701 702 /** 703 * Returns all the templates matching the group, class name ID, class PK, 704 * and type. 705 * 706 * @param groupId the primary key of the group 707 * @param classNameId the primary key of the class name for the template's 708 * related model 709 * @param classPK the primary key of the template's related entity 710 * @param type the template's type. For more information, see {@link 711 * com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants}. 712 * @return the matching templates 713 * @throws SystemException if a system exception occurred 714 */ 715 @Override 716 public List<DDMTemplate> getTemplates( 717 long groupId, long classNameId, long classPK, String type) 718 throws SystemException { 719 720 return ddmTemplatePersistence.findByG_C_C_T( 721 groupId, classNameId, classPK, type); 722 } 723 724 /** 725 * Returns all the templates matching the group, class name ID, class PK, 726 * type, and mode. 727 * 728 * @param groupId the primary key of the group 729 * @param classNameId the primary key of the class name for the template's 730 * related model 731 * @param classPK the primary key of the template's related entity 732 * @param type the template's type. For more information, see {@link 733 * com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants}. 734 * @param mode the template's mode. For more information, see {@link 735 * com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants}. 736 * @return the matching templates 737 * @throws SystemException if a system exception occurred 738 */ 739 @Override 740 public List<DDMTemplate> getTemplates( 741 long groupId, long classNameId, long classPK, String type, 742 String mode) 743 throws SystemException { 744 745 return ddmTemplatePersistence.findByG_C_C_T_M( 746 groupId, classNameId, classPK, type, mode); 747 } 748 749 @Override 750 public List<DDMTemplate> getTemplatesByClassPK(long groupId, long classPK) 751 throws SystemException { 752 753 return ddmTemplatePersistence.findByG_CPK(groupId, classPK); 754 } 755 756 @Override 757 public List<DDMTemplate> getTemplatesByClassPK( 758 long groupId, long classPK, int start, int end) 759 throws SystemException { 760 761 return ddmTemplatePersistence.findByG_CPK(groupId, classPK, start, end); 762 } 763 764 @Override 765 public List<DDMTemplate> getTemplatesByClassPK( 766 long[] groupIds, long classPK) 767 throws SystemException { 768 769 return ddmTemplatePersistence.findByG_CPK(groupIds, classPK); 770 } 771 772 /** 773 * Returns the number of templates matching the group and class PK. 774 * 775 * @param groupId the primary key of the group 776 * @param classPK the primary key of the template's related entity 777 * @return the number of templates belonging to the group and class PK 778 * @throws SystemException if a system exception occurred 779 */ 780 @Override 781 public int getTemplatesByClassPKCount(long groupId, long classPK) 782 throws SystemException { 783 784 return ddmTemplatePersistence.countByG_CPK(groupId, classPK); 785 } 786 787 /** 788 * Returns an ordered range of all the templates matching the group and 789 * structure class name ID. 790 * 791 * <p> 792 * Useful when paginating results. Returns a maximum of <code>end - 793 * start</code> instances. <code>start</code> and <code>end</code> are not 794 * primary keys, they are indexes in the result set. Thus, <code>0</code> 795 * refers to the first result in the set. Setting both <code>start</code> 796 * and <code>end</code> to {@link 797 * com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full 798 * result set. 799 * </p> 800 * 801 * @param groupId the primary key of the group 802 * @param structureClassNameId the primary key of the class name for the 803 * template's related structure 804 * @param start the lower bound of the range of templates to return 805 * @param end the upper bound of the range of templates to return (not 806 * inclusive) 807 * @param orderByComparator the comparator to order the templates 808 * (optionally <code>null</code>) 809 * @return the range of matching templates ordered by the comparator 810 * @throws SystemException if a system exception occurred 811 */ 812 @Override 813 public List<DDMTemplate> getTemplatesByStructureClassNameId( 814 long groupId, long structureClassNameId, int start, int end, 815 OrderByComparator orderByComparator) 816 throws SystemException { 817 818 return ddmTemplateFinder.findByG_SC( 819 groupId, structureClassNameId, start, end, orderByComparator); 820 } 821 822 /** 823 * Returns the number of templates matching the group and structure class 824 * name ID, including Generic Templates. 825 * 826 * @param groupId the primary key of the group 827 * @param structureClassNameId the primary key of the class name for the 828 * template's related structure 829 * @return the number of matching templates 830 * @throws SystemException if a system exception occurred 831 */ 832 @Override 833 public int getTemplatesByStructureClassNameIdCount( 834 long groupId, long structureClassNameId) 835 throws SystemException { 836 837 return ddmTemplateFinder.countByG_SC(groupId, structureClassNameId); 838 } 839 840 /** 841 * Returns the number of templates belonging to the group. 842 * 843 * @param groupId the primary key of the group 844 * @return the number of templates belonging to the group 845 * @throws SystemException if a system exception occurred 846 */ 847 @Override 848 public int getTemplatesCount(long groupId) throws SystemException { 849 return ddmTemplatePersistence.countByGroupId(groupId); 850 } 851 852 /** 853 * Returns the number of templates matching the group and class name ID. 854 * 855 * @param groupId the primary key of the group 856 * @param classNameId the primary key of the class name for the template's 857 * related model 858 * @return the number of matching templates 859 * @throws SystemException if a system exception occurred 860 */ 861 @Override 862 public int getTemplatesCount(long groupId, long classNameId) 863 throws SystemException { 864 865 return ddmTemplatePersistence.countByG_C(groupId, classNameId); 866 } 867 868 /** 869 * Returns the number of templates matching the group, class name ID, and 870 * class PK. 871 * 872 * @param groupId the primary key of the group 873 * @param classNameId the primary key of the class name for the template's 874 * related model 875 * @param classPK the primary key of the template's related entity 876 * @return the number of matching templates 877 * @throws SystemException if a system exception occurred 878 */ 879 @Override 880 public int getTemplatesCount(long groupId, long classNameId, long classPK) 881 throws SystemException { 882 883 return ddmTemplatePersistence.countByG_C_C( 884 groupId, classNameId, classPK); 885 } 886 887 /** 888 * Returns an ordered range of all the templates matching the group, class 889 * name ID, class PK, type, and mode, and matching the keywords in the 890 * template names and descriptions. 891 * 892 * <p> 893 * Useful when paginating results. Returns a maximum of <code>end - 894 * start</code> instances. <code>start</code> and <code>end</code> are not 895 * primary keys, they are indexes in the result set. Thus, <code>0</code> 896 * refers to the first result in the set. Setting both <code>start</code> 897 * and <code>end</code> to {@link 898 * com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full 899 * result set. 900 * </p> 901 * 902 * @param companyId the primary key of the template's company 903 * @param groupId the primary key of the group 904 * @param classNameId the primary key of the class name for the template's 905 * related model 906 * @param classPK the primary key of the template's related entity 907 * @param keywords the keywords (space separated), which may occur in the 908 * template's name or description (optionally <code>null</code>) 909 * @param type the template's type (optionally <code>null</code>). For more 910 * information, see {@link 911 * com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants}. 912 * @param mode the template's mode (optionally <code>null</code>). For more 913 * information, see {@link 914 * com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants}. 915 * @param start the lower bound of the range of templates to return 916 * @param end the upper bound of the range of templates to return (not 917 * inclusive) 918 * @param orderByComparator the comparator to order the templates 919 * (optionally <code>null</code>) 920 * @return the range of matching templates ordered by the comparator 921 * @throws SystemException if a system exception occurred 922 */ 923 @Override 924 public List<DDMTemplate> search( 925 long companyId, long groupId, long classNameId, long classPK, 926 String keywords, String type, String mode, int start, int end, 927 OrderByComparator orderByComparator) 928 throws SystemException { 929 930 return ddmTemplateFinder.findByKeywords( 931 companyId, groupId, classNameId, classPK, keywords, type, mode, 932 start, end, orderByComparator); 933 } 934 935 /** 936 * Returns an ordered range of all the templates matching the group, class 937 * name ID, class PK, name keyword, description keyword, type, mode, and 938 * language. 939 * 940 * <p> 941 * Useful when paginating results. Returns a maximum of <code>end - 942 * start</code> instances. <code>start</code> and <code>end</code> are not 943 * primary keys, they are indexes in the result set. Thus, <code>0</code> 944 * refers to the first result in the set. Setting both <code>start</code> 945 * and <code>end</code> to {@link 946 * com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full 947 * result set. 948 * </p> 949 * 950 * @param companyId the primary key of the template's company 951 * @param groupId the primary key of the group 952 * @param classNameId the primary key of the class name for the template's 953 * related model 954 * @param classPK the primary key of the template's related entity 955 * @param name the name keywords (optionally <code>null</code>) 956 * @param description the description keywords (optionally 957 * <code>null</code>) 958 * @param type the template's type (optionally <code>null</code>). For more 959 * information, see {@link 960 * com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants}. 961 * @param mode the template's mode (optionally <code>null</code>). For more 962 * information, see {@link 963 * com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants}. 964 * @param language the template's script language (optionally 965 * <code>null</code>). For more information, see {@link 966 * com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants}. 967 * @param andOperator whether every field must match its keywords, or just 968 * one field 969 * @param start the lower bound of the range of templates to return 970 * @param end the upper bound of the range of templates to return (not 971 * inclusive) 972 * @param orderByComparator the comparator to order the templates 973 * (optionally <code>null</code>) 974 * @return the range of matching templates ordered by the comparator 975 * @throws SystemException if a system exception occurred 976 */ 977 @Override 978 public List<DDMTemplate> search( 979 long companyId, long groupId, long classNameId, long classPK, 980 String name, String description, String type, String mode, 981 String language, boolean andOperator, int start, int end, 982 OrderByComparator orderByComparator) 983 throws SystemException { 984 985 return ddmTemplateFinder.findByC_G_C_C_N_D_T_M_L( 986 companyId, groupId, classNameId, classPK, name, description, type, 987 mode, language, andOperator, start, end, orderByComparator); 988 } 989 990 /** 991 * Returns an ordered range of all the templates matching the group IDs, 992 * class Name IDs, class PK, type, and mode, and include the keywords on its 993 * names and descriptions. 994 * 995 * <p> 996 * Useful when paginating results. Returns a maximum of <code>end - 997 * start</code> instances. <code>start</code> and <code>end</code> are not 998 * primary keys, they are indexes in the result set. Thus, <code>0</code> 999 * refers to the first result in the set. Setting both <code>start</code> 1000 * and <code>end</code> to {@link 1001 * com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full 1002 * result set. 1003 * </p> 1004 * 1005 * @param companyId the primary key of the template's company 1006 * @param groupIds the primary keys of the groups 1007 * @param classNameIds the primary keys of the entity's instances the 1008 * templates are related to 1009 * @param classPKs the primary keys of the template's related entities 1010 * @param keywords the keywords (space separated), which may occur in the 1011 * template's name or description (optionally <code>null</code>) 1012 * @param type the template's type (optionally <code>null</code>). For more 1013 * information, see {@link 1014 * com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants}. 1015 * @param mode the template's mode (optionally <code>null</code>). For more 1016 * information, see {@link 1017 * com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants}. 1018 * @param start the lower bound of the range of templates to return 1019 * @param end the upper bound of the range of templates to return (not 1020 * inclusive) 1021 * @param orderByComparator the comparator to order the templates 1022 * (optionally <code>null</code>) 1023 * @return the range of matching templates ordered by the comparator 1024 * @throws SystemException if a system exception occurred 1025 */ 1026 @Override 1027 public List<DDMTemplate> search( 1028 long companyId, long[] groupIds, long[] classNameIds, 1029 long[] classPKs, String keywords, String type, String mode, 1030 int start, int end, OrderByComparator orderByComparator) 1031 throws SystemException { 1032 1033 return ddmTemplateFinder.findByKeywords( 1034 companyId, groupIds, classNameIds, classPKs, keywords, type, mode, 1035 start, end, orderByComparator); 1036 } 1037 1038 /** 1039 * Returns an ordered range of all the templates matching the group IDs, 1040 * class name IDs, class PK, name keyword, description keyword, type, mode, 1041 * and language. 1042 * 1043 * <p> 1044 * Useful when paginating results. Returns a maximum of <code>end - 1045 * start</code> instances. <code>start</code> and <code>end</code> are not 1046 * primary keys, they are indexes in the result set. Thus, <code>0</code> 1047 * refers to the first result in the set. Setting both <code>start</code> 1048 * and <code>end</code> to {@link 1049 * com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full 1050 * result set. 1051 * </p> 1052 * 1053 * @param companyId the primary key of the template's company 1054 * @param groupIds the primary keys of the groups 1055 * @param classNameIds the primary keys of the entity's instances the 1056 * templates are related to 1057 * @param classPKs the primary keys of the template's related entities 1058 * @param name the name keywords (optionally <code>null</code>) 1059 * @param description the description keywords (optionally 1060 * <code>null</code>) 1061 * @param type the template's type (optionally <code>null</code>). For more 1062 * information, see {@link 1063 * com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants}. 1064 * @param mode the template's mode (optionally <code>null</code>). For more 1065 * information, see {@link 1066 * com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants}. 1067 * @param language the template's script language (optionally 1068 * <code>null</code>). For more information, see {@link 1069 * com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants}. 1070 * @param andOperator whether every field must match its keywords, or just 1071 * one field. 1072 * @param start the lower bound of the range of templates to return 1073 * @param end the upper bound of the range of templates to return (not 1074 * inclusive) 1075 * @param orderByComparator the comparator to order the templates 1076 * (optionally <code>null</code>) 1077 * @return the range of matching templates ordered by the comparator 1078 * @throws SystemException if a system exception occurred 1079 */ 1080 @Override 1081 public List<DDMTemplate> search( 1082 long companyId, long[] groupIds, long[] classNameIds, 1083 long[] classPKs, String name, String description, String type, 1084 String mode, String language, boolean andOperator, int start, 1085 int end, OrderByComparator orderByComparator) 1086 throws SystemException { 1087 1088 return ddmTemplateFinder.findByC_G_C_C_N_D_T_M_L( 1089 companyId, groupIds, classNameIds, classPKs, name, description, 1090 type, mode, language, andOperator, start, end, orderByComparator); 1091 } 1092 1093 /** 1094 * Returns the number of templates matching the group, class name ID, class 1095 * PK, type, and matching the keywords in the template names and 1096 * descriptions. 1097 * 1098 * @param companyId the primary key of the template's company 1099 * @param groupId the primary key of the group 1100 * @param classNameId the primary key of the class name for the template's 1101 * related model 1102 * @param classPK the primary key of the template's related entity 1103 * @param keywords the keywords (space separated), which may occur in the 1104 * template's name or description (optionally <code>null</code>) 1105 * @param type the template's type (optionally <code>null</code>). For more 1106 * information, see {@link 1107 * com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants}. 1108 * @param mode the template's mode (optionally <code>null</code>). For more 1109 * information, see {@link 1110 * com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants}. 1111 * @return the number of matching templates 1112 * @throws SystemException if a system exception occurred 1113 */ 1114 @Override 1115 public int searchCount( 1116 long companyId, long groupId, long classNameId, long classPK, 1117 String keywords, String type, String mode) 1118 throws SystemException { 1119 1120 return ddmTemplateFinder.countByKeywords( 1121 companyId, groupId, classNameId, classPK, keywords, type, mode); 1122 } 1123 1124 /** 1125 * Returns the number of templates matching the group, class name ID, class 1126 * PK, name keyword, description keyword, type, mode, and language. 1127 * 1128 * @param companyId the primary key of the template's company 1129 * @param groupId the primary key of the group 1130 * @param classNameId the primary key of the class name for the template's 1131 * related model 1132 * @param classPK the primary key of the template's related entity 1133 * @param name the name keywords (optionally <code>null</code>) 1134 * @param description the description keywords (optionally 1135 * <code>null</code>) 1136 * @param type the template's type (optionally <code>null</code>). For more 1137 * information, see {@link 1138 * com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants}. 1139 * @param mode the template's mode (optionally <code>null</code>). For more 1140 * information, see {@link 1141 * com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants}. 1142 * @param language the template's script language (optionally 1143 * <code>null</code>). For more information, see {@link 1144 * com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants}. 1145 * @param andOperator whether every field must match its keywords, or just 1146 * one field. 1147 * @return the number of matching templates 1148 * @throws SystemException if a system exception occurred 1149 */ 1150 @Override 1151 public int searchCount( 1152 long companyId, long groupId, long classNameId, long classPK, 1153 String name, String description, String type, String mode, 1154 String language, boolean andOperator) 1155 throws SystemException { 1156 1157 return ddmTemplateFinder.countByC_G_C_C_N_D_T_M_L( 1158 companyId, groupId, classNameId, classPK, name, description, type, 1159 mode, language, andOperator); 1160 } 1161 1162 /** 1163 * Returns the number of templates matching the group IDs, class name IDs, 1164 * class PK, type, and mode, and matching the keywords in the template names 1165 * and descriptions. 1166 * 1167 * @param companyId the primary key of the template's company 1168 * @param groupIds the primary keys of the groups 1169 * @param classNameIds the primary keys of the entity's instance the 1170 * templates are related to 1171 * @param classPKs the primary keys of the template's related entities 1172 * @param keywords the keywords (space separated), which may occur in the 1173 * template's name or description (optionally <code>null</code>) 1174 * @param type the template's type (optionally <code>null</code>). For more 1175 * information, see {@link 1176 * com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants}. 1177 * @param mode the template's mode (optionally <code>null</code>). For more 1178 * information, see {@link 1179 * com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants}. 1180 * @return the number of matching templates 1181 * @throws SystemException if a system exception occurred 1182 */ 1183 @Override 1184 public int searchCount( 1185 long companyId, long[] groupIds, long[] classNameIds, 1186 long[] classPKs, String keywords, String type, String mode) 1187 throws SystemException { 1188 1189 return ddmTemplateFinder.countByKeywords( 1190 companyId, groupIds, classNameIds, classPKs, keywords, type, mode); 1191 } 1192 1193 /** 1194 * Returns the number of templates matching the group IDs, class name IDs, 1195 * class PKs, name keyword, description keyword, type, mode, and language. 1196 * 1197 * @param companyId the primary key of the templates company 1198 * @param groupIds the primary keys of the groups 1199 * @param classNameIds the primary keys of the entity's instance the 1200 * templates are related to 1201 * @param classPKs the primary keys of the template's related entities 1202 * @param name the name keywords (optionally <code>null</code>) 1203 * @param description the description keywords (optionally 1204 * <code>null</code>) 1205 * @param type the template's type (optionally <code>null</code>). For more 1206 * information, see {@link 1207 * com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants}. 1208 * @param mode the template's mode (optionally <code>null</code>). For more 1209 * information, see {@link 1210 * com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants}. 1211 * @param language the template's script language (optionally 1212 * <code>null</code>). For more information, see {@link 1213 * com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants}. 1214 * @param andOperator whether every field must match its keywords, or just 1215 * one field. 1216 * @return the number of matching templates 1217 * @throws SystemException if a system exception occurred 1218 */ 1219 @Override 1220 public int searchCount( 1221 long companyId, long[] groupIds, long[] classNameIds, 1222 long[] classPKs, String name, String description, String type, 1223 String mode, String language, boolean andOperator) 1224 throws SystemException { 1225 1226 return ddmTemplateFinder.countByC_G_C_C_N_D_T_M_L( 1227 companyId, groupIds, classNameIds, classPKs, name, description, 1228 type, mode, language, andOperator); 1229 } 1230 1231 /** 1232 * Updates the template matching the ID. 1233 * 1234 * @param templateId the primary key of the template 1235 * @param classPK the primary key of the template's related entity 1236 * @param nameMap the template's new locales and localized names 1237 * @param descriptionMap the template's new locales and localized 1238 * description 1239 * @param type the template's type. For more information, see {@link 1240 * com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants}. 1241 * @param mode the template's mode. For more information, see {@link 1242 * com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants}. 1243 * @param language the template's script language. For more information, 1244 * see {@link 1245 * com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants}. 1246 * @param script the template's script 1247 * @param cacheable whether the template is cacheable 1248 * @param smallImage whether the template has a small image 1249 * @param smallImageURL the template's small image URL (optionally 1250 * <code>null</code>) 1251 * @param smallImageFile the template's small image file (optionally 1252 * <code>null</code>) 1253 * @param serviceContext the service context to be applied. Can set the 1254 * modification date. 1255 * @return the updated template 1256 * @throws PortalException if a portal exception occurred 1257 * @throws SystemException if a system exception occurred 1258 */ 1259 @Override 1260 public DDMTemplate updateTemplate( 1261 long templateId, long classPK, Map<Locale, String> nameMap, 1262 Map<Locale, String> descriptionMap, String type, String mode, 1263 String language, String script, boolean cacheable, 1264 boolean smallImage, String smallImageURL, File smallImageFile, 1265 ServiceContext serviceContext) 1266 throws PortalException, SystemException { 1267 1268 script = formatScript(type, language, script); 1269 1270 byte[] smallImageBytes = null; 1271 1272 try { 1273 smallImageBytes = FileUtil.getBytes(smallImageFile); 1274 } 1275 catch (IOException ioe) { 1276 } 1277 1278 validate( 1279 nameMap, script, smallImage, smallImageURL, smallImageFile, 1280 smallImageBytes); 1281 1282 DDMTemplate template = ddmTemplateLocalService.getDDMTemplate( 1283 templateId); 1284 1285 template.setModifiedDate(serviceContext.getModifiedDate(null)); 1286 1287 if ((template.getClassPK() == 0) && (classPK > 0)) { 1288 1289 // Allow users to set the structure if and only if it currently does 1290 // not have one. Otherwise, you can have bad data because there may 1291 // be an existing content that has chosen to use a structure and 1292 // template combination that no longer exists. 1293 1294 template.setClassPK(classPK); 1295 } 1296 1297 template.setNameMap(nameMap); 1298 template.setDescriptionMap(descriptionMap); 1299 template.setType(type); 1300 template.setMode(mode); 1301 template.setLanguage(language); 1302 template.setScript(script); 1303 template.setCacheable(cacheable); 1304 template.setSmallImage(smallImage); 1305 template.setSmallImageURL(smallImageURL); 1306 1307 ddmTemplatePersistence.update(template); 1308 1309 // Small image 1310 1311 saveImages( 1312 smallImage, template.getSmallImageId(), smallImageFile, 1313 smallImageBytes); 1314 1315 return template; 1316 } 1317 1318 /** 1319 * Updates the template matching the ID. 1320 * 1321 * @param templateId the primary key of the template 1322 * @param classPK the primary key of the template's related entity 1323 * @param nameMap the template's new locales and localized names 1324 * @param descriptionMap the template's new locales and localized 1325 * description 1326 * @param type the template's type. For more information, see {@link 1327 * com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants}. 1328 * @param mode the template's mode. For more information, see {@link 1329 * com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants}. 1330 * @param language the template's script language. For more information, 1331 * see {@link 1332 * com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants}. 1333 * @param script the template's script 1334 * @param cacheable whether the template is cacheable 1335 * @param serviceContext the service context to be applied. Can set the 1336 * modification date. 1337 * @return the updated template 1338 * @throws PortalException if a portal exception occurred 1339 * @throws SystemException if a system exception occurred 1340 */ 1341 @Override 1342 public DDMTemplate updateTemplate( 1343 long templateId, long classPK, Map<Locale, String> nameMap, 1344 Map<Locale, String> descriptionMap, String type, String mode, 1345 String language, String script, boolean cacheable, 1346 ServiceContext serviceContext) 1347 throws PortalException, SystemException { 1348 1349 DDMTemplate template = ddmTemplateLocalService.getDDMTemplate( 1350 templateId); 1351 1352 File smallImageFile = getSmallImageFile(template); 1353 1354 return updateTemplate( 1355 templateId, classPK, nameMap, descriptionMap, type, mode, language, 1356 script, cacheable, template.isSmallImage(), 1357 template.getSmallImageURL(), smallImageFile, serviceContext); 1358 } 1359 1360 protected DDMTemplate copyTemplate( 1361 long userId, DDMTemplate template, long classPK, 1362 Map<Locale, String> nameMap, Map<Locale, String> descriptionMap, 1363 ServiceContext serviceContext) 1364 throws PortalException, SystemException { 1365 1366 File smallImageFile = getSmallImageFile(template); 1367 1368 return addTemplate( 1369 userId, template.getGroupId(), template.getClassNameId(), classPK, 1370 null, nameMap, descriptionMap, template.getType(), 1371 template.getMode(), template.getLanguage(), template.getScript(), 1372 template.isCacheable(), template.isSmallImage(), 1373 template.getSmallImageURL(), smallImageFile, serviceContext); 1374 } 1375 1376 protected String formatScript(String type, String language, String script) 1377 throws PortalException { 1378 1379 if (type.equals(DDMTemplateConstants.TEMPLATE_TYPE_FORM) || 1380 language.equals(TemplateConstants.LANG_TYPE_XSL)) { 1381 1382 try { 1383 script = DDMXMLUtil.validateXML(script); 1384 script = DDMXMLUtil.formatXML(script); 1385 } 1386 catch (Exception e) { 1387 throw new TemplateScriptException(e); 1388 } 1389 } 1390 1391 return script; 1392 } 1393 1394 protected File getSmallImageFile(DDMTemplate template) 1395 throws SystemException { 1396 1397 File smallImageFile = null; 1398 1399 if (template.isSmallImage() && 1400 Validator.isNull(template.getSmallImageURL())) { 1401 1402 Image smallImage = ImageUtil.fetchByPrimaryKey( 1403 template.getSmallImageId()); 1404 1405 if (smallImage != null) { 1406 smallImageFile = FileUtil.createTempFile(smallImage.getType()); 1407 1408 try { 1409 FileUtil.write(smallImageFile, smallImage.getTextObj()); 1410 } 1411 catch (IOException ioe) { 1412 _log.error(ioe, ioe); 1413 } 1414 } 1415 } 1416 1417 return smallImageFile; 1418 } 1419 1420 protected void saveImages( 1421 boolean smallImage, long smallImageId, File smallImageFile, 1422 byte[] smallImageBytes) 1423 throws PortalException, SystemException { 1424 1425 if (smallImage) { 1426 if ((smallImageFile != null) && (smallImageBytes != null)) { 1427 imageLocalService.updateImage(smallImageId, smallImageBytes); 1428 } 1429 } 1430 else { 1431 imageLocalService.deleteImage(smallImageId); 1432 } 1433 } 1434 1435 protected void validate( 1436 long groupId, long classNameId, String templateKey, 1437 Map<Locale, String> nameMap, String script, boolean smallImage, 1438 String smallImageURL, File smallImageFile, byte[] smallImageBytes) 1439 throws PortalException, SystemException { 1440 1441 templateKey = StringUtil.toUpperCase(templateKey.trim()); 1442 1443 DDMTemplate template = ddmTemplatePersistence.fetchByG_C_T( 1444 groupId, classNameId, templateKey); 1445 1446 if (template != null) { 1447 throw new TemplateDuplicateTemplateKeyException( 1448 "Template already exists with template key " + templateKey); 1449 } 1450 1451 validate( 1452 nameMap, script, smallImage, smallImageURL, smallImageFile, 1453 smallImageBytes); 1454 } 1455 1456 protected void validate(Map<Locale, String> nameMap, String script) 1457 throws PortalException { 1458 1459 validateName(nameMap); 1460 1461 if (Validator.isNull(script)) { 1462 throw new TemplateScriptException("Script is null"); 1463 } 1464 } 1465 1466 protected void validate( 1467 Map<Locale, String> nameMap, String script, boolean smallImage, 1468 String smallImageURL, File smallImageFile, byte[] smallImageBytes) 1469 throws PortalException, SystemException { 1470 1471 validate(nameMap, script); 1472 1473 String[] imageExtensions = PrefsPropsUtil.getStringArray( 1474 PropsKeys.DYNAMIC_DATA_MAPPING_IMAGE_EXTENSIONS, StringPool.COMMA); 1475 1476 if (!smallImage || Validator.isNotNull(smallImageURL) || 1477 (smallImageFile == null) || (smallImageBytes == null)) { 1478 1479 return; 1480 } 1481 1482 String smallImageName = smallImageFile.getName(); 1483 1484 if (smallImageName != null) { 1485 boolean validSmallImageExtension = false; 1486 1487 for (int i = 0; i < imageExtensions.length; i++) { 1488 if (StringPool.STAR.equals(imageExtensions[i]) || 1489 StringUtil.endsWith( 1490 smallImageName, imageExtensions[i])) { 1491 1492 validSmallImageExtension = true; 1493 1494 break; 1495 } 1496 } 1497 1498 if (!validSmallImageExtension) { 1499 throw new TemplateSmallImageNameException(smallImageName); 1500 } 1501 } 1502 1503 long smallImageMaxSize = PrefsPropsUtil.getLong( 1504 PropsKeys.DYNAMIC_DATA_MAPPING_IMAGE_SMALL_MAX_SIZE); 1505 1506 if ((smallImageMaxSize > 0) && 1507 ((smallImageBytes == null) || 1508 (smallImageBytes.length > smallImageMaxSize))) { 1509 1510 throw new TemplateSmallImageSizeException( 1511 "Image " + smallImageName + " has " + smallImageBytes.length + 1512 " bytes and exceeds the maximum size of " + 1513 smallImageMaxSize); 1514 } 1515 } 1516 1517 protected void validateName(Map<Locale, String> nameMap) 1518 throws PortalException { 1519 1520 String name = nameMap.get(LocaleUtil.getSiteDefault()); 1521 1522 if (Validator.isNull(name)) { 1523 throw new TemplateNameException("Name is null"); 1524 } 1525 } 1526 1527 private static Log _log = LogFactoryUtil.getLog( 1528 DDMTemplateLocalServiceImpl.class); 1529 1530 }