protected void doAddJournalTemplates(
      String journalStructureId, String name, InputStream inputStream) throws Exception {

    name = getName(name);

    Map<Locale, String> nameMap = getNameMap(name);

    String xsl = StringUtil.read(inputStream);

    JournalTemplate journalTemplate =
        JournalTemplateLocalServiceUtil.addTemplate(
            userId,
            groupId,
            StringPool.BLANK,
            true,
            journalStructureId,
            nameMap,
            null,
            xsl,
            true,
            JournalTemplateConstants.LANG_TYPE_VM,
            false,
            false,
            StringPool.BLANK,
            null,
            serviceContext);

    addJournalArticles(
        journalStructureId, journalTemplate.getTemplateId(), "/journal/articles/" + name);
  }
  public void addTemplateResources(
      JournalTemplate template, String[] groupPermissions, String[] guestPermissions)
      throws PortalException, SystemException {

    resourceLocalService.addModelResources(
        template.getCompanyId(),
        template.getGroupId(),
        template.getUserId(),
        JournalTemplate.class.getName(),
        template.getId(),
        groupPermissions,
        guestPermissions);
  }
  public void addTemplateResources(
      JournalTemplate template, boolean addGroupPermissions, boolean addGuestPermissions)
      throws PortalException, SystemException {

    resourceLocalService.addResources(
        template.getCompanyId(),
        template.getGroupId(),
        template.getUserId(),
        JournalTemplate.class.getName(),
        template.getId(),
        false,
        addGroupPermissions,
        addGuestPermissions);
  }
  public void checkNewLine(long groupId, String templateId)
      throws PortalException, SystemException {

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

    String xsl = template.getXsl();

    if ((xsl != null) && (xsl.indexOf("\\n") != -1)) {
      xsl = StringUtil.replace(xsl, new String[] {"\\n", "\\r"}, new String[] {"\n", "\r"});

      template.setXsl(xsl);

      journalTemplatePersistence.update(template, false);
    }
  }
Example #5
0
  public JournalTemplateResourceImpl(JournalTemplate template, String parentPath, String name) {

    super(
        parentPath,
        name,
        template.getTemplateId(),
        template.getCreateDate(),
        template.getModifiedDate(),
        template.getXsl().length());

    setModel(template);
    setClassName(JournalTemplate.class.getName());
    setPrimaryKey(template.getPrimaryKey());

    _template = template;
  }
  public void deleteTemplate(JournalTemplate template) throws PortalException, SystemException {

    if (journalArticlePersistence.countByG_C_T(template.getGroupId(), 0, template.getTemplateId())
        > 0) {

      throw new RequiredTemplateException();
    }

    // WebDAVProps

    webDAVPropsLocalService.deleteWebDAVProps(JournalTemplate.class.getName(), template.getId());

    // Small image

    imageLocalService.deleteImage(template.getSmallImageId());

    // Expando

    expandoValueLocalService.deleteValues(JournalTemplate.class.getName(), template.getId());

    // Resources

    resourceLocalService.deleteResource(
        template.getCompanyId(),
        JournalTemplate.class.getName(),
        ResourceConstants.SCOPE_INDIVIDUAL,
        template.getId());

    // Article

    journalArticleLocalService.updateTemplateId(
        template.getGroupId(),
        PortalUtil.getClassNameId(JournalStructure.class.getName()),
        template.getTemplateId(),
        StringPool.BLANK);

    // Template

    journalTemplatePersistence.remove(template);
  }
  /**
   * Updates the journal template in the database or adds it if it does not yet exist. Also notifies
   * the appropriate model listeners.
   *
   * @param journalTemplate the journal template
   * @param merge whether to merge the journal template with the current session. See {@link
   *     com.liferay.portal.service.persistence.BatchSession#update(com.liferay.portal.kernel.dao.orm.Session,
   *     com.liferay.portal.model.BaseModel, boolean)} for an explanation.
   * @return the journal template that was updated
   * @throws SystemException if a system exception occurred
   */
  public JournalTemplate updateJournalTemplate(JournalTemplate journalTemplate, boolean merge)
      throws SystemException {
    journalTemplate.setNew(false);

    journalTemplate = journalTemplatePersistence.update(journalTemplate, merge);

    Indexer indexer = IndexerRegistryUtil.getIndexer(getModelClassName());

    if (indexer != null) {
      try {
        indexer.reindex(journalTemplate);
      } catch (SearchException se) {
        if (_log.isWarnEnabled()) {
          _log.warn(se, se);
        }
      }
    }

    return journalTemplate;
  }
  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;
  }
  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;
  }