List of usage examples for com.liferay.portal.kernel.util MathUtil format
public static double format(double x, int max, int min)
From source file:com.liferay.journal.service.impl.JournalArticleLocalServiceImpl.java
License:Open Source License
protected double getNextVersion(JournalArticle article) throws PortalException { double nextVersion = article.getVersion(); // The next version must be greater than the version of the latest live // article//from w w w. j a va2 s .co m JournalArticle latestLiveArticle = fetchLatestLiveArticle(article); if ((latestLiveArticle != null) && (latestLiveArticle.getVersion() > nextVersion)) { nextVersion = latestLiveArticle.getVersion(); } return MathUtil.format(nextVersion + 0.1, 1, 1); }
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 va 2 s . c om 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.JournalArticleLocalServiceImpl.java
License:Open Source License
public JournalArticle updateArticleTranslation(long groupId, String articleId, double version, Locale locale, String title, String description, String content, Map<String, byte[]> images) throws PortalException, SystemException { validateContent(content);/*from w w w. j a v a2s . co m*/ JournalArticle oldArticle = getLatestArticle(groupId, articleId, WorkflowConstants.STATUS_ANY); double oldVersion = oldArticle.getVersion(); if ((version > 0) && (version != oldVersion)) { throw new ArticleVersionException(); } JournalArticle article = null; User user = userService.getUserById(oldArticle.getUserId()); if (!oldArticle.isDraft()) { 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(oldArticle.getUserId()); article.setUserName(user.getFullName()); article.setCreateDate(new Date()); article.setModifiedDate(new Date()); article.setClassNameId(oldArticle.getClassNameId()); article.setClassPK(oldArticle.getClassPK()); article.setArticleId(articleId); article.setVersion(newVersion); article.setTitleMap(oldArticle.getTitleMap()); article.setUrlTitle(getUniqueUrlTitle(article.getId(), groupId, articleId, title)); article.setDescriptionMap(oldArticle.getDescriptionMap()); article.setType(oldArticle.getType()); article.setStructureId(oldArticle.getStructureId()); article.setTemplateId(oldArticle.getTemplateId()); article.setLayoutUuid(oldArticle.getLayoutUuid()); article.setDisplayDate(oldArticle.getDisplayDate()); article.setExpirationDate(oldArticle.getExpirationDate()); article.setReviewDate(oldArticle.getReviewDate()); article.setIndexable(oldArticle.getIndexable()); article.setSmallImage(oldArticle.getSmallImage()); article.setSmallImageId(oldArticle.getSmallImageId()); if (article.getSmallImageId() == 0) { article.setSmallImageId(counterLocalService.increment()); } article.setSmallImageURL(oldArticle.getSmallImageURL()); article.setStatus(WorkflowConstants.STATUS_DRAFT); article.setStatusDate(new Date()); } else { article = oldArticle; } Map<Locale, String> titleMap = article.getTitleMap(); titleMap.put(locale, title); article.setTitleMap(titleMap); Map<Locale, String> descriptionMap = article.getDescriptionMap(); descriptionMap.put(locale, description); article.setDescriptionMap(descriptionMap); content = format(user, groupId, articleId, version, !oldArticle.isDraft(), content, oldArticle.getStructureId(), images); article.setContent(content); journalArticlePersistence.update(article, false); return article; }
From source file:com.liferay.portlet.journal.service.impl.JournalArticleLocalServiceImpl.java
License:Open Source License
protected void formatImage(long groupId, String articleId, double version, boolean incrementVersion, Element el, String elInstanceId, String elName, Map<String, byte[]> images) throws PortalException, SystemException { List<Element> imageContents = el.elements("dynamic-content"); for (Element dynamicContent : imageContents) { String elLanguage = dynamicContent.attributeValue("language-id", StringPool.BLANK); if (!elLanguage.equals(StringPool.BLANK)) { elLanguage = "_" + elLanguage; }/*from www. ja v a 2 s . c o m*/ long imageId = journalArticleImageLocalService.getArticleImageId(groupId, articleId, version, elInstanceId, elName, elLanguage); double oldVersion = MathUtil.format(version - 0.1, 1, 1); long oldImageId = 0; if ((oldVersion >= 1) && incrementVersion) { oldImageId = journalArticleImageLocalService.getArticleImageId(groupId, articleId, oldVersion, elInstanceId, elName, elLanguage); } String elContent = "/image/journal/article?img_id=" + imageId + "&t=" + WebServerServletTokenUtil.getToken(imageId); if (dynamicContent.getText().equals("delete")) { dynamicContent.setText(StringPool.BLANK); imageLocalService.deleteImage(imageId); String defaultElLanguage = ""; if (!Validator.isNotNull(elLanguage)) { defaultElLanguage = "_" + LocaleUtil.toLanguageId(LocaleUtil.getDefault()); } long defaultImageId = journalArticleImageLocalService.getArticleImageId(groupId, articleId, version, elInstanceId, elName, defaultElLanguage); imageLocalService.deleteImage(defaultImageId); continue; } byte[] bytes = images.get(elInstanceId + "_" + elName + elLanguage); if ((bytes != null) && (bytes.length > 0)) { dynamicContent.setText(elContent); dynamicContent.addAttribute("id", String.valueOf(imageId)); imageLocalService.updateImage(imageId, bytes); continue; } if ((version > JournalArticleConstants.VERSION_DEFAULT) && (incrementVersion)) { Image oldImage = null; if (oldImageId > 0) { oldImage = imageLocalService.getImage(oldImageId); } if (oldImage != null) { dynamicContent.setText(elContent); dynamicContent.addAttribute("id", String.valueOf(imageId)); bytes = oldImage.getTextObj(); imageLocalService.updateImage(imageId, bytes); } continue; } Image image = imageLocalService.getImage(imageId); if (image != null) { dynamicContent.setText(elContent); dynamicContent.addAttribute("id", String.valueOf(imageId)); continue; } long contentImageId = GetterUtil.getLong(HttpUtil.getParameter(dynamicContent.getText(), "img_id")); if (contentImageId <= 0) { contentImageId = GetterUtil .getLong(HttpUtil.getParameter(dynamicContent.getText(), "img_id", false)); } if (contentImageId > 0) { image = imageLocalService.getImage(contentImageId); if (image != null) { dynamicContent.addAttribute("id", String.valueOf(contentImageId)); continue; } } String defaultElLanguage = ""; if (!Validator.isNotNull(elLanguage)) { defaultElLanguage = "_" + LocaleUtil.toLanguageId(LocaleUtil.getDefault()); } long defaultImageId = journalArticleImageLocalService.getArticleImageId(groupId, articleId, version, elInstanceId, elName, defaultElLanguage); Image defaultImage = imageLocalService.getImage(defaultImageId); if (defaultImage != null) { dynamicContent.setText(elContent); dynamicContent.addAttribute("id", String.valueOf(defaultImageId)); bytes = defaultImage.getTextObj(); imageLocalService.updateImage(defaultImageId, bytes); continue; } if (Validator.isNotNull(elLanguage)) { dynamicContent.setText(StringPool.BLANK); } } }
From source file:com.liferay.portlet.ratings.action.RateEntryAction.java
License:Open Source License
@Override public String getJSON(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { String className = getClassName(request); long classPK = getClassPK(request); double score = ParamUtil.getDouble(request, "score"); if (score == 0) { RatingsEntryServiceUtil.deleteEntry(className, classPK); } else {/*ww w . java2s . co m*/ RatingsEntryServiceUtil.updateEntry(className, classPK, score); } RatingsStats stats = RatingsStatsLocalServiceUtil.getStats(className, classPK); double averageScore = MathUtil.format(stats.getAverageScore(), 1, 1); JSONObject jsonObj = JSONFactoryUtil.createJSONObject(); jsonObj.put("totalEntries", stats.getTotalEntries()); jsonObj.put("totalScore", stats.getTotalScore()); jsonObj.put("averageScore", averageScore); return jsonObj.toString(); }
From source file:com.liferay.portlet.wiki.service.impl.WikiPageLocalServiceImpl.java
License:Open Source License
public WikiPage updatePage(long userId, long nodeId, String title, double version, String content, String summary, boolean minorEdit, String format, String parentTitle, String redirectTitle, ServiceContext serviceContext) throws PortalException, SystemException { // Page// w w w .j ava2 s. co m User user = userPersistence.findByPrimaryKey(userId); Date now = new Date(); WikiPage oldPage = null; try { oldPage = wikiPagePersistence.findByN_T_First(nodeId, title, null); } catch (NoSuchPageException nspe) { return addPage(userId, nodeId, title, WikiPageConstants.VERSION_DEFAULT, content, summary, minorEdit, format, true, parentTitle, redirectTitle, serviceContext); } long pageId = 0; if (oldPage.isApproved()) { pageId = counterLocalService.increment(); } else { pageId = oldPage.getPageId(); } content = SanitizerUtil.sanitize(user.getCompanyId(), oldPage.getGroupId(), userId, WikiPage.class.getName(), pageId, "text/" + format, content); validate(nodeId, content, format); double oldVersion = oldPage.getVersion(); if ((version > 0) && (version != oldVersion)) { throw new PageVersionException(); } long resourcePrimKey = wikiPageResourceLocalService.getPageResourcePrimKey(nodeId, title); long groupId = oldPage.getGroupId(); WikiPage page = oldPage; double newVersion = oldVersion; if (oldPage.isApproved()) { newVersion = MathUtil.format(oldVersion + 0.1, 1, 1); page = wikiPagePersistence.create(pageId); } page.setResourcePrimKey(resourcePrimKey); page.setGroupId(groupId); page.setCompanyId(user.getCompanyId()); page.setUserId(user.getUserId()); page.setUserName(user.getFullName()); page.setCreateDate(serviceContext.getModifiedDate(now)); page.setModifiedDate(serviceContext.getModifiedDate(now)); page.setNodeId(nodeId); page.setTitle(title); page.setVersion(newVersion); page.setMinorEdit(minorEdit); page.setContent(content); if (oldPage.isPending()) { page.setStatus(oldPage.getStatus()); } else { page.setStatus(WorkflowConstants.STATUS_DRAFT); } page.setSummary(summary); page.setFormat(format); if (Validator.isNotNull(parentTitle)) { page.setParentTitle(parentTitle); } if (Validator.isNotNull(redirectTitle)) { page.setRedirectTitle(redirectTitle); } wikiPagePersistence.update(page, false); // Expando ExpandoBridge expandoBridge = page.getExpandoBridge(); expandoBridge.setAttributes(serviceContext); // Node WikiNode node = wikiNodePersistence.findByPrimaryKey(nodeId); node.setLastPostDate(serviceContext.getModifiedDate(now)); wikiNodePersistence.update(node, false); // Asset updateAsset(userId, page, serviceContext.getAssetCategoryIds(), serviceContext.getAssetTagNames(), serviceContext.getAssetLinkEntryIds()); // Workflow WorkflowHandlerRegistryUtil.startWorkflowInstance(user.getCompanyId(), page.getGroupId(), userId, WikiPage.class.getName(), page.getPageId(), page, serviceContext); return page; }
From source file:com.liferay.portlet.wiki.service.impl.WikiPageLocalServiceImpl.java
License:Open Source License
protected WikiPage getPreviousVersionPage(WikiPage page) throws PortalException, SystemException { double previousVersion = MathUtil.format(page.getVersion() - 0.1, 1, 1); if (previousVersion < 1) { return null; }/*ww w.ja v a 2s . co m*/ return getPage(page.getNodeId(), page.getTitle(), previousVersion); }
From source file:com.liferay.so.activities.hook.social.WikiActivityInterpreter.java
License:Open Source License
@Override public void updateActivitySet(long activityId) throws PortalException { SocialActivity activity = SocialActivityLocalServiceUtil.fetchSocialActivity(activityId); if ((activity == null) || (activity.getActivitySetId() > 0)) { return;//from ww w .j av a2 s .co m } long activitySetId = getActivitySetId(activityId); if (activitySetId > 0) { SocialActivitySetLocalServiceUtil.incrementActivityCount(activitySetId, activityId); if (activity.getType() == SocialActivityKeyConstants.WIKI_UPDATE_PAGE) { SocialActivitySet activitySet = SocialActivitySetLocalServiceUtil .fetchSocialActivitySet(activitySetId); WikiPageResource pageResource = WikiPageResourceLocalServiceUtil .fetchWikiPageResource(activity.getClassPK()); WikiPage wikiPage = WikiPageLocalServiceUtil.getPage(pageResource.getResourcePrimKey()); JSONObject extraDataJSONObject = null; if (Validator.isNull(activitySet.getExtraData())) { extraDataJSONObject = JSONFactoryUtil.createJSONObject(); extraDataJSONObject.put("sourceVersion", MathUtil.format(wikiPage.getVersion() - 0.1, 1, 1)); } else { extraDataJSONObject = JSONFactoryUtil.createJSONObject(activitySet.getExtraData()); } extraDataJSONObject.put("targetVersion", wikiPage.getVersion()); activitySet.setExtraData(extraDataJSONObject.toString()); SocialActivitySetLocalServiceUtil.updateSocialActivitySet(activitySet); } } else { SocialActivitySet activitySet = SocialActivitySetLocalServiceUtil.addActivitySet(activityId); if (activity.getType() == SocialActivityKeyConstants.WIKI_UPDATE_PAGE) { WikiPageResource pageResource = WikiPageResourceLocalServiceUtil .fetchWikiPageResource(activity.getClassPK()); WikiPage wikiPage = WikiPageLocalServiceUtil.getPage(pageResource.getResourcePrimKey()); JSONObject extraDataJSONObject = JSONFactoryUtil.createJSONObject(); extraDataJSONObject.put("sourceVersion", MathUtil.format(wikiPage.getVersion() - 0.1, 1, 1)); extraDataJSONObject.put("targetVersion", wikiPage.getVersion()); activitySet.setExtraData(extraDataJSONObject.toString()); SocialActivitySetLocalServiceUtil.updateSocialActivitySet(activitySet); } } }
From source file:com.liferay.wiki.service.impl.WikiPageLocalServiceImpl.java
License:Open Source License
@Override public WikiPage getPreviousVersionPage(WikiPage page) throws PortalException { double previousVersion = MathUtil.format(page.getVersion() - 0.1, 1, 1); if (previousVersion < 1) { return null; }/*from ww w .j a v a 2 s . c o m*/ return getPage(page.getNodeId(), page.getTitle(), previousVersion); }
From source file:com.liferay.wiki.service.impl.WikiPageLocalServiceImpl.java
License:Open Source License
protected WikiPage updatePage(long userId, WikiPage oldPage, long newNodeId, String newTitle, String content, String summary, boolean minorEdit, String format, String parentTitle, String redirectTitle, ServiceContext serviceContext) throws PortalException { User user = userPersistence.findByPrimaryKey(userId); long pageId = 0; if (oldPage.isApproved()) { pageId = counterLocalService.increment(); } else {/* w ww. j ava 2 s . com*/ pageId = oldPage.getPageId(); } content = SanitizerUtil.sanitize(user.getCompanyId(), oldPage.getGroupId(), userId, WikiPage.class.getName(), pageId, "text/" + format, content); long nodeId = oldPage.getNodeId(); if (newNodeId != 0) { nodeId = newNodeId; } if (!format.equals(oldPage.getFormat())) { validate(nodeId, content, format); } serviceContext.validateModifiedDate(oldPage, PageVersionException.class); long resourcePrimKey = wikiPageResourceLocalService.getPageResourcePrimKey(oldPage.getGroupId(), oldPage.getNodeId(), oldPage.getTitle()); Date now = new Date(); WikiPage page = oldPage; double newVersion = oldPage.getVersion(); if (oldPage.isApproved()) { newVersion = MathUtil.format(oldPage.getVersion() + 0.1, 1, 1); page = wikiPagePersistence.create(pageId); page.setUuid(serviceContext.getUuid()); } page.setResourcePrimKey(resourcePrimKey); page.setGroupId(oldPage.getGroupId()); page.setCompanyId(user.getCompanyId()); page.setUserId(user.getUserId()); page.setUserName(user.getFullName()); page.setCreateDate(oldPage.getCreateDate()); page.setNodeId(nodeId); page.setTitle(Validator.isNull(newTitle) ? oldPage.getTitle() : newTitle); page.setVersion(newVersion); page.setMinorEdit(minorEdit); page.setContent(content); if (oldPage.isPending()) { page.setStatus(oldPage.getStatus()); } else { page.setStatus(WorkflowConstants.STATUS_DRAFT); } page.setSummary(summary); page.setFormat(format); if (Validator.isNotNull(parentTitle)) { page.setParentTitle(parentTitle); } if (Validator.isNotNull(redirectTitle)) { page.setRedirectTitle(redirectTitle); } ExpandoBridgeUtil.setExpandoBridgeAttributes(oldPage.getExpandoBridge(), page.getExpandoBridge(), serviceContext); wikiPagePersistence.update(page); // Node WikiNode node = wikiNodePersistence.findByPrimaryKey(nodeId); node.setLastPostDate(serviceContext.getModifiedDate(now)); wikiNodePersistence.update(node); // Asset updateAsset(userId, page, serviceContext.getAssetCategoryIds(), serviceContext.getAssetTagNames(), serviceContext.getAssetLinkEntryIds(), serviceContext.getAssetPriority()); // Workflow page = startWorkflowInstance(userId, page, serviceContext); return page; }