public JournalTemplate addTemplate(
      long userId,
      long groupId,
      String templateId,
      boolean autoTemplateId,
      String structureId,
      Map<Locale, String> nameMap,
      Map<Locale, String> descriptionMap,
      String xsl,
      boolean formatXsl,
      String langType,
      boolean cacheable,
      boolean smallImage,
      String smallImageURL,
      File smallImageFile,
      ServiceContext serviceContext)
      throws PortalException, SystemException {

    // Template

    User user = userPersistence.findByPrimaryKey(userId);
    templateId = templateId.trim().toUpperCase();
    Date now = new Date();

    try {
      if (formatXsl) {
        if (langType.equals(JournalTemplateConstants.LANG_TYPE_VM)) {
          xsl = JournalUtil.formatVM(xsl);
        } else {
          xsl = DDMXMLUtil.formatXML(xsl);
        }
      }
    } catch (Exception e) {
      throw new TemplateXslException();
    }

    byte[] smallImageBytes = null;

    try {
      smallImageBytes = FileUtil.getBytes(smallImageFile);
    } catch (IOException ioe) {
    }

    validate(
        groupId,
        templateId,
        autoTemplateId,
        nameMap,
        xsl,
        smallImage,
        smallImageURL,
        smallImageFile,
        smallImageBytes);

    if (autoTemplateId) {
      templateId = String.valueOf(counterLocalService.increment());
    }

    long id = counterLocalService.increment();

    JournalTemplate template = journalTemplatePersistence.create(id);

    template.setUuid(serviceContext.getUuid());
    template.setGroupId(groupId);
    template.setCompanyId(user.getCompanyId());
    template.setUserId(user.getUserId());
    template.setUserName(user.getFullName());
    template.setCreateDate(serviceContext.getCreateDate(now));
    template.setModifiedDate(serviceContext.getModifiedDate(now));
    template.setTemplateId(templateId);
    template.setStructureId(structureId);
    template.setNameMap(nameMap);
    template.setDescriptionMap(descriptionMap);
    template.setXsl(xsl);
    template.setLangType(langType);
    template.setCacheable(cacheable);
    template.setSmallImage(smallImage);
    template.setSmallImageId(counterLocalService.increment());
    template.setSmallImageURL(smallImageURL);

    journalTemplatePersistence.update(template, false);

    // Resources

    if (serviceContext.getAddGroupPermissions() || serviceContext.getAddGuestPermissions()) {

      addTemplateResources(
          template,
          serviceContext.getAddGroupPermissions(),
          serviceContext.getAddGuestPermissions());
    } else {
      addTemplateResources(
          template, serviceContext.getGroupPermissions(), serviceContext.getGuestPermissions());
    }

    // Expando

    ExpandoBridge expandoBridge = template.getExpandoBridge();

    expandoBridge.setAttributes(serviceContext);

    // Small image

    saveImages(smallImage, template.getSmallImageId(), smallImageFile, smallImageBytes);

    return template;
  }
  public JournalTemplate copyTemplate(
      long userId, long groupId, String oldTemplateId, String newTemplateId, boolean autoTemplateId)
      throws PortalException, SystemException {

    // Template

    User user = userPersistence.findByPrimaryKey(userId);
    oldTemplateId = oldTemplateId.trim().toUpperCase();
    newTemplateId = newTemplateId.trim().toUpperCase();
    Date now = new Date();

    JournalTemplate oldTemplate = journalTemplatePersistence.findByG_T(groupId, oldTemplateId);

    if (autoTemplateId) {
      newTemplateId = String.valueOf(counterLocalService.increment());
    } else {
      validate(newTemplateId);

      JournalTemplate newTemplate = journalTemplatePersistence.fetchByG_T(groupId, newTemplateId);

      if (newTemplate != null) {
        throw new DuplicateTemplateIdException();
      }
    }

    long id = counterLocalService.increment();

    JournalTemplate newTemplate = journalTemplatePersistence.create(id);

    newTemplate.setGroupId(groupId);
    newTemplate.setCompanyId(user.getCompanyId());
    newTemplate.setUserId(user.getUserId());
    newTemplate.setUserName(user.getFullName());
    newTemplate.setCreateDate(now);
    newTemplate.setModifiedDate(now);
    newTemplate.setTemplateId(newTemplateId);
    newTemplate.setStructureId(oldTemplate.getStructureId());
    newTemplate.setNameMap(oldTemplate.getNameMap());
    newTemplate.setDescriptionMap(oldTemplate.getDescriptionMap());
    newTemplate.setXsl(oldTemplate.getXsl());
    newTemplate.setLangType(oldTemplate.getLangType());
    newTemplate.setCacheable(oldTemplate.isCacheable());
    newTemplate.setSmallImage(oldTemplate.isSmallImage());
    newTemplate.setSmallImageId(counterLocalService.increment());
    newTemplate.setSmallImageURL(oldTemplate.getSmallImageURL());

    journalTemplatePersistence.update(newTemplate, false);

    // Small image

    if (oldTemplate.getSmallImage()) {
      Image image = imageLocalService.getImage(oldTemplate.getSmallImageId());

      byte[] smallImageBytes = image.getTextObj();

      imageLocalService.updateImage(newTemplate.getSmallImageId(), smallImageBytes);
    }

    // Resources

    addTemplateResources(newTemplate, true, true);

    return newTemplate;
  }