Example usage for com.liferay.portal.kernel.util FileUtil getBytes

List of usage examples for com.liferay.portal.kernel.util FileUtil getBytes

Introduction

In this page you can find the example usage for com.liferay.portal.kernel.util FileUtil getBytes.

Prototype

public static byte[] getBytes(InputStream is) throws IOException 

Source Link

Usage

From source file:com.liferay.portlet.documentlibrary.store.BaseStore.java

License:Open Source License

/**
 * Returns the file as a byte array./* www .  j a va 2  s .co  m*/
 *
 * @param  companyId the primary key of the company
 * @param  repositoryId the primary key of the data repository (optionally
 *         {@link CompanyConstants#SYSTEM})
 * @param  fileName the file's name
 * @param  versionLabel the file's version label
 * @return Returns the byte array with the file's name
 * @throws PortalException if the file's information was invalid
 * @throws SystemException if a system exception occurred
 */
public byte[] getFileAsBytes(long companyId, long repositoryId, String fileName, String versionLabel)
        throws PortalException, SystemException {

    byte[] bytes = null;

    try {
        InputStream is = getFileAsStream(companyId, repositoryId, fileName, versionLabel);

        bytes = FileUtil.getBytes(is);
    } catch (IOException ioe) {
        throw new SystemException(ioe);
    }

    return bytes;
}

From source file:com.liferay.portlet.documentlibrary.store.DBStore.java

License:Open Source License

@Override
public void updateFile(long companyId, long repositoryId, String fileName, String versionLabel,
        InputStream inputStream) throws PortalException, SystemException {

    if (DLContentLocalServiceUtil.hasContent(companyId, repositoryId, fileName, versionLabel)) {

        throw new DuplicateFileException(fileName);
    }//from  ww w  . j ava2 s .c  o  m

    long length = -1;

    if (inputStream instanceof ByteArrayInputStream) {
        ByteArrayInputStream byteArrayInputStream = (ByteArrayInputStream) inputStream;

        length = byteArrayInputStream.available();
    } else if (inputStream instanceof FileInputStream) {
        FileInputStream fileInputStream = (FileInputStream) inputStream;

        FileChannel fileChannel = fileInputStream.getChannel();

        try {
            length = fileChannel.size();
        } catch (IOException ioe) {
            if (_log.isWarnEnabled()) {
                _log.warn("Unable to detect file size from file channel", ioe);
            }
        }
    } else if (inputStream instanceof UnsyncByteArrayInputStream) {
        UnsyncByteArrayInputStream unsyncByteArrayInputStream = (UnsyncByteArrayInputStream) inputStream;

        length = unsyncByteArrayInputStream.available();
    }

    if (length >= 0) {
        DLContentLocalServiceUtil.addContent(companyId, repositoryId, fileName, versionLabel, inputStream,
                length);
    } else {
        if (_log.isWarnEnabled()) {
            _log.warn("Unable to detect length from input stream. Reading "
                    + "entire input stream into memory as a last resort.");
        }

        byte[] bytes = null;

        try {
            bytes = FileUtil.getBytes(inputStream);
        } catch (IOException ioe) {
            throw new SystemException(ioe);
        }

        DLContentLocalServiceUtil.addContent(companyId, repositoryId, fileName, versionLabel, bytes);
    }
}

From source file:com.liferay.portlet.documentlibrary.util.ImageProcessorImpl.java

License:Open Source License

private void _generateImages(FileVersion fileVersion) {
    try {//from  w w w. j ava  2s  .co m
        if (!PropsValues.DL_FILE_ENTRY_THUMBNAIL_ENABLED) {
            return;
        }

        InputStream inputStream = fileVersion.getContentStream(false);

        byte[] bytes = FileUtil.getBytes(inputStream);

        ImageBag imageBag = ImageToolUtil.read(bytes);

        RenderedImage renderedImage = imageBag.getRenderedImage();

        if (renderedImage == null) {
            return;
        }

        storeThumbnailImages(fileVersion, renderedImage);
    } catch (NoSuchFileEntryException nsfee) {
    } catch (Exception e) {
        _log.error(e, e);
    } finally {
        _fileVersionIds.remove(fileVersion.getFileVersionId());
    }
}

From source file:com.liferay.portlet.dynamicdatamapping.util.DDMImpl.java

License:Open Source License

protected List<Serializable> getFieldValues(DDMStructure ddmStructure, String fieldName, String fieldNamespace,
        ServiceContext serviceContext) throws PortalException, SystemException {

    String fieldDataType = ddmStructure.getFieldDataType(fieldName);
    String fieldType = ddmStructure.getFieldType(fieldName);

    List<String> fieldNames = getFieldNames(fieldNamespace, fieldName, serviceContext);

    List<Serializable> fieldValues = new ArrayList<Serializable>(fieldNames.size());

    for (String fieldNameValue : fieldNames) {
        Serializable fieldValue = serviceContext.getAttribute(fieldNameValue);
        if (fieldDataType.equals(FieldConstants.DATE)) {
            int fieldValueMonth = GetterUtil.getInteger(serviceContext.getAttribute(fieldNameValue + "Month"));
            int fieldValueDay = GetterUtil.getInteger(serviceContext.getAttribute(fieldNameValue + "Day"));
            int fieldValueYear = GetterUtil.getInteger(serviceContext.getAttribute(fieldNameValue + "Year"));

            Date fieldValueDate = PortalUtil.getDate(fieldValueMonth, fieldValueDay, fieldValueYear);

            if (fieldValueDate != null) {
                fieldValue = String.valueOf(fieldValueDate.getTime());
            }//www .j a v  a  2  s.c om
        } else if (fieldDataType.equals(FieldConstants.IMAGE) && Validator.isNull(fieldValue)) {

            HttpServletRequest request = serviceContext.getRequest();

            if (!(request instanceof UploadRequest)) {
                return null;
            }

            UploadRequest uploadRequest = (UploadRequest) request;

            File file = uploadRequest.getFile(fieldNameValue);

            try {
                byte[] bytes = FileUtil.getBytes(file);

                if (ArrayUtil.isNotEmpty(bytes)) {
                    fieldValue = UnicodeFormatter.bytesToHex(bytes);
                } else {
                    fieldValue = "update";
                }
            } catch (IOException ioe) {
                return null;
            }
        }

        if (fieldValue == null) {
            return null;
        }

        if (DDMImpl.TYPE_RADIO.equals(fieldType) || DDMImpl.TYPE_SELECT.equals(fieldType)) {

            if (fieldValue instanceof String) {
                fieldValue = new String[] { String.valueOf(fieldValue) };
            }

            fieldValue = JSONFactoryUtil.serialize(fieldValue);
        }

        Serializable fieldValueSerializable = FieldConstants.getSerializable(fieldDataType,
                GetterUtil.getString(fieldValue));

        fieldValues.add(fieldValueSerializable);
    }

    return fieldValues;
}

From source file:com.liferay.portlet.journal.action.EditArticleAction.java

License:Open Source License

protected Map<String, byte[]> getImages(UploadPortletRequest uploadRequest) throws Exception {

    Map<String, byte[]> images = new HashMap<String, byte[]>();

    String imagePrefix = "structure_image_";

    Enumeration<String> enu = uploadRequest.getParameterNames();

    while (enu.hasMoreElements()) {
        String name = enu.nextElement();

        if (name.startsWith(imagePrefix)) {
            File file = uploadRequest.getFile(name);
            byte[] bytes = FileUtil.getBytes(file);

            if ((bytes != null) && (bytes.length > 0)) {
                name = name.substring(imagePrefix.length(), name.length());

                images.put(name, bytes);
            }//from  w w w  .j  a  va2s .  co m
        }
    }

    return images;
}

From source file:com.liferay.portlet.journal.action.ViewArticleContentAction.java

License:Open Source License

protected void format(long groupId, String articleId, double version, String previewArticleId, Element root,
        UploadServletRequest uploadServletRequest) throws Exception {

    Iterator<Element> itr = root.elements().iterator();

    while (itr.hasNext()) {
        Element el = itr.next();//from  www  .  j av a2s.co  m

        Element dynamicContent = el.element("dynamic-content");

        String elInstanceId = el.attributeValue("instance-id", StringPool.BLANK);
        String elName = el.attributeValue("name", StringPool.BLANK);
        String elType = el.attributeValue("type", StringPool.BLANK);
        String elContent = StringPool.BLANK;
        String elLanguage = StringPool.BLANK;

        if (dynamicContent != null) {
            elContent = dynamicContent.getTextTrim();

            elLanguage = dynamicContent.attributeValue("language-id", StringPool.BLANK);

            if (!elLanguage.equals(StringPool.BLANK)) {
                elLanguage = "_" + elLanguage;
            }
        }

        if (elType.equals("image") && Validator.isNull(elContent)) {
            File file = uploadServletRequest.getFile("structure_image_" + elName + elLanguage);
            byte[] bytes = FileUtil.getBytes(file);

            if ((bytes != null) && (bytes.length > 0)) {
                long imageId = JournalArticleImageLocalServiceUtil.getArticleImageId(groupId, previewArticleId,
                        version, elInstanceId, elName, elLanguage, true);

                String token = WebServerServletTokenUtil.getToken(imageId);

                dynamicContent.setText("/image/journal/article?img_id=" + imageId + "&t=" + token);

                ImageLocalServiceUtil.updateImage(imageId, bytes);
            } else {
                if (Validator.isNotNull(articleId)) {
                    long imageId = JournalArticleImageLocalServiceUtil.getArticleImageId(groupId, articleId,
                            version, elInstanceId, elName, elLanguage);

                    String token = WebServerServletTokenUtil.getToken(imageId);

                    dynamicContent.setText("/image/journal/article?img_id=" + imageId + "&t=" + token);
                }
            }
        }

        format(groupId, articleId, version, previewArticleId, el, uploadServletRequest);
    }
}

From source file:com.liferay.portlet.journal.service.impl.JournalArticleLocalServiceImpl.java

License:Open Source License

public JournalArticle addArticle(long userId, long groupId, long classNameId, long classPK, String articleId,
        boolean autoArticleId, double version, Map<Locale, String> titleMap, Map<Locale, String> descriptionMap,
        String content, String type, String structureId, String templateId, String layoutUuid,
        int displayDateMonth, int displayDateDay, int displayDateYear, int displayDateHour,
        int displayDateMinute, int expirationDateMonth, int expirationDateDay, int expirationDateYear,
        int expirationDateHour, int expirationDateMinute, boolean neverExpire, int reviewDateMonth,
        int reviewDateDay, int reviewDateYear, int reviewDateHour, int reviewDateMinute, boolean neverReview,
        boolean indexable, boolean smallImage, String smallImageURL, File smallImageFile,
        Map<String, byte[]> images, String articleURL, ServiceContext serviceContext)
        throws PortalException, SystemException {

    // Article/* w  w w.  j  a v  a 2  s. com*/

    User user = userPersistence.findByPrimaryKey(userId);
    articleId = articleId.trim().toUpperCase();

    Date displayDate = PortalUtil.getDate(displayDateMonth, displayDateDay, displayDateYear, displayDateHour,
            displayDateMinute, user.getTimeZone(), new ArticleDisplayDateException());

    Date expirationDate = null;

    if (!neverExpire) {
        expirationDate = PortalUtil.getDate(expirationDateMonth, expirationDateDay, expirationDateYear,
                expirationDateHour, expirationDateMinute, user.getTimeZone(),
                new ArticleExpirationDateException());
    }

    Date reviewDate = null;

    if (!neverReview) {
        reviewDate = PortalUtil.getDate(reviewDateMonth, reviewDateDay, reviewDateYear, reviewDateHour,
                reviewDateMinute, user.getTimeZone(), new ArticleReviewDateException());
    }

    byte[] smallImageBytes = null;

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

    Date now = new Date();

    validate(user.getCompanyId(), groupId, classNameId, articleId, autoArticleId, version, titleMap, content,
            type, structureId, templateId, smallImage, smallImageURL, smallImageFile, smallImageBytes);

    if (autoArticleId) {
        articleId = String.valueOf(counterLocalService.increment());
    }

    long id = counterLocalService.increment();

    long resourcePrimKey = journalArticleResourceLocalService
            .getArticleResourcePrimKey(serviceContext.getUuid(), groupId, articleId);

    JournalArticle article = journalArticlePersistence.create(id);

    Locale locale = LocaleUtil.getDefault();

    String defaultLanguageId = GetterUtil.getString(serviceContext.getAttribute("defaultLanguageId"));

    if (Validator.isNotNull(defaultLanguageId)) {
        locale = LocaleUtil.fromLanguageId(defaultLanguageId);
    }

    String title = titleMap.get(locale);

    content = format(user, groupId, articleId, version, false, content, structureId, images);

    article.setResourcePrimKey(resourcePrimKey);
    article.setGroupId(groupId);
    article.setCompanyId(user.getCompanyId());
    article.setUserId(user.getUserId());
    article.setUserName(user.getFullName());
    article.setCreateDate(serviceContext.getCreateDate(now));
    article.setModifiedDate(serviceContext.getModifiedDate(now));
    article.setClassNameId(classNameId);
    article.setClassPK(classPK);
    article.setArticleId(articleId);
    article.setVersion(version);
    article.setTitleMap(titleMap, locale);
    article.setUrlTitle(getUniqueUrlTitle(id, groupId, articleId, title));
    article.setDescriptionMap(descriptionMap, locale);
    article.setContent(content);
    article.setType(type);
    article.setStructureId(structureId);
    article.setTemplateId(templateId);
    article.setLayoutUuid(layoutUuid);
    article.setDisplayDate(displayDate);
    article.setExpirationDate(expirationDate);
    article.setReviewDate(reviewDate);
    article.setIndexable(indexable);
    article.setSmallImage(smallImage);
    article.setSmallImageId(counterLocalService.increment());
    article.setSmallImageURL(smallImageURL);

    if ((expirationDate == null) || expirationDate.after(now)) {
        article.setStatus(WorkflowConstants.STATUS_DRAFT);
    } else {
        article.setStatus(WorkflowConstants.STATUS_EXPIRED);
    }

    journalArticlePersistence.update(article, false);

    // Resources

    if (serviceContext.isAddGroupPermissions() || serviceContext.isAddGuestPermissions()) {

        addArticleResources(article, serviceContext.isAddGroupPermissions(),
                serviceContext.isAddGuestPermissions());
    } else {
        addArticleResources(article, serviceContext.getGroupPermissions(),
                serviceContext.getGuestPermissions());
    }

    // Expando

    ExpandoBridge expandoBridge = article.getExpandoBridge();

    expandoBridge.setAttributes(serviceContext);

    // Small image

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

    // Asset

    updateAsset(userId, article, serviceContext.getAssetCategoryIds(), serviceContext.getAssetTagNames(),
            serviceContext.getAssetLinkEntryIds());

    // Message boards

    if (PropsValues.JOURNAL_ARTICLE_COMMENTS_ENABLED) {
        mbMessageLocalService.addDiscussionMessage(userId, article.getUserName(), groupId,
                JournalArticle.class.getName(), resourcePrimKey, WorkflowConstants.ACTION_PUBLISH);
    }

    // Email

    PortletPreferences preferences = ServiceContextUtil.getPortletPreferences(serviceContext);

    sendEmail(article, articleURL, preferences, "requested", serviceContext);

    // Workflow

    if (classNameId == 0) {
        WorkflowHandlerRegistryUtil.startWorkflowInstance(user.getCompanyId(), groupId, userId,
                JournalArticle.class.getName(), article.getId(), article, serviceContext);

        if (serviceContext.getWorkflowAction() != WorkflowConstants.ACTION_PUBLISH) {

            // Indexer

            Indexer indexer = IndexerRegistryUtil.getIndexer(JournalArticle.class);

            indexer.reindex(article);
        }
    } else {
        updateStatus(userId, article, WorkflowConstants.STATUS_APPROVED, null, serviceContext);
    }

    return article;
}

From source file:com.liferay.portlet.journal.service.impl.JournalArticleLocalServiceImpl.java

License:Open Source License

public JournalArticle updateArticle(long userId, long groupId, String articleId, double version,
        Map<Locale, String> titleMap, Map<Locale, String> descriptionMap, String content, String type,
        String structureId, String templateId, String layoutUuid, int displayDateMonth, int displayDateDay,
        int displayDateYear, int displayDateHour, int displayDateMinute, int expirationDateMonth,
        int expirationDateDay, int expirationDateYear, int expirationDateHour, int expirationDateMinute,
        boolean neverExpire, int reviewDateMonth, int reviewDateDay, int reviewDateYear, int reviewDateHour,
        int reviewDateMinute, boolean neverReview, boolean indexable, boolean smallImage, String smallImageURL,
        File smallImageFile, Map<String, byte[]> images, String articleURL, ServiceContext serviceContext)
        throws PortalException, SystemException {

    // Article//from ww  w .  j  a v a2s .  c o  m

    User user = userPersistence.findByPrimaryKey(userId);
    articleId = articleId.trim().toUpperCase();

    Date displayDate = PortalUtil.getDate(displayDateMonth, displayDateDay, displayDateYear, displayDateHour,
            displayDateMinute, user.getTimeZone(), new ArticleDisplayDateException());

    Date expirationDate = null;

    if (!neverExpire) {
        expirationDate = PortalUtil.getDate(expirationDateMonth, expirationDateDay, expirationDateYear,
                expirationDateHour, expirationDateMinute, user.getTimeZone(),
                new ArticleExpirationDateException());
    }

    Date now = new Date();

    boolean expired = false;

    if ((expirationDate != null) && expirationDate.before(now)) {
        expired = true;
    }

    Date reviewDate = null;

    if (!neverReview) {
        reviewDate = PortalUtil.getDate(reviewDateMonth, reviewDateDay, reviewDateYear, reviewDateHour,
                reviewDateMinute, user.getTimeZone(), new ArticleReviewDateException());
    }

    byte[] smallImageBytes = null;

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

    JournalArticle oldArticle = null;
    double oldVersion = 0;

    boolean incrementVersion = false;

    boolean imported = GetterUtil.getBoolean(serviceContext.getAttribute("imported"));

    if (imported) {
        oldArticle = getArticle(groupId, articleId, version);
        oldVersion = version;

        if (!expired) {
            incrementVersion = true;
        } else {
            return expireArticle(userId, groupId, articleId, version, articleURL, serviceContext);
        }
    } else {
        oldArticle = getLatestArticle(groupId, articleId, WorkflowConstants.STATUS_ANY);

        oldVersion = oldArticle.getVersion();

        if ((version > 0) && (version != oldVersion)) {
            throw new ArticleVersionException();
        }

        if (oldArticle.isApproved() || oldArticle.isExpired()) {
            incrementVersion = true;
        }
    }

    validate(user.getCompanyId(), groupId, oldArticle.getClassNameId(), titleMap, content, type, structureId,
            templateId, smallImage, smallImageURL, smallImageFile, smallImageBytes);

    JournalArticle article = null;

    if (incrementVersion) {
        double newVersion = MathUtil.format(oldVersion + 0.1, 1, 1);

        long id = counterLocalService.increment();

        article = journalArticlePersistence.create(id);

        article.setResourcePrimKey(oldArticle.getResourcePrimKey());
        article.setGroupId(oldArticle.getGroupId());
        article.setCompanyId(oldArticle.getCompanyId());
        article.setUserId(user.getUserId());
        article.setUserName(user.getFullName());
        article.setCreateDate(serviceContext.getModifiedDate(now));
        article.setClassNameId(oldArticle.getClassNameId());
        article.setClassPK(oldArticle.getClassPK());
        article.setArticleId(articleId);
        article.setVersion(newVersion);
        article.setSmallImageId(oldArticle.getSmallImageId());
    } else {
        article = oldArticle;
    }

    Locale locale = LocaleUtil.getDefault();

    String defaultLanguageId = GetterUtil.getString(serviceContext.getAttribute("defaultLanguageId"));

    if (Validator.isNotNull(defaultLanguageId)) {
        locale = LocaleUtil.fromLanguageId(defaultLanguageId);
    }

    String title = titleMap.get(locale);

    content = format(user, groupId, articleId, article.getVersion(), incrementVersion, content, structureId,
            images);

    article.setModifiedDate(serviceContext.getModifiedDate(now));
    article.setTitleMap(titleMap, locale);
    article.setUrlTitle(getUniqueUrlTitle(article.getId(), groupId, articleId, title));
    article.setDescriptionMap(descriptionMap, locale);
    article.setContent(content);
    article.setType(type);
    article.setStructureId(structureId);
    article.setTemplateId(templateId);
    article.setLayoutUuid(layoutUuid);
    article.setDisplayDate(displayDate);
    article.setExpirationDate(expirationDate);
    article.setReviewDate(reviewDate);
    article.setIndexable(indexable);
    article.setSmallImage(smallImage);

    if (article.getSmallImageId() == 0) {
        article.setSmallImageId(counterLocalService.increment());
    }

    article.setSmallImageURL(smallImageURL);

    if (oldArticle.isPending()) {
        article.setStatus(oldArticle.getStatus());
    } else if (!expired) {
        article.setStatus(WorkflowConstants.STATUS_DRAFT);
    } else {
        article.setStatus(WorkflowConstants.STATUS_EXPIRED);
    }

    journalArticlePersistence.update(article, false);

    // Asset

    updateAsset(userId, article, serviceContext.getAssetCategoryIds(), serviceContext.getAssetTagNames(),
            serviceContext.getAssetLinkEntryIds());

    // Expando

    ExpandoBridge expandoBridge = article.getExpandoBridge();

    expandoBridge.setAttributes(serviceContext);

    // Small image

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

    // Email

    PortletPreferences preferences = ServiceContextUtil.getPortletPreferences(serviceContext);

    // Workflow

    if (serviceContext.getWorkflowAction() == WorkflowConstants.ACTION_PUBLISH) {

        sendEmail(article, articleURL, preferences, "requested", serviceContext);

        WorkflowHandlerRegistryUtil.startWorkflowInstance(user.getCompanyId(), groupId, userId,
                JournalArticle.class.getName(), article.getId(), article, serviceContext);
    } else if (article.getVersion() == JournalArticleConstants.VERSION_DEFAULT) {

        // Indexer

        Indexer indexer = IndexerRegistryUtil.getIndexer(JournalArticle.class);

        indexer.reindex(article);
    }

    return article;
}

From source file:com.liferay.portlet.journal.service.impl.JournalTemplateLocalServiceImpl.java

License:Open Source License

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/*from  w  w w.  java  2s. co m*/

    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.isAddGroupPermissions() || serviceContext.isAddGuestPermissions()) {

        addTemplateResources(template, serviceContext.isAddGroupPermissions(),
                serviceContext.isAddGuestPermissions());
    } 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;
}

From source file:com.liferay.portlet.journal.service.impl.JournalTemplateLocalServiceImpl.java

License:Open Source License

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//  w  w  w .  j  a v a2  s.c o  m

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