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 updateTemplate(
      long groupId,
      String templateId,
      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

    templateId = templateId.trim().toUpperCase();

    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(nameMap, xsl, smallImage, smallImageURL, smallImageFile, smallImageBytes);

    JournalTemplate template = journalTemplatePersistence.findByG_T(groupId, templateId);

    template.setModifiedDate(new Date());

    if (Validator.isNull(template.getStructureId()) && Validator.isNotNull(structureId)) {

      // Allow users to set the structure if and only if it currently
      // does not have one. Otherwise, you can have bad data because there
      // may be an existing article that has chosen to use a structure and
      // template combination that no longer exists.

      template.setStructureId(structureId);
    }

    template.setNameMap(nameMap);
    template.setDescriptionMap(descriptionMap);
    template.setXsl(xsl);
    template.setLangType(langType);
    template.setCacheable(cacheable);
    template.setSmallImage(smallImage);
    template.setSmallImageURL(smallImageURL);
    template.setModifiedDate(serviceContext.getModifiedDate(null));

    journalTemplatePersistence.update(template, false);

    // Expando

    ExpandoBridge expandoBridge = template.getExpandoBridge();

    expandoBridge.setAttributes(serviceContext);

    // Small image

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

    return template;
  }