--- /dev/null
+/*******************************************************************************
+ * Copyright � Igor Barma, Alexandre Desoubeaux, Christian Martel, Eric Brun, Mathieu Amblard, Gwenael Gevet, Pierre Guillot, 2012
+ * Copyright Alexandre Desoubeaux, Christian Martel, Cedric Lecarpentier, Alexandre Lefevre, Marc Salvat 2014-2016
+ * Copyright Alexandre Desoubeaux, Christian Martel, Cedric Lecarpentier, Marc Salvat, Marc Suarez, Harifetra Ramamonjy 2017
+ *
+ * This file is part of the work and learning management system Pentila Nero.
+ *
+ * Pentila Nero is free software. You can redistribute it and/or modify since
+ * you respect the terms of either (at least one of the both license) :
+ * - under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ * - the CeCILL-C as published by CeCILL-C; either version 1 of the
+ * License, or any later version
+ * - the GNU Lesser General Public License as published by the
+ * Free Software Foundation, either version 3 of the license,
+ * or (at your option) any later version.
+ *
+ * There are special exceptions to the terms and conditions of the
+ * licenses as they are applied to this software. View the full text of
+ * the exception in file LICENSE-PROJECT.txt in the directory of this software
+ * distribution.
+ *
+ * Pentila Nero 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
+ * Licenses for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * and the CeCILL-C and the GNU Lesser General Public License along with
+ * Pentila Nero. If not, see :
+ * <http://www.gnu.org/licenses/> and
+ * <http://www.cecill.info/licences.fr.html>.
+ ******************************************************************************/
+package com.pentila.entSavoie.blog.service.impl;
+
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+import com.liferay.portal.kernel.exception.PortalException;
+import com.liferay.portal.kernel.exception.SystemException;
+import com.liferay.portal.kernel.util.ObjectValuePair;
+import com.liferay.portal.model.CompanyConstants;
+import com.liferay.portlet.documentlibrary.DuplicateDirectoryException;
+import com.liferay.portlet.documentlibrary.DuplicateFileException;
+import com.liferay.portlet.documentlibrary.NoSuchDirectoryException;
+import com.liferay.portlet.documentlibrary.store.DLStoreUtil;
+import com.pentila.entSavoie.blog.model.BlogCategory;
+import com.pentila.entSavoie.blog.model.BlogItem;
+import com.pentila.entSavoie.blog.service.BlogCategoryLocalServiceUtil;
+import com.pentila.entSavoie.blog.service.base.BlogItemLocalServiceBaseImpl;
+import com.pentila.entSavoie.blog.util.ThreadConvertMediaFile;
+
+
+public class BlogItemLocalServiceImpl extends BlogItemLocalServiceBaseImpl {
+
+ public BlogItem addBlogItem() throws SystemException {
+ final long blogItemId = counterLocalService.increment();
+ final BlogItem blogItem = this.createBlogItem(blogItemId);
+ return blogItem;
+ }
+
+ public BlogItem addBlogItem(long companyId, long channelId, long groupId, long userId, long categoryId, String title, String description, Date pubDate) throws SystemException{
+ BlogItem blogItem = addBlogItem();
+ blogItem.setCompanyId(companyId);
+ blogItem.setChannelId(channelId);
+ blogItem.setGroupId(groupId);
+ blogItem.setUserId(userId);
+ blogItem.setCategoryId(categoryId);
+ blogItem.setTitle(title);
+ blogItem.setDescription(description);
+ blogItem.setPubDate(pubDate);
+ blogItemPersistence.update(blogItem, false);
+ return blogItem;
+ }
+
+ public List<BlogItem> getBlogItemByCompanyId(long companyId) throws SystemException, PortalException{
+ return blogItemPersistence.findBycompanyId(companyId);
+ }
+
+ public void deleteBlogItemByCompanyId(long companyId) throws SystemException, PortalException{
+ blogItemPersistence.removeBycompanyId(companyId);
+ }
+
+ public List<BlogItem> getBlogItemByChannelId(long channelId) throws SystemException, PortalException{
+ return blogItemPersistence.findBychannelId(channelId);
+ }
+
+ public void deleteBlogItemByChannelId(long channelId) throws SystemException, PortalException{
+ blogItemPersistence.removeBychannelId(channelId);
+ }
+
+ public List<BlogItem> getBlogItemByGroupId(long groupId) throws SystemException, PortalException{
+ return blogItemPersistence.findBygroupId(groupId);
+ }
+
+ public void deleteBlogItemByGroupId(long groupId) throws SystemException, PortalException{
+ blogItemPersistence.removeBygroupId(groupId);
+ }
+
+ public List<BlogItem> getBlogItemByUserId(long userId) throws SystemException, PortalException{
+ return blogItemPersistence.findByuserId(userId);
+ }
+
+ public void deleteBlogItemByUserId(long userId) throws SystemException, PortalException{
+ blogItemPersistence.removeByuserId(userId);
+ }
+
+ public List<BlogItem> getBlogItemByPubDate(Date currentDate) throws SystemException, PortalException{
+ return blogItemPersistence.findBypubDate(currentDate);
+ }
+
+ public List<BlogItem> getBlogItemByChannelId_PubDate(long channelId, Date currentDate) throws SystemException, PortalException{
+ return blogItemPersistence.findBychannelId_pubDate(channelId, currentDate);
+ }
+
+ public List<BlogItem> getBlogItemByGroupId_PubDate(long groupId,Date currentDate) throws SystemException, PortalException{
+ return blogItemPersistence.findBygroupId_pubDate(groupId, currentDate);
+ }
+
+ public List<BlogItem> getBlogItemByUserId_PubDate(long userId,Date currentDate) throws SystemException, PortalException{
+ return blogItemPersistence.findByuserId_pubDate(userId, currentDate);
+ }
+
+ public List<BlogItem> getBlogItemByCategoryId_PubDate(long categoryId,Date currentDate) throws SystemException, PortalException{
+ return blogItemPersistence.findBycategoryId_pubDate(categoryId, currentDate);
+ }
+
+ public List<BlogItem> getBlogItemByCategoryId(long categoryId) throws SystemException, PortalException{
+ return blogItemPersistence.findBycategoryId(categoryId);
+ }
+
+ public void deleteBlogItemByCategoryId(long categoryId) throws SystemException, PortalException{
+ blogItemPersistence.removeBycategoryId(categoryId);
+ }
+
+ public int countBlogItemByCategoryId(long categoryId) throws SystemException, PortalException{
+ return blogItemPersistence.countBycategoryId(categoryId);
+ }
+
+ private Set<Long> getChildCategoryList(Set<Long> categories, long categoryId) throws PortalException, SystemException{
+
+ categories.add(categoryId);
+ List<BlogCategory> lbc = BlogCategoryLocalServiceUtil.getBlogCategoryByParentCategoryId(categoryId);
+
+ for(BlogCategory bc: lbc){
+ getChildCategoryList(categories, bc.getCategoryId());
+ }
+
+ return categories;
+ }
+
+ public List<BlogItem> getBlogItemByCategoryId(long channelId, long categoryId) throws SystemException, PortalException{
+
+ List<BlogItem> items = blogItemPersistence.findBychannelId(channelId);
+ Set<Long> categoryIds = new HashSet<Long>();
+ categoryIds = getChildCategoryList(categoryIds, categoryId);
+
+ List<BlogItem> itemsFinal = new ArrayList<BlogItem>();
+ for(BlogItem item: items){
+ if(categoryIds.contains(item.getCategoryId())){
+ itemsFinal.add(item);
+ }
+ }
+
+ return itemsFinal;
+ }
+
+ // Mise a jour des pieces jointes
+ public void updateAttachements(long itemId, List<ObjectValuePair<String, byte[]>> files,
+ List<String> existingFiles) throws SystemException, PortalException {
+
+ BlogItem item = blogItemPersistence.fetchByPrimaryKey(itemId);
+
+ // Attachments
+ long companyId = item.getCompanyId();
+ long repositoryId = CompanyConstants.SYSTEM;
+ String dirName = item.getAttachmentsDir();
+
+ if (!files.isEmpty() || !existingFiles.isEmpty()) {
+ try {
+ DLStoreUtil.addDirectory(companyId, repositoryId, dirName);
+ }
+ catch (DuplicateDirectoryException dde) {
+ }
+
+ String[] fileNames = DLStoreUtil.getFileNames(
+ companyId, repositoryId, dirName);
+
+ for (String fileName: fileNames) {
+ if (!existingFiles.contains(fileName)) {
+ DLStoreUtil.deleteFile(
+ companyId, repositoryId, fileName);
+ }
+ }
+
+ for (int i = 0; i < files.size(); i++) {
+ ObjectValuePair<String, byte[]> ovp = files.get(i);
+
+ String fileName = ovp.getKey();
+ byte[] bytes = ovp.getValue();
+
+ try {
+ DLStoreUtil.addFile(
+ companyId, repositoryId,
+ dirName + "/" + fileName, true, bytes);
+ }
+ catch (DuplicateFileException dfe) {
+ }
+ }
+ }
+ else {
+ try {
+ DLStoreUtil.deleteDirectory(
+ companyId, repositoryId, dirName);
+ }
+ catch (NoSuchDirectoryException nsde) {
+ }
+ }
+ }
+
+ // Mise a jour du media original
+ public void updateMediaOriginalFile(long itemId, ObjectValuePair<String, byte[]> file,
+ String existingFile) throws PortalException, SystemException{
+
+ BlogItem item = blogItemPersistence.fetchByPrimaryKey(itemId);
+
+ // MediaOriginalFile
+ long companyId = item.getCompanyId();
+ long repositoryId = CompanyConstants.SYSTEM;
+ String dirName = item.getMediaOriginalFileDir();
+
+ if (file != null || !existingFile.isEmpty()) {
+ try {
+ DLStoreUtil.addDirectory(companyId, repositoryId, dirName);
+ }
+ catch (DuplicateDirectoryException dde) {
+ }
+
+ String[] fileNames = DLStoreUtil.getFileNames(
+ companyId, repositoryId, dirName);
+
+ for (String fileName: fileNames) {
+ DLStoreUtil.deleteFile(
+ companyId, repositoryId, fileName);
+ }
+
+ if(file != null){
+ ObjectValuePair<String, byte[]> ovp = file;
+
+ String fileName = ovp.getKey();
+ byte[] bytes = ovp.getValue();
+
+ try {
+ DLStoreUtil.addFile(
+ companyId, repositoryId,
+ dirName + "/" + fileName, true, bytes);
+ }
+ catch (DuplicateFileException dfe) {
+ }
+
+ // Conversion du fichier
+ Thread tcmf = new ThreadConvertMediaFile(itemId, fileName, bytes, companyId);
+ tcmf.start();
+ }
+ }
+ else {
+ try {
+ DLStoreUtil.deleteDirectory(
+ companyId, repositoryId, dirName);
+ }
+ catch (NoSuchDirectoryException nsde) {
+ }
+ }
+ }
+
+ // Mise a jour du media original
+ public void updateMediaConvertedFile(long itemId, ObjectValuePair<String, byte[]> file,
+ String existingFile) throws PortalException, SystemException{
+
+ BlogItem item = blogItemPersistence.fetchByPrimaryKey(itemId);
+
+ // MediaOriginalFile
+ long companyId = item.getCompanyId();
+ long repositoryId = CompanyConstants.SYSTEM;
+ String dirName = item.getMediaConvertedFileDir();
+
+ if (file != null || !existingFile.isEmpty()) {
+ try {
+ DLStoreUtil.addDirectory(companyId, repositoryId, dirName);
+ }
+ catch (DuplicateDirectoryException dde) {
+ }
+
+ String[] fileNames = DLStoreUtil.getFileNames(
+ companyId, repositoryId, dirName);
+
+ for (String fileName: fileNames) {
+ DLStoreUtil.deleteFile(
+ companyId, repositoryId, fileName);
+ }
+
+ if(file != null){
+ ObjectValuePair<String, byte[]> ovp = file;
+
+ String fileName = ovp.getKey();
+ byte[] bytes = ovp.getValue();
+
+ try {
+ DLStoreUtil.addFile(
+ companyId, repositoryId,
+ dirName + "/" + fileName, true, bytes);
+ }
+ catch (DuplicateFileException dfe) {
+ }
+ }
+ }
+ else {
+ try {
+ DLStoreUtil.deleteDirectory(
+ companyId, repositoryId, dirName);
+ }
+ catch (NoSuchDirectoryException nsde) {
+ }
+ }
+ }
+
+ // Mise a jour de l'image
+ public void updateImageFile(long itemId, ObjectValuePair<String, byte[]> file,
+ String existingFile) throws PortalException, SystemException{
+
+ BlogItem item = blogItemPersistence.fetchByPrimaryKey(itemId);
+
+ // ImageFile
+ long companyId = item.getCompanyId();
+ long repositoryId = CompanyConstants.SYSTEM;
+ String dirName = item.getImageFileDir();
+
+ if (file != null || !existingFile.isEmpty()) {
+ try {
+ DLStoreUtil.addDirectory(companyId, repositoryId, dirName);
+ }
+ catch (DuplicateDirectoryException dde) {
+ }
+
+ String[] fileNames = DLStoreUtil.getFileNames(
+ companyId, repositoryId, dirName);
+
+ for (String fileName: fileNames) {
+ DLStoreUtil.deleteFile(
+ companyId, repositoryId, fileName);
+ }
+
+ if(file != null){
+ ObjectValuePair<String, byte[]> ovp = file;
+
+ String fileName = ovp.getKey();
+ byte[] bytes = ovp.getValue();
+
+ try {
+ DLStoreUtil.addFile(
+ companyId, repositoryId,
+ dirName + "/" + fileName, true, bytes);
+ }
+ catch (DuplicateFileException dfe) {
+ }
+ }
+ }
+ else {
+ try {
+ DLStoreUtil.deleteDirectory(
+ companyId, repositoryId, dirName);
+ }
+ catch (NoSuchDirectoryException nsde) {
+ }
+ }
+ }
+
+ // Suppression du blog directory
+ public void deleteBlogDirectory(long itemId) throws PortalException, SystemException{
+
+ BlogItem item = blogItemPersistence.fetchByPrimaryKey(itemId);
+
+ long companyId = item.getCompanyId();
+ long repositoryId = CompanyConstants.SYSTEM;
+ String dirName = item.getBlogDir();
+
+ DLStoreUtil.deleteDirectory(
+ companyId, repositoryId, dirName);
+ }
+
+}