--- /dev/null
+/**
+ * Copyright (c) 2000-2012 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.pentila.entSavoie.service.impl;
+
+import java.io.File;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+
+import javax.naming.NamingException;
+
+import com.liferay.counter.service.CounterLocalServiceUtil;
+import com.liferay.portal.kernel.exception.PortalException;
+import com.liferay.portal.kernel.exception.SystemException;
+import com.liferay.portal.kernel.log.Log;
+import com.liferay.portal.kernel.log.LogFactoryUtil;
+import com.liferay.portal.kernel.repository.model.FileEntry;
+import com.liferay.portal.kernel.repository.model.Folder;
+import com.liferay.portal.model.User;
+import com.liferay.portal.service.UserLocalServiceUtil;
+import com.liferay.portlet.documentlibrary.model.DLFileEntry;
+import com.pentila.entSavoie.cartable.service.CasierEntryLocalServiceUtil;
+import com.pentila.entSavoie.directory.OrganizationFinderServiceUtil;
+import com.pentila.entSavoie.model.DiskUsageUser;
+import com.pentila.entSavoie.service.DiskUsageUserLocalServiceUtil;
+import com.pentila.entSavoie.service.base.DiskUsageUserLocalServiceBaseImpl;
+import com.pentila.entSavoie.service.persistence.DiskUsageUserFinderUtil;
+import com.pentila.entSavoie.userManagement.service.UsersUtilsLocalServiceUtil;
+import com.pentila.entSavoie.utils.DLEntryUtil;
+import com.pentila.entSavoie.utils.PortletConstant;
+
+/**
+ * The implementation of the disk usage user local service.
+ *
+ * <p>
+ * All custom service methods should be put in this class. Whenever methods are added, rerun ServiceBuilder to copy their definitions into the {@link com.pentila.entSavoie.service.DiskUsageUserLocalService} interface.
+ *
+ * <p>
+ * This is a local service. Methods of this service will not have security checks based on the propagated JAAS credentials because this service can only be accessed from within the same VM.
+ * </p>
+ *
+ * @author Alexandre Lefevre
+ * @see com.pentila.entSavoie.service.base.DiskUsageUserLocalServiceBaseImpl
+ * @see com.pentila.entSavoie.service.DiskUsageUserLocalServiceUtil
+ */
+public class DiskUsageUserLocalServiceImpl
+ extends DiskUsageUserLocalServiceBaseImpl {
+ /*
+ * NOTE FOR DEVELOPERS:
+ *
+ * Never reference this interface directly. Always use {@link com.pentila.entSavoie.service.DiskUsageUserLocalServiceUtil} to access the disk usage user local service.
+ */
+
+
+ /*
+ * Value which represent the number of byte in megabyte. This is based on IEC standard 1000 based (and not SI standard which are 1024 based) = 1000000
+ */
+ public static long MEGA_BYTE= 1000000;
+
+ private static Log logger = LogFactoryUtil.getLog(DiskUsageUserLocalServiceImpl.class);
+
+ /**
+ * Get the users Quota for user in an etab
+ * @param pOrganizationId the etab Id
+ * @return Map with user id as key and . Disk usage in Mb in value
+ * @throws PortalException
+ * @throws SystemException
+ */
+ public HashMap<Long, Long> getUsersQuotasOnEtab(long pOrganizationId) throws PortalException, SystemException{
+ // get user for etab
+ List<User> users = UserLocalServiceUtil.getOrganizationUsers(pOrganizationId);
+ HashMap<Long,Long> mapUserIdQuota = new HashMap <Long,Long> ();
+
+ // get disk usage for users (userId column musn't be at 0)
+ List<DiskUsageUser> diskUsages = new ArrayList<DiskUsageUser>();
+ diskUsages.addAll(diskUsageUserPersistence.findByetabId(pOrganizationId));
+
+ for(User aUser : users){
+
+ int iterator = 0;
+ boolean found = false;
+ long sizeInMegabyte = 0;
+
+ while( (!found) && (iterator < diskUsages.size()) ){
+ if(diskUsages.get(iterator).getUserId() == aUser.getUserId()){
+ found = true;
+ sizeInMegabyte = diskUsages.get(iterator).getGlobalSize()/MEGA_BYTE;
+ diskUsages.remove(iterator);
+ }
+ iterator++;
+ }
+ mapUserIdQuota.put(aUser.getUserId(), sizeInMegabyte);
+ }
+ return mapUserIdQuota;
+ }
+
+ /**
+ * Get the users Quota for user in an etab. It return only mail size
+ * @param pOrganizationId the etab Id
+ * @return Map with user id as key and . Disk usage in Mb in value
+ * @throws PortalException
+ * @throws SystemException
+ */
+ public HashMap<Long, Long> getUserMessagingQuotasOnEtab(long pOrganizationId) throws PortalException, SystemException{
+ // get user for etab
+ List<User> users = UserLocalServiceUtil.getOrganizationUsers(pOrganizationId);
+ HashMap<Long,Long> mapUserIdQuota = new HashMap <Long,Long> ();
+
+ // get disk usage for users (userId column musn't be at 0)
+ List<DiskUsageUser> diskUsages = new ArrayList<DiskUsageUser>();
+ diskUsages.addAll(diskUsageUserPersistence.findByetabId(pOrganizationId));
+
+ for(User aUser : users){
+
+ int iterator = 0;
+ boolean found = false;
+ long sizeInMegabyte = 0;
+
+ while( (!found) && (iterator < diskUsages.size()) ){
+ if(diskUsages.get(iterator).getUserId() == aUser.getUserId()){
+ found = true;
+ sizeInMegabyte = diskUsages.get(iterator).getMailSize()/MEGA_BYTE;
+ diskUsages.remove(iterator);
+ }
+ iterator++;
+ }
+ mapUserIdQuota.put(aUser.getUserId(), sizeInMegabyte);
+ }
+ return mapUserIdQuota;
+ }
+
+ /**
+ * This method add the size of DLFileEntry to Disk usage of the user in parameter.
+ * @param pFile DLFileEntry which has uploaded by user.
+ * @param pUserId The user which uploaded file
+ * @param pPortletId A string representing the Portlet ID, this could be found on request : PortalUtil.getPortletId(request)
+ * @return true if this works, false otherwise
+ * @throws SystemException
+ */
+ public boolean addDiskUsage(DLFileEntry pFile, long pUserId, String pPortletId){
+ try {
+ return this.addDiskUsagePrivate(pFile.getSize(),pUserId,pPortletId);
+ } catch (Exception e) {
+ return false;
+ }
+ }
+
+ /**
+ * This method add the size of FileEntry to Disk usage of the user in parameter.
+ * @param pFile DLFileEntry which has uploaded by user.
+ * @param pUserId The user which uploaded file
+ * @param pPortletId A string representing the Portlet ID, this could be found on request : PortalUtil.getPortletId(request)
+ * @return true if this works, false otherwise
+ * @throws SystemException
+ */
+ public boolean addDiskUsage(FileEntry pFile, long pUserId, String pPortletId){
+ try {
+ return this.addDiskUsagePrivate(pFile.getSize(),pUserId,pPortletId);
+ } catch (Exception e) {
+ return false;
+ }
+ }
+
+ /**
+ * This method add the size of Folder and sub files to Disk usage of the user in parameter.
+ * @param pFolder Folder which has uploaded by user.
+ * @param pUserId The user which uploaded file
+ * @param pPortletId A string representing the Portlet ID, this could be found on request : PortalUtil.getPortletId(request)
+ * @return true if this works, false otherwise
+ * @throws SystemException
+ */
+ public boolean addDiskUsage(Folder pFolder, long pUserId, String pPortletId){
+ try {
+ long folderSize = DLEntryUtil.getFolderTotalSize(pFolder.getFolderId());
+ return this.addDiskUsagePrivate(folderSize,pUserId,pPortletId);
+ } catch (Exception e) {
+ return false;
+ }
+ }
+
+ /**
+ * This method add the size of Folder and sub files to Disk usage of the user in parameter.
+ * @param pFile File which has uploaded by user.
+ * @param pUserId The user which uploaded file
+ * @param pPortletId A string representing the Portlet ID, this could be found on request : PortalUtil.getPortletId(request)
+ * @return true if this works, false otherwise
+ * @throws SystemException
+ */
+ public boolean addDiskUsage(File pFile, long pUserId, String pPortletId){
+ try {
+ return this.addDiskUsagePrivate(pFile.getTotalSpace(),pUserId,pPortletId);
+ } catch (Exception e) {
+ return false;
+ }
+ }
+
+ /**
+ * This method substract the size of FileEntry to Disk usage of the user in parameter.
+ * @param pFileSize size in byte of file removed by user
+ * @param pUserId The user which uploaded file
+ * @param pPortletId A string representing the Portlet ID, this could be found on request : PortalUtil.getPortletId(request)
+ * @return true if this works, false otherwise
+ * @throws SystemException
+ */
+ public boolean addDiskUsage(long pFileSize, long pUserId, String pPortletId){
+ try {
+ return this.addDiskUsagePrivate(pFileSize,pUserId,pPortletId);
+ } catch (Exception e) {
+ return false;
+ }
+ }
+
+ /**
+ * This method take the size of file and add it to the portlet disk usage and total disk usage.
+ * Can't call this method directly
+ * @throws Exception
+ * @throws NamingException
+ */
+ public boolean addDiskUsagePrivate(long pFileSize, long pUserId, String pPortletId) throws Exception{
+ DiskUsageUser userDiskUsage = diskUsageUserPersistence.fetchByuserId(pUserId);
+
+ if ( userDiskUsage == null)
+ {
+ long newId = CounterLocalServiceUtil.increment(DiskUsageUser.class.getName());;
+ userDiskUsage = diskUsageUserLocalService.createDiskUsageUser(newId);
+ userDiskUsage.setUserId(pUserId);
+ long organizationId = OrganizationFinderServiceUtil.getEtabRatachement(UserLocalServiceUtil.getUser(pUserId)).getOrganizationId();
+ userDiskUsage.setEtabId(organizationId);
+ diskUsageUserLocalService.addDiskUsageUser(userDiskUsage);
+ }
+
+ if ( pPortletId.equals(PortletConstant.BLOG_PORTLET_ID) ) {
+ long blogDiskUsage = userDiskUsage.getBlogSize() + pFileSize;
+ userDiskUsage.setBlogSize(blogDiskUsage);
+ }
+ else if ( pPortletId.equals(PortletConstant.MESSAGERIE_PORTLET_ID) ) {
+ long mailDiskUsage = userDiskUsage.getMailSize() + pFileSize;
+ userDiskUsage.setMailSize(mailDiskUsage);
+ }
+ else if ( pPortletId.equals(PortletConstant.CARTABLE_PORTLET_ID) ) {
+ long cartableDiskUsage = userDiskUsage.getCartableSize() + pFileSize;
+ userDiskUsage.setCartableSize(cartableDiskUsage);
+ }
+ else{
+ return false;
+ }
+
+ long totalDiskUsage = userDiskUsage.getGlobalSize() + pFileSize;
+ userDiskUsage.setGlobalSize(totalDiskUsage);
+
+ diskUsageUserLocalService.updateDiskUsageUser(userDiskUsage);
+ return true;
+ }
+
+ /**
+ * This method substract the size of DLFileEntry to Disk usage of the user in parameter.
+ * @param pFile DLFileEntry removed by user.
+ * @param pUserId The user which uploaded file
+ * @param pPortletId A string representing the Portlet ID, this could be found on request : PortalUtil.getPortletId(request)
+ * @return true if this works, false otherwise
+ * @throws SystemException
+ */
+ public boolean substractDiskUsage(DLFileEntry pFile, long pUserId, String pPortletId){
+ try {
+ return this.substractDiskUsagePrivate(pFile.getSize(),pUserId,pPortletId);
+ } catch (Exception e) {
+ return false;
+ }
+ }
+
+ /**
+ * This method substract the size of Folder and subfile to Disk usage of the user in parameter.
+ * @param pFolder Folder removed by user.
+ * @param pUserId The user which uploaded file
+ * @param pPortletId A string representing the Portlet ID, this could be found on request : PortalUtil.getPortletId(request)
+ * @return true if this works, false otherwise
+ * @throws SystemException
+ */
+ public boolean substractDiskUsage(Folder pFolder, long pUserId, String pPortletId){
+ try {
+ long folderSize = DLEntryUtil.getFolderTotalSize(pFolder.getFolderId());
+ return this.substractDiskUsagePrivate(folderSize,pUserId,pPortletId);
+ } catch (Exception e) {
+ return false;
+ }
+ }
+
+ /**
+ * This method substract the size of FileEntry to Disk usage of the user in parameter.
+ * @param pFile DLFileEntry which removed by user.
+ * @param pUserId The user which uploaded file
+ * @param pPortletId A string representing the Portlet ID, this could be found on request : PortalUtil.getPortletId(request)
+ * @return true if this works, false otherwise
+ * @throws SystemException
+ */
+ public boolean substractDiskUsage(FileEntry pFile, long pUserId, String pPortletId){
+ try {
+ return this.substractDiskUsagePrivate(pFile.getSize(),pUserId,pPortletId);
+ } catch (Exception e) {
+ return false;
+ }
+ }
+
+ /**
+ * This method substract the size of FileEntry to Disk usage of the user in parameter.
+ * @param pFileSize size in byte of file removed by user
+ * @param pUserId The user which uploaded file
+ * @param pPortletId A string representing the Portlet ID, this could be found on request : PortalUtil.getPortletId(request)
+ * @return true if this works, false otherwise
+ * @throws SystemException
+ */
+ public boolean substractDiskUsage(long pFileSize, long pUserId, String pPortletId){
+ try {
+ return this.substractDiskUsagePrivate(pFileSize,pUserId,pPortletId);
+ } catch (Exception e) {
+ return false;
+ }
+ }
+
+ /**
+ * This method take the size of file and substract it to the portlet disk usage and total disk usage.
+ * Can't call this method directly
+ * @throws Exception
+ * @throws NamingException
+ */
+ private boolean substractDiskUsagePrivate(long pFileSize, long pUserId, String pPortletId) throws Exception{
+ DiskUsageUser userDiskUsage = diskUsageUserPersistence.fetchByuserId(pUserId);
+
+ if ( userDiskUsage == null)
+ {
+ long newId = CounterLocalServiceUtil.increment(DiskUsageUser.class.getName());;
+ userDiskUsage = diskUsageUserLocalService.createDiskUsageUser(newId);
+ userDiskUsage.setUserId(pUserId);
+ long organizationId = OrganizationFinderServiceUtil.getEtabRatachement(UserLocalServiceUtil.getUser(pUserId)).getOrganizationId();
+ userDiskUsage.setEtabId(organizationId);
+ diskUsageUserLocalService.addDiskUsageUser(userDiskUsage);
+ }
+
+ if ( pPortletId.equals(PortletConstant.BLOG_PORTLET_ID) ) {
+ long blogDiskUsage = userDiskUsage.getBlogSize() - pFileSize;
+ userDiskUsage.setBlogSize(blogDiskUsage);
+ }
+ else if ( pPortletId.equals(PortletConstant.MESSAGERIE_PORTLET_ID) ) {
+ long mailDiskUsage = userDiskUsage.getMailSize() - pFileSize;
+ userDiskUsage.setMailSize(mailDiskUsage);
+ }
+ else if ( pPortletId.equals(PortletConstant.CARTABLE_PORTLET_ID) || pPortletId.equals(PortletConstant.CASIER_CARTABLE_PORTLET_ID)) {
+ long cartableDiskUsage = userDiskUsage.getCartableSize() - pFileSize;
+ userDiskUsage.setCartableSize(cartableDiskUsage);
+ }
+ else{
+ return false;
+ }
+
+ long totalDiskUsage = userDiskUsage.getGlobalSize() - pFileSize;
+ userDiskUsage.setGlobalSize(totalDiskUsage);
+
+ diskUsageUserLocalService.updateDiskUsageUser(userDiskUsage);
+ return true;
+ }
+
+ /**
+ * This method update all the documentary size for suers for a company, it skips the users which throw exceptions
+ * @param pCompanyId long which represent the company in database
+ * @return boolean true, return needed for liferay services
+ */
+ public boolean updateAllUsersQuotasFromDocuments(){
+ List<User> usersAlreadyConnected = UsersUtilsLocalServiceUtil.getAllUserAlreadyConnected();
+
+ if(usersAlreadyConnected == null || usersAlreadyConnected.size() == 0){
+ return true;
+ }
+ long totalTime = System.currentTimeMillis();
+ for(User aUser: usersAlreadyConnected){
+ long time = System.currentTimeMillis();
+ try {
+ //calcul new cartable size , get size of cartable folder
+ long cartableNodeId = CasierEntryLocalServiceUtil.getRootFolderCartable(aUser.getUserId(), aUser.getGroupId(), true);
+ long cartableSize = 0;
+ if(cartableNodeId != 0){
+ cartableSize = DiskUsageUserLocalServiceUtil.getSubfileSizeInFolder(cartableNodeId);
+ }
+
+ long sendingBoxCartableSize = getSizeCartableSendingBoxForUser(aUser);
+ long totalCartableDisckUsage = cartableSize + sendingBoxCartableSize;
+
+ //Update database, if null use the existing method in the case disck usage don't exist
+ DiskUsageUser userDiskUsage = diskUsageUserPersistence.fetchByuserId(aUser.getUserId());
+ if ( userDiskUsage == null)
+ {
+ addDiskUsage(totalCartableDisckUsage, aUser.getUserId(), PortletConstant.CARTABLE_PORTLET_ID);
+ }
+ else{
+ userDiskUsage.setCartableSize(totalCartableDisckUsage);
+ userDiskUsage.setGlobalSize(totalCartableDisckUsage + userDiskUsage.getBlogSize() + userDiskUsage.getMailSize());
+ diskUsageUserLocalService.updateDiskUsageUser(userDiskUsage);
+ }
+ logger.debug ("User with id " + aUser.getUserId() + " use " + totalCartableDisckUsage
+ + " bytes for document disk usage. Calculation last:" + (System.currentTimeMillis() - time) + "ms");
+ } catch (Exception e) {
+ logger.error(e);
+ }
+ }
+ logger.debug("Total time for users document disk usage calculation " + (System.currentTimeMillis() - totalTime) +"ms");
+ return true;
+ }
+
+ /**
+ * Return the size of folder with it's sub folder
+ * @param pFolder Folder object it will get the size
+ * @return Long, the size of folder in paramter
+ */
+ public Long getSubfileSizeInFolder(long pFolderId) {
+ return DiskUsageUserFinderUtil.getSubfileSizeInFolder(pFolderId);
+ }
+
+ /**
+ * It calcul the size of user's sending box for cartable
+ * @param pUser User object. Calcul the size of cartable sending box of this user
+ * @return Long object which represent the file size of sending box for cartable portlet
+ */
+ public Long getSizeCartableSendingBoxForUser(User pUser) {
+ return DiskUsageUserFinderUtil.getSizeCartableSendingBoxForUser(pUser);
+ }
+}
\ No newline at end of file