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