com.liferay.fragment.service.impl.FragmentEntryLocalServiceImpl Maven / Gradle / Ivy
/**
* Copyright (c) 2000-present Liferay, Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*/
package com.liferay.fragment.service.impl;
import com.liferay.fragment.exception.DuplicateFragmentEntryKeyException;
import com.liferay.fragment.exception.FragmentEntryContentException;
import com.liferay.fragment.exception.FragmentEntryNameException;
import com.liferay.fragment.exception.RequiredFragmentEntryException;
import com.liferay.fragment.model.FragmentEntry;
import com.liferay.fragment.processor.FragmentEntryProcessorRegistry;
import com.liferay.fragment.service.base.FragmentEntryLocalServiceBaseImpl;
import com.liferay.petra.string.CharPool;
import com.liferay.petra.string.StringPool;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.model.ModelHintsUtil;
import com.liferay.portal.kernel.model.SystemEventConstants;
import com.liferay.portal.kernel.model.User;
import com.liferay.portal.kernel.service.ServiceContext;
import com.liferay.portal.kernel.systemevent.SystemEvent;
import com.liferay.portal.kernel.util.OrderByComparator;
import com.liferay.portal.kernel.util.StringUtil;
import com.liferay.portal.kernel.util.TempFileEntryUtil;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.portal.kernel.workflow.WorkflowConstants;
import com.liferay.portal.spring.extender.service.ServiceReference;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
/**
* @author Jürgen Kappler
*/
public class FragmentEntryLocalServiceImpl
extends FragmentEntryLocalServiceBaseImpl {
@Override
public FragmentEntry addFragmentEntry(
long userId, long groupId, long fragmentCollectionId, String name,
int status, ServiceContext serviceContext)
throws PortalException {
return addFragmentEntry(
userId, groupId, fragmentCollectionId, StringPool.BLANK, name, 0,
status, serviceContext);
}
@Override
public FragmentEntry addFragmentEntry(
long userId, long groupId, long fragmentCollectionId, String name,
long previewFileEntryId, int status, ServiceContext serviceContext)
throws PortalException {
return addFragmentEntry(
userId, groupId, fragmentCollectionId, StringPool.BLANK, name,
previewFileEntryId, status, serviceContext);
}
@Override
public FragmentEntry addFragmentEntry(
long userId, long groupId, long fragmentCollectionId,
String fragmentEntryKey, String name, int status,
ServiceContext serviceContext)
throws PortalException {
return addFragmentEntry(
userId, groupId, fragmentCollectionId, fragmentEntryKey, name, 0,
status, serviceContext);
}
@Override
public FragmentEntry addFragmentEntry(
long userId, long groupId, long fragmentCollectionId,
String fragmentEntryKey, String name, long previewFileEntryId,
int status, ServiceContext serviceContext)
throws PortalException {
// Fragment entry
User user = userLocalService.getUser(userId);
validate(name);
if (Validator.isNull(fragmentEntryKey)) {
fragmentEntryKey = _generateFragmentEntryKey(groupId, name);
}
fragmentEntryKey = _getFragmentEntryKey(fragmentEntryKey);
validateFragmentEntryKey(groupId, fragmentEntryKey);
long fragmentEntryId = counterLocalService.increment();
FragmentEntry fragmentEntry = fragmentEntryPersistence.create(
fragmentEntryId);
fragmentEntry.setUuid(serviceContext.getUuid());
fragmentEntry.setGroupId(groupId);
fragmentEntry.setCompanyId(user.getCompanyId());
fragmentEntry.setUserId(user.getUserId());
fragmentEntry.setUserName(user.getFullName());
fragmentEntry.setCreateDate(serviceContext.getCreateDate(new Date()));
fragmentEntry.setModifiedDate(
serviceContext.getModifiedDate(new Date()));
fragmentEntry.setFragmentCollectionId(fragmentCollectionId);
fragmentEntry.setFragmentEntryKey(fragmentEntryKey);
fragmentEntry.setName(name);
fragmentEntry.setPreviewFileEntryId(previewFileEntryId);
fragmentEntry.setStatus(status);
fragmentEntry.setStatusByUserId(userId);
fragmentEntry.setStatusByUserName(user.getFullName());
fragmentEntry.setStatusDate(new Date());
return fragmentEntryPersistence.update(fragmentEntry);
}
@Override
public FragmentEntry addFragmentEntry(
long userId, long groupId, long fragmentCollectionId, String name,
String css, String html, String js, int status,
ServiceContext serviceContext)
throws PortalException {
return addFragmentEntry(
userId, groupId, fragmentCollectionId, StringPool.BLANK, name, css,
html, js, status, serviceContext);
}
@Override
public FragmentEntry addFragmentEntry(
long userId, long groupId, long fragmentCollectionId, String name,
String css, String html, String js, long previewFileEntryId,
int status, ServiceContext serviceContext)
throws PortalException {
return addFragmentEntry(
userId, groupId, fragmentCollectionId, StringPool.BLANK, name, css,
html, js, previewFileEntryId, status, serviceContext);
}
@Override
public FragmentEntry addFragmentEntry(
long userId, long groupId, long fragmentCollectionId,
String fragmentEntryKey, String name, String css, String html,
String js, int status, ServiceContext serviceContext)
throws PortalException {
return addFragmentEntry(
userId, groupId, fragmentCollectionId, fragmentEntryKey, name, css,
html, js, 0, status, serviceContext);
}
@Override
public FragmentEntry addFragmentEntry(
long userId, long groupId, long fragmentCollectionId,
String fragmentEntryKey, String name, String css, String html,
String js, long previewFileEntryId, int status,
ServiceContext serviceContext)
throws PortalException {
// Fragment entry
User user = userLocalService.getUser(userId);
if (Validator.isNull(fragmentEntryKey)) {
fragmentEntryKey = String.valueOf(counterLocalService.increment());
}
else {
fragmentEntryKey = _getFragmentEntryKey(fragmentEntryKey);
}
validate(name);
validateFragmentEntryKey(groupId, fragmentEntryKey);
if (WorkflowConstants.STATUS_APPROVED == status) {
validateContent(html);
html = _parseHTMLContent(html);
}
long fragmentEntryId = counterLocalService.increment();
FragmentEntry fragmentEntry = fragmentEntryPersistence.create(
fragmentEntryId);
fragmentEntry.setUuid(serviceContext.getUuid());
fragmentEntry.setGroupId(groupId);
fragmentEntry.setCompanyId(user.getCompanyId());
fragmentEntry.setUserId(user.getUserId());
fragmentEntry.setUserName(user.getFullName());
fragmentEntry.setCreateDate(serviceContext.getCreateDate(new Date()));
fragmentEntry.setModifiedDate(
serviceContext.getModifiedDate(new Date()));
fragmentEntry.setFragmentCollectionId(fragmentCollectionId);
fragmentEntry.setFragmentEntryKey(fragmentEntryKey);
fragmentEntry.setName(name);
fragmentEntry.setCss(css);
fragmentEntry.setHtml(html);
fragmentEntry.setJs(js);
fragmentEntry.setPreviewFileEntryId(previewFileEntryId);
fragmentEntry.setStatus(status);
fragmentEntry.setStatusByUserId(userId);
fragmentEntry.setStatusByUserName(user.getFullName());
fragmentEntry.setStatusDate(new Date());
return fragmentEntryPersistence.update(fragmentEntry);
}
@Override
@SystemEvent(type = SystemEventConstants.TYPE_DELETE)
public FragmentEntry deleteFragmentEntry(FragmentEntry fragmentEntry)
throws PortalException {
// Fragment entry
long fragmentEntryLinkCount = fragmentEntryLinkPersistence.countByG_F(
fragmentEntry.getGroupId(), fragmentEntry.getFragmentEntryId());
if (fragmentEntryLinkCount > 0) {
throw new RequiredFragmentEntryException();
}
fragmentEntryPersistence.remove(fragmentEntry);
return fragmentEntry;
}
@Override
public FragmentEntry deleteFragmentEntry(long fragmentEntryId)
throws PortalException {
FragmentEntry fragmentEntry = getFragmentEntry(fragmentEntryId);
return fragmentEntryLocalService.deleteFragmentEntry(fragmentEntry);
}
@Override
public FragmentEntry fetchFragmentEntry(long fragmentEntryId) {
return fragmentEntryPersistence.fetchByPrimaryKey(fragmentEntryId);
}
@Override
public FragmentEntry fetchFragmentEntry(
long groupId, String fragmentEntryKey) {
return fragmentEntryPersistence.fetchByG_FEK(
groupId, _getFragmentEntryKey(fragmentEntryKey));
}
@Override
public List getFragmentEntries(long fragmentCollectionId) {
return fragmentEntryPersistence.findByFragmentCollectionId(
fragmentCollectionId);
}
@Override
public List getFragmentEntries(
long fragmentCollectionId, int start, int end) {
return fragmentEntryPersistence.findByFragmentCollectionId(
fragmentCollectionId, start, end);
}
@Override
public List getFragmentEntries(
long groupId, long fragmentCollectionId, int status) {
return fragmentEntryPersistence.findByG_FCI_S(
groupId, fragmentCollectionId, status);
}
@Override
public List getFragmentEntries(
long groupId, long fragmentCollectionId, int start, int end,
OrderByComparator orderByComparator) {
return fragmentEntryPersistence.findByG_FCI(
groupId, fragmentCollectionId, start, end, orderByComparator);
}
@Override
public List getFragmentEntries(
long groupId, long fragmentCollectionId, String name, int start,
int end, OrderByComparator orderByComparator) {
if (Validator.isNull(name)) {
return fragmentEntryPersistence.findByG_FCI(
groupId, fragmentCollectionId, start, end, orderByComparator);
}
return fragmentEntryPersistence.findByG_FCI_LikeN(
groupId, fragmentCollectionId, name, start, end, orderByComparator);
}
@Override
public int getFragmentEntriesCount(long fragmentCollectionId) {
return fragmentEntryPersistence.countByFragmentCollectionId(
fragmentCollectionId);
}
@Override
public String[] getTempFileNames(
long userId, long groupId, String folderName)
throws PortalException {
return TempFileEntryUtil.getTempFileNames(groupId, userId, folderName);
}
@Override
public FragmentEntry updateFragmentEntry(
long fragmentEntryId, long previewFileEntryId)
throws PortalException {
FragmentEntry fragmentEntry = fragmentEntryPersistence.findByPrimaryKey(
fragmentEntryId);
fragmentEntry.setModifiedDate(new Date());
fragmentEntry.setPreviewFileEntryId(previewFileEntryId);
return fragmentEntryPersistence.update(fragmentEntry);
}
@Override
public FragmentEntry updateFragmentEntry(
long userId, long fragmentEntryId, String name, String css,
String html, String js, int status)
throws PortalException {
FragmentEntry fragmentEntry = fragmentEntryPersistence.findByPrimaryKey(
fragmentEntryId);
return updateFragmentEntry(
userId, fragmentEntryId, name, css, html, js,
fragmentEntry.getPreviewFileEntryId(), status);
}
@Override
public FragmentEntry updateFragmentEntry(
long userId, long fragmentEntryId, String name, String css,
String html, String js, long previewFileEntryId, int status)
throws PortalException {
FragmentEntry fragmentEntry = fragmentEntryPersistence.findByPrimaryKey(
fragmentEntryId);
validate(name);
if (WorkflowConstants.STATUS_APPROVED == status) {
validateContent(html);
html = _parseHTMLContent(html);
}
User user = userLocalService.getUser(userId);
fragmentEntry.setModifiedDate(new Date());
fragmentEntry.setName(name);
fragmentEntry.setCss(css);
fragmentEntry.setHtml(html);
fragmentEntry.setJs(js);
fragmentEntry.setPreviewFileEntryId(previewFileEntryId);
fragmentEntry.setStatus(status);
fragmentEntry.setStatusByUserId(userId);
fragmentEntry.setStatusByUserName(user.getFullName());
fragmentEntry.setStatusDate(new Date());
return fragmentEntryPersistence.update(fragmentEntry);
}
@Override
public FragmentEntry updateFragmentEntry(long fragmentEntryId, String name)
throws PortalException {
FragmentEntry fragmentEntry = fragmentEntryPersistence.findByPrimaryKey(
fragmentEntryId);
if (Objects.equals(fragmentEntry.getName(), name)) {
return fragmentEntry;
}
validate(name);
fragmentEntry.setName(name);
return fragmentEntryPersistence.update(fragmentEntry);
}
protected void validate(String name) throws PortalException {
if (Validator.isNull(name)) {
throw new FragmentEntryNameException("Name must not be null");
}
if (name.contains(StringPool.PERIOD) ||
name.contains(StringPool.SLASH)) {
throw new FragmentEntryNameException(
"Name contains invalid characters");
}
int nameMaxLength = ModelHintsUtil.getMaxLength(
FragmentEntry.class.getName(), "name");
if (name.length() > nameMaxLength) {
throw new FragmentEntryNameException(
"Maximum length of name exceeded");
}
}
protected void validateContent(String html) throws PortalException {
_fragmentEntryProcessorRegistry.validateFragmentEntryHTML(html);
}
protected void validateFragmentEntryKey(
long groupId, String fragmentEntryKey)
throws PortalException {
fragmentEntryKey = _getFragmentEntryKey(fragmentEntryKey);
FragmentEntry fragmentEntry = fragmentEntryPersistence.fetchByG_FEK(
groupId, fragmentEntryKey);
if (fragmentEntry != null) {
throw new DuplicateFragmentEntryKeyException();
}
}
private String _generateFragmentEntryKey(long groupId, String name) {
String fragmentEntryKey = _getFragmentEntryKey(name);
fragmentEntryKey = StringUtil.replace(
fragmentEntryKey, CharPool.SPACE, CharPool.DASH);
String curFragmentEntryKey = fragmentEntryKey;
int count = 0;
while (true) {
FragmentEntry fragmentEntry = fragmentEntryPersistence.fetchByG_FEK(
groupId, curFragmentEntryKey);
if (fragmentEntry == null) {
return curFragmentEntryKey;
}
curFragmentEntryKey = fragmentEntryKey + CharPool.DASH + count++;
}
}
private String _getFragmentEntryKey(String fragmentEntryKey) {
if (fragmentEntryKey != null) {
fragmentEntryKey = fragmentEntryKey.trim();
return StringUtil.toLowerCase(fragmentEntryKey);
}
return StringPool.BLANK;
}
private String _parseHTMLContent(String html)
throws FragmentEntryContentException {
Document document = Jsoup.parseBodyFragment(html);
Document.OutputSettings outputSettings = new Document.OutputSettings();
outputSettings.prettyPrint(false);
document.outputSettings(outputSettings);
Element bodyElement = document.body();
String bodyHtml = bodyElement.html();
if (Validator.isNull(bodyHtml)) {
throw new FragmentEntryContentException();
}
return bodyHtml;
}
@ServiceReference(type = FragmentEntryProcessorRegistry.class)
private FragmentEntryProcessorRegistry _fragmentEntryProcessorRegistry;
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy