--- /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.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.dao.orm.QueryUtil;
+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.Group;
+import com.liferay.portal.model.Organization;
+import com.liferay.portal.model.User;
+import com.liferay.portal.service.GroupLocalServiceUtil;
+import com.liferay.portal.service.OrganizationLocalServiceUtil;
+import com.liferay.portal.service.UserLocalServiceUtil;
+import com.liferay.portlet.documentlibrary.model.DLFileEntry;
+import com.pentila.entSavoie.cartable.service.CasierEntryLocalServiceUtil;
+import com.pentila.entSavoie.communityInfos.model.CommunityInfos;
+import com.pentila.entSavoie.communityInfos.service.LocalGroupesLocalServiceUtil;
+import com.pentila.entSavoie.directory.OrganizationFinderServiceUtil;
+import com.pentila.entSavoie.model.DiskUsageGroup;
+import com.pentila.entSavoie.service.DiskUsageUserLocalServiceUtil;
+import com.pentila.entSavoie.service.base.DiskUsageGroupLocalServiceBaseImpl;
+import com.pentila.entSavoie.utils.DLEntryUtil;
+import com.pentila.entSavoie.utils.PortletConstant;
+
+/**
+ * The implementation of the disk usage group 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.DiskUsageGroupLocalService} 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.DiskUsageGroupLocalServiceBaseImpl
+ * @see com.pentila.entSavoie.service.DiskUsageGroupLocalServiceUtil
+ */
+public class DiskUsageGroupLocalServiceImpl
+ extends DiskUsageGroupLocalServiceBaseImpl {
+ /*
+ * NOTE FOR DEVELOPERS:
+ *
+ * Never reference this interface directly. Always use {@link com.pentila.entSavoie.service.DiskUsageGroupLocalServiceUtil} to access the disk usage group local service.
+ */
+
+ /**
+ * Suffix of group name for the group which represent an organization = "LFR_ORGANIZATION"
+ */
+ public static String ORGANISATION_GROUP_SUFFIX = "LFR_ORGANIZATION";
+ public static int CI_STATUT_DELETED = 3;
+ /*
+ * 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(DiskUsageGroupLocalServiceImpl.class);
+
+ /**
+ * Get the institutionnal groups Quota for user in an etab
+ * @param pOrganization the object representing the organization
+ * @return Map with group id as key and Disk usage in MB as value
+ * @throws PortalException
+ * @throws SystemException
+ */
+ public HashMap<Long, Long> getGroupsQuotasOnEtab(Long pOrganizationId) throws PortalException, SystemException{
+ // get all groups
+ Organization organization = OrganizationLocalServiceUtil.getOrganization(pOrganizationId);
+ List<Organization> allOrganizations = new ArrayList<Organization>();
+ allOrganizations.add(organization);
+ allOrganizations.addAll(organization.getSuborganizations());
+ List<Group> groups= GroupLocalServiceUtil.getOrganizationsGroups(allOrganizations);
+
+ HashMap<Long,Long> mapGroupIdQuota = new HashMap <Long,Long> ();
+
+ // get disk usage for groups (groupId column musn't be at 0)
+ List<DiskUsageGroup> diskUsages = new ArrayList<DiskUsageGroup>();
+ diskUsages.addAll(diskUsageGroupPersistence.findByetabId(pOrganizationId));
+
+ //verify the groupe
+ for(Group aGroup : groups){
+
+ int iterator = 0;
+ boolean found = false;
+ long sizeInMegabyte = 0;
+
+ while( (!found) && (iterator < diskUsages.size()) ){
+ if(diskUsages.get(iterator).getGroupId() == aGroup.getGroupId()){
+ found = true;
+ sizeInMegabyte = diskUsages.get(iterator).getGlobalSize()/MEGA_BYTE;
+ diskUsages.remove(iterator);
+ }
+ iterator++;
+ }
+ mapGroupIdQuota.put(aGroup.getGroupId(), sizeInMegabyte);
+ }
+ return mapGroupIdQuota;
+ }
+
+ /**
+ * Get the groups Quota for user in an etab
+ * @param pOrganization the object representing the organization
+ * @return Map with community (local group) id as key and Disk usage in MB as value
+ * @throws PortalException
+ * @throws SystemException
+ */
+ public HashMap<Long, Long> getCommunityQuotasOnEtab(Long pOrganizationId) throws PortalException, SystemException{
+ // get community and verify if the user has storage
+ List<CommunityInfos> communities = LocalGroupesLocalServiceUtil.getCommunityListFromEtabId(pOrganizationId);
+
+ HashMap<Long,Long> mapLocalGroupIdQuota = new HashMap <Long,Long> ();
+
+ // get disk usage for groups (groupId column musn't be at 0)
+ List<DiskUsageGroup> diskUsages = new ArrayList<DiskUsageGroup>();
+ diskUsages.addAll(diskUsageGroupPersistence.findByetabId(pOrganizationId));
+
+ //verify the groupe
+ for(CommunityInfos aCommunity : communities){
+ if(aCommunity.getStatus() != CI_STATUT_DELETED){
+ int iterator = 0;
+ boolean found = false;
+ long sizeInMegabyte = 0;
+
+ while( (!found) && (iterator < diskUsages.size()) ){
+ if(diskUsages.get(iterator).getGroupId() == aCommunity.getGroupId()){
+ found = true;
+ sizeInMegabyte = diskUsages.get(iterator).getGlobalSize()/MEGA_BYTE;
+ diskUsages.remove(iterator);
+ }
+ iterator++;
+ }
+ mapLocalGroupIdQuota.put(aCommunity.getGroupId(), sizeInMegabyte);
+ }
+ }
+ return mapLocalGroupIdQuota;
+ }
+
+ /**
+ * This method add the size of DLFileEntry to Disk usage of the group in parameter.
+ * @param pFile DLFileEntry which has uploaded by user.
+ * @param pGroupId The group 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 pGroupId, String pPortletId){
+ try{
+ return this.addDiskUsagePrivate(pFile.getSize(),pGroupId,pPortletId);
+ } catch (Exception e) {
+ return false;
+ }
+ }
+
+ /**
+ * This method add the size of FileEntry to Disk usage of the group in parameter.
+ * @param pFile DLFileEntry which has uploaded by group.
+ * @param pGroupId The group 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 pGroupId, String pPortletId){
+ try{
+ return this.addDiskUsagePrivate(pFile.getSize(),pGroupId,pPortletId);
+ } catch (Exception e) {
+ return false;
+ }
+ }
+
+ /**
+ * This method add the size of Folder and sub files to Disk usage of the group in parameter.
+ * @param pFolder Folder which has uploaded by group.
+ * @param pGroupId The group 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 pGroupId, String pPortletId){
+ try {
+ long folderSize = DLEntryUtil.getFolderTotalSize(pFolder.getFolderId());
+ return this.addDiskUsagePrivate(folderSize,pGroupId,pPortletId);
+ } catch (Exception e) {
+ return false;
+ }
+ }
+
+ /**
+ * This method add the size set in parameters to Disk usage of the group in parameter.
+ * @param pSize size to add from disk usage
+ * @param pGroupId The group 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 pSize, long pGroupId, String pPortletId){
+ try {
+ return this.addDiskUsagePrivate(pSize,pGroupId,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 SystemException
+ */
+ public boolean addDiskUsagePrivate(long pFileSize, long pGroupId, String pPortletId) throws SystemException{
+ DiskUsageGroup groupDiskUsage = diskUsageGroupPersistence.fetchBygroupId(pGroupId);
+
+ if ( groupDiskUsage == null){
+ try {
+ // group information
+ Group aGroup = GroupLocalServiceUtil.getGroup(pGroupId);
+
+ // the etab ratach for a group is the etab rattach of the group creator
+ long organizationId = 0 ;
+ if(aGroup.getName().trim().endsWith(ORGANISATION_GROUP_SUFFIX)){
+ // create disk usage for group or organization
+ long newId = CounterLocalServiceUtil.increment(DiskUsageGroup.class.getName());
+ groupDiskUsage = diskUsageGroupLocalService.createDiskUsageGroup(newId);
+ groupDiskUsage.setGroupId(pGroupId);
+
+ // get the name of group and cut before the suffix.
+ String organizationName = aGroup.getName().substring(0, aGroup.getName().length() - ORGANISATION_GROUP_SUFFIX.length()).trim();
+ Organization organizationGroup = OrganizationLocalServiceUtil.getOrganization(aGroup.getCompanyId(), organizationName);
+ // if the parent of current orga have parent with organization id = 0, the current organization is an etab
+ while(organizationGroup.getParentOrganization() != null && organizationGroup.getParentOrganization().getParentOrganizationId() != 0){
+ organizationGroup = organizationGroup.getParentOrganization();
+ }
+ organizationId = organizationGroup.getOrganizationId();
+ }
+ else{
+ User creator = UserLocalServiceUtil.getUser(aGroup.getCreatorUserId());
+
+ // not a user group or organization
+ if(creator == null || creator.isDefaultUser()){
+ return true;
+ }
+
+ //in some error case the user is simple delete in LDAP an it's impossible to find the attach etab
+ Organization etabAttach = OrganizationFinderServiceUtil.getEtabRatachement(creator);
+ if(etabAttach == null){
+ return true;
+ }
+
+ // create disk usage for group or organization
+ long newId = CounterLocalServiceUtil.increment(DiskUsageGroup.class.getName());
+ groupDiskUsage = diskUsageGroupLocalService.createDiskUsageGroup(newId);
+ groupDiskUsage.setGroupId(pGroupId);
+
+ //Case community
+ // exception when it's already an etab
+ organizationId = OrganizationFinderServiceUtil.getEtabRatachement(creator).getOrganizationId();
+ }
+ groupDiskUsage.setEtabId(organizationId);
+
+ diskUsageGroupLocalService.addDiskUsageGroup(groupDiskUsage);
+ } catch (Exception e) {
+ logger.error(e);
+ return false;
+ }
+
+ }
+
+ if ( pPortletId.equals(PortletConstant.ACTUALITES_PORTLET_ID) ) {
+ long actuDiskUsage = groupDiskUsage.getActuSize() + pFileSize;
+ groupDiskUsage.setActuSize(actuDiskUsage);
+ }
+ else if ( pPortletId.equals(PortletConstant.FORUM_PORTLET_ID) ) {
+ long forumDiskUsage = groupDiskUsage.getForumSize() + pFileSize;
+ groupDiskUsage.setForumSize(forumDiskUsage);
+ }
+ else if ( pPortletId.equals(PortletConstant.CARTABLE_PORTLET_ID) ) {
+ long cartableDiskUsage = groupDiskUsage.getDocSize() + pFileSize;
+ groupDiskUsage.setDocSize(cartableDiskUsage);
+ }
+ else{
+ return false;
+ }
+
+ long totalDiskUsage = groupDiskUsage.getGlobalSize() + pFileSize;
+ groupDiskUsage.setGlobalSize(totalDiskUsage);
+
+ diskUsageGroupLocalService.updateDiskUsageGroup(groupDiskUsage);
+ return true;
+ }
+
+
+ /**
+ * This method substract the size of DLFileEntry to Disk usage of the user in parameter.
+ * @param pFile DLFileEntry removed by user.
+ * @param pGroupId 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 pGroupId, String pPortletId){
+ try{
+ return this.substractDiskUsagePrivate(pFile.getSize(),pGroupId,pPortletId);
+ } catch (Exception e) {
+ return false;
+ }
+ }
+
+ /**
+ * This method substract the size of FileEntry to Disk usage of the group in parameter.
+ * @param pFile DLFileEntry removed by group.
+ * @param pGroupId 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 pGroupId, String pPortletId){
+ try{
+ return this.substractDiskUsagePrivate(pFile.getSize(),pGroupId,pPortletId);
+ } catch (Exception e) {
+ return false;
+ }
+ }
+
+ /**
+ * This method substract the size of Folder and subfile to Disk usage of the group in parameter.
+ * @param pFolder Folder removed by group.
+ * @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 pGroupId, String pPortletId){
+ try {
+ long folderSize = DLEntryUtil.getFolderTotalSize(pFolder.getFolderId());
+ return this.substractDiskUsagePrivate(folderSize,pGroupId,pPortletId);
+ } catch (Exception e) {
+ return false;
+ }
+ }
+
+ /**
+ * This method substract the size in parameter to Disk usage of the group in parameter.
+ * @param pSize size to remove from disk usage
+ * @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 pSize, long pGroupId, String pPortletId){
+ try {
+ return this.substractDiskUsagePrivate(pSize,pGroupId,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 pGroupId, String pPortletId) throws Exception{
+ DiskUsageGroup groupDiskUsage = diskUsageGroupPersistence.fetchBygroupId(pGroupId);
+
+ if ( groupDiskUsage == null)
+ {
+ long newId = CounterLocalServiceUtil.increment(DiskUsageGroup.class.getName());;
+ groupDiskUsage = diskUsageGroupLocalService.createDiskUsageGroup(newId);
+ groupDiskUsage.setGroupId(pGroupId);
+
+ // the etab ratach for a group is the etab rattach of the group creator
+ Group aGroup;
+ long organizationId = 0 ;
+ try {
+ aGroup = GroupLocalServiceUtil.getGroup(pGroupId);
+ if(aGroup.getName().trim().endsWith(ORGANISATION_GROUP_SUFFIX)){
+ // get the name of group and cut before the suffix.
+ String organizationName = aGroup.getName().substring(0, aGroup.getName().length() - ORGANISATION_GROUP_SUFFIX.length()).trim();
+ Organization organizationGroup = OrganizationLocalServiceUtil.getOrganization(aGroup.getCompanyId(), organizationName);
+ // if the parent of current orga have parent with organization id = 0, the current organization is an etab
+ while(organizationGroup.getParentOrganization().getParentOrganizationId() != 0){
+ organizationGroup = organizationGroup.getParentOrganization();
+ }
+ organizationId = organizationGroup.getOrganizationId();
+ }
+ else{
+ //Case community
+ long creatorID = aGroup.getCreatorUserId();
+ // exception when it's already an etab
+ organizationId = OrganizationFinderServiceUtil.getEtabRatachement(UserLocalServiceUtil.getUser(creatorID)).getOrganizationId();
+ }
+
+ } catch (Exception e) {
+ return false;
+ }
+ groupDiskUsage.setEtabId(organizationId);
+
+ diskUsageGroupLocalService.addDiskUsageGroup(groupDiskUsage);
+ }
+
+ if ( pPortletId.equals(PortletConstant.ACTUALITES_PORTLET_ID) ) {
+ long actuDiskUsage = groupDiskUsage.getActuSize() - pFileSize;
+ groupDiskUsage.setActuSize(actuDiskUsage);
+ }
+ else if ( pPortletId.equals(PortletConstant.FORUM_PORTLET_ID) ) {
+ long forumDiskUsage = groupDiskUsage.getForumSize() - pFileSize;
+ groupDiskUsage.setForumSize(forumDiskUsage);
+ }
+ else if ( pPortletId.equals(PortletConstant.CARTABLE_PORTLET_ID) ) {
+ long cartableDiskUsage = groupDiskUsage.getDocSize() - pFileSize;
+ groupDiskUsage.setDocSize(cartableDiskUsage);
+ }
+ else{
+ return false;
+ }
+
+ long totalDiskUsage = groupDiskUsage.getGlobalSize() - pFileSize;
+ groupDiskUsage.setGlobalSize(totalDiskUsage);
+
+ diskUsageGroupLocalService.updateDiskUsageGroup(groupDiskUsage);
+ return true;
+ }
+
+ /**
+ * This method update all the docSize for groups for a company
+ * @param pCompanyId long which represent the company in database
+ * @return boolean true if no major exception, false otherwise
+ */
+ public boolean updateAllGroupCartableDiskUsageForCompany(long pCompanyId){
+ try {
+ List<Group> groupsList = GroupLocalServiceUtil.getCompanyGroups(pCompanyId,QueryUtil.ALL_POS,QueryUtil.ALL_POS);
+ long totalTime = System.currentTimeMillis();
+ for(Group aGroup:groupsList){
+ long time = System.currentTimeMillis();
+ if(aGroup.getSite()){
+ long cartableNodeId;
+ //calcul new cartable size , get size of cartable folder
+ cartableNodeId = CasierEntryLocalServiceUtil.getRootFolderCartable(aGroup.getCreatorUserId(), aGroup.getGroupId(), false);
+
+ long cartableSize = 0;
+ if(cartableNodeId != 0){
+ cartableSize = DiskUsageUserLocalServiceUtil.getSubfileSizeInFolder(cartableNodeId);
+ }
+ //Update database, if null use the existing method in the case disk usage don't exist
+ DiskUsageGroup groupDiskUsage = diskUsageGroupPersistence.fetchBygroupId(aGroup.getGroupId());
+ if ( groupDiskUsage == null)
+ {
+ addDiskUsage(cartableSize, aGroup.getGroupId(), PortletConstant.CARTABLE_PORTLET_ID);
+ }
+ else{
+ groupDiskUsage.setDocSize(cartableSize);
+ groupDiskUsage.setGlobalSize(cartableSize + groupDiskUsage.getActuSize() + groupDiskUsage.getForumSize());
+ diskUsageGroupLocalService.updateDiskUsageGroup(groupDiskUsage);
+ }
+ logger.debug("Group with id " + aGroup.getGroupId() + " use " + cartableSize
+ + " bytes for document disk usage. Calculation last:" + (System.currentTimeMillis() - time ));
+ }
+ }
+ logger.debug("Total time for group document disk usage calculation " + (System.currentTimeMillis() - totalTime) +"ms");
+ } catch (SystemException e) {
+ logger.error(e);
+ return false;
+ }
+ return true;
+ }
+}
+