0001: /**
0002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
0003: *
0004: * Permission is hereby granted, free of charge, to any person obtaining a copy
0005: * of this software and associated documentation files (the "Software"), to deal
0006: * in the Software without restriction, including without limitation the rights
0007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
0008: * copies of the Software, and to permit persons to whom the Software is
0009: * furnished to do so, subject to the following conditions:
0010: *
0011: * The above copyright notice and this permission notice shall be included in
0012: * all copies or substantial portions of the Software.
0013: *
0014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
0015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
0016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
0017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
0018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
0019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
0020: * SOFTWARE.
0021: */package com.liferay.portlet.documentlibrary.service.impl;
0022:
0023: import com.liferay.documentlibrary.DuplicateFileException;
0024: import com.liferay.documentlibrary.FileSizeException;
0025: import com.liferay.documentlibrary.NoSuchFileException;
0026: import com.liferay.portal.PortalException;
0027: import com.liferay.portal.SystemException;
0028: import com.liferay.portal.kernel.util.GetterUtil;
0029: import com.liferay.portal.kernel.util.OrderByComparator;
0030: import com.liferay.portal.kernel.util.StringPool;
0031: import com.liferay.portal.kernel.util.Validator;
0032: import com.liferay.portal.model.User;
0033: import com.liferay.portal.model.impl.ResourceImpl;
0034: import com.liferay.portal.util.MimeTypesUtil;
0035: import com.liferay.portal.util.PortletKeys;
0036: import com.liferay.portal.util.PropsValues;
0037: import com.liferay.portlet.documentlibrary.DuplicateFolderNameException;
0038: import com.liferay.portlet.documentlibrary.NoSuchFileEntryException;
0039: import com.liferay.portlet.documentlibrary.NoSuchFolderException;
0040: import com.liferay.portlet.documentlibrary.model.DLFileEntry;
0041: import com.liferay.portlet.documentlibrary.model.DLFileVersion;
0042: import com.liferay.portlet.documentlibrary.model.DLFolder;
0043: import com.liferay.portlet.documentlibrary.model.impl.DLFileEntryImpl;
0044: import com.liferay.portlet.documentlibrary.model.impl.DLFolderImpl;
0045: import com.liferay.portlet.documentlibrary.service.base.DLFileEntryLocalServiceBaseImpl;
0046: import com.liferay.util.FileUtil;
0047: import com.liferay.util.MathUtil;
0048:
0049: import java.io.BufferedInputStream;
0050: import java.io.ByteArrayInputStream;
0051: import java.io.File;
0052: import java.io.FileInputStream;
0053: import java.io.FileNotFoundException;
0054: import java.io.IOException;
0055: import java.io.InputStream;
0056:
0057: import java.rmi.RemoteException;
0058:
0059: import java.util.ArrayList;
0060: import java.util.Date;
0061: import java.util.Iterator;
0062: import java.util.List;
0063:
0064: import org.apache.commons.logging.Log;
0065: import org.apache.commons.logging.LogFactory;
0066:
0067: /**
0068: * <a href="DLFileEntryLocalServiceImpl.java.html"><b><i>View Source</i></b></a>
0069: *
0070: * <p>
0071: * For DLFileEntries, the naming convention for some of the variables is not
0072: * very informative, due to legacy code. Each DLFileEntry has a corresponding
0073: * name and title. The "name" is a unique identifier for a given file and
0074: * usually follows the format "DLFE-1234.xls" whereas the "title" is the actual
0075: * name specified by the user (e.g., "Budget.xls").
0076: * </p>
0077: *
0078: * @author Brian Wing Shun Chan
0079: * @author Harry Mark
0080: *
0081: */
0082: public class DLFileEntryLocalServiceImpl extends
0083: DLFileEntryLocalServiceBaseImpl {
0084:
0085: public DLFileEntry addFileEntry(long userId, long folderId,
0086: String name, String title, String description,
0087: String[] tagsEntries, String extraSettings, File file,
0088: boolean addCommunityPermissions, boolean addGuestPermissions)
0089: throws PortalException, SystemException {
0090:
0091: return addFileEntry(userId, folderId, name, title, description,
0092: tagsEntries, extraSettings, file, Boolean
0093: .valueOf(addCommunityPermissions), Boolean
0094: .valueOf(addGuestPermissions), null, null);
0095: }
0096:
0097: public DLFileEntry addFileEntry(long userId, long folderId,
0098: String name, String title, String description,
0099: String[] tagsEntries, String extraSettings,
0100: byte[] byteArray, boolean addCommunityPermissions,
0101: boolean addGuestPermissions) throws PortalException,
0102: SystemException {
0103:
0104: return addFileEntry(null, userId, folderId, name, title,
0105: description, tagsEntries, extraSettings, byteArray,
0106: Boolean.valueOf(addCommunityPermissions), Boolean
0107: .valueOf(addGuestPermissions), null, null);
0108: }
0109:
0110: public DLFileEntry addFileEntry(String uuid, long userId,
0111: long folderId, String name, String title,
0112: String description, String[] tagsEntries,
0113: String extraSettings, byte[] byteArray,
0114: boolean addCommunityPermissions, boolean addGuestPermissions)
0115: throws PortalException, SystemException {
0116:
0117: return addFileEntry(uuid, userId, folderId, name, title,
0118: description, tagsEntries, extraSettings, byteArray,
0119: Boolean.valueOf(addCommunityPermissions), Boolean
0120: .valueOf(addGuestPermissions), null, null);
0121: }
0122:
0123: public DLFileEntry addFileEntry(long userId, long folderId,
0124: String name, String title, String description,
0125: String[] tagsEntries, String extraSettings, File file,
0126: String[] communityPermissions, String[] guestPermissions)
0127: throws PortalException, SystemException {
0128:
0129: return addFileEntry(userId, folderId, name, title, description,
0130: tagsEntries, extraSettings, file, null, null,
0131: communityPermissions, guestPermissions);
0132: }
0133:
0134: public DLFileEntry addFileEntry(long userId, long folderId,
0135: String name, String title, String description,
0136: String[] tagsEntries, String extraSettings,
0137: byte[] byteArray, String[] communityPermissions,
0138: String[] guestPermissions) throws PortalException,
0139: SystemException {
0140:
0141: return addFileEntry(null, userId, folderId, name, title,
0142: description, tagsEntries, extraSettings, byteArray,
0143: null, null, communityPermissions, guestPermissions);
0144: }
0145:
0146: public DLFileEntry addFileEntry(long userId, long folderId,
0147: String name, String title, String description,
0148: String[] tagsEntries, String extraSettings, File file,
0149: Boolean addCommunityPermissions,
0150: Boolean addGuestPermissions, String[] communityPermissions,
0151: String[] guestPermissions) throws PortalException,
0152: SystemException {
0153:
0154: if ((file == null) || (file.length() == 0)) {
0155: throw new FileSizeException();
0156: }
0157:
0158: InputStream is = null;
0159:
0160: try {
0161: is = new BufferedInputStream(new FileInputStream(file));
0162:
0163: return addFileEntry(null, userId, folderId, name, title,
0164: description, tagsEntries, extraSettings, is, file
0165: .length(), addCommunityPermissions,
0166: addGuestPermissions, communityPermissions,
0167: guestPermissions);
0168: } catch (FileNotFoundException fnfe) {
0169: throw new FileSizeException();
0170: } finally {
0171: try {
0172: if (is != null) {
0173: is.close();
0174: }
0175: } catch (IOException ioe) {
0176: _log.error(ioe);
0177: }
0178: }
0179: }
0180:
0181: public DLFileEntry addFileEntry(String uuid, long userId,
0182: long folderId, String name, String title,
0183: String description, String[] tagsEntries,
0184: String extraSettings, byte[] byteArray,
0185: Boolean addCommunityPermissions,
0186: Boolean addGuestPermissions, String[] communityPermissions,
0187: String[] guestPermissions) throws PortalException,
0188: SystemException {
0189:
0190: if ((byteArray == null) || (byteArray.length == 0)) {
0191: throw new FileSizeException();
0192: }
0193:
0194: InputStream is = new ByteArrayInputStream(byteArray);
0195:
0196: return addFileEntry(uuid, userId, folderId, name, title,
0197: description, tagsEntries, extraSettings, is,
0198: byteArray.length, addCommunityPermissions,
0199: addGuestPermissions, communityPermissions,
0200: guestPermissions);
0201: }
0202:
0203: public DLFileEntry addFileEntry(String uuid, long userId,
0204: long folderId, String name, String title,
0205: String description, String[] tagsEntries,
0206: String extraSettings, InputStream is, long size,
0207: Boolean addCommunityPermissions,
0208: Boolean addGuestPermissions, String[] communityPermissions,
0209: String[] guestPermissions) throws PortalException,
0210: SystemException {
0211:
0212: // File entry
0213:
0214: User user = userPersistence.findByPrimaryKey(userId);
0215: folderId = getFolderId(user.getCompanyId(), folderId);
0216: DLFolder folder = dlFolderPersistence
0217: .findByPrimaryKey(folderId);
0218: Date now = new Date();
0219:
0220: if (Validator.isNull(title)) {
0221: title = name;
0222: }
0223:
0224: name = getName(name);
0225: title = DLFileEntryImpl.stripExtension(name, title);
0226:
0227: validate(folder.getGroupId(), folderId, name, title, is);
0228:
0229: long fileEntryId = counterLocalService.increment();
0230:
0231: DLFileEntry fileEntry = dlFileEntryPersistence
0232: .create(fileEntryId);
0233:
0234: fileEntry.setUuid(uuid);
0235: fileEntry.setCompanyId(user.getCompanyId());
0236: fileEntry.setUserId(user.getUserId());
0237: fileEntry.setUserName(user.getFullName());
0238: fileEntry.setVersionUserId(user.getUserId());
0239: fileEntry.setVersionUserName(user.getFullName());
0240: fileEntry.setCreateDate(now);
0241: fileEntry.setModifiedDate(now);
0242: fileEntry.setFolderId(folderId);
0243: fileEntry.setName(name);
0244: fileEntry.setTitle(title);
0245: fileEntry.setDescription(description);
0246: fileEntry.setVersion(DLFileEntryImpl.DEFAULT_VERSION);
0247: fileEntry.setSize((int) size);
0248: fileEntry.setReadCount(DLFileEntryImpl.DEFAULT_READ_COUNT);
0249: fileEntry.setExtraSettings(extraSettings);
0250:
0251: dlFileEntryPersistence.update(fileEntry);
0252:
0253: // File
0254:
0255: dlLocalService.addFile(user.getCompanyId(),
0256: PortletKeys.DOCUMENT_LIBRARY, folder.getGroupId(),
0257: folderId, name, fileEntry.getLuceneProperties(),
0258: tagsEntries, is);
0259:
0260: // Resources
0261:
0262: if ((addCommunityPermissions != null)
0263: && (addGuestPermissions != null)) {
0264:
0265: addFileEntryResources(folder, fileEntry,
0266: addCommunityPermissions.booleanValue(),
0267: addGuestPermissions.booleanValue());
0268: } else {
0269: addFileEntryResources(folder, fileEntry,
0270: communityPermissions, guestPermissions);
0271: }
0272:
0273: // Tags
0274:
0275: updateTagsAsset(userId, fileEntry, tagsEntries);
0276:
0277: // Folder
0278:
0279: folder.setLastPostDate(fileEntry.getModifiedDate());
0280:
0281: dlFolderPersistence.update(folder);
0282:
0283: return fileEntry;
0284: }
0285:
0286: public void addFileEntryResources(long folderId, String name,
0287: boolean addCommunityPermissions, boolean addGuestPermissions)
0288: throws PortalException, SystemException {
0289:
0290: DLFolder folder = dlFolderPersistence
0291: .findByPrimaryKey(folderId);
0292: DLFileEntry fileEntry = dlFileEntryPersistence.findByF_N(
0293: folderId, name);
0294:
0295: addFileEntryResources(folder, fileEntry,
0296: addCommunityPermissions, addGuestPermissions);
0297: }
0298:
0299: public void addFileEntryResources(DLFolder folder,
0300: DLFileEntry fileEntry, boolean addCommunityPermissions,
0301: boolean addGuestPermissions) throws PortalException,
0302: SystemException {
0303:
0304: resourceLocalService.addResources(fileEntry.getCompanyId(),
0305: folder.getGroupId(), fileEntry.getUserId(),
0306: DLFileEntry.class.getName(),
0307: fileEntry.getFileEntryId(), false,
0308: addCommunityPermissions, addGuestPermissions);
0309: }
0310:
0311: public void addFileEntryResources(long folderId, String name,
0312: String[] communityPermissions, String[] guestPermissions)
0313: throws PortalException, SystemException {
0314:
0315: DLFolder folder = dlFolderPersistence
0316: .findByPrimaryKey(folderId);
0317: DLFileEntry fileEntry = dlFileEntryPersistence.findByF_N(
0318: folderId, name);
0319:
0320: addFileEntryResources(folder, fileEntry, communityPermissions,
0321: guestPermissions);
0322: }
0323:
0324: public void addFileEntryResources(DLFolder folder,
0325: DLFileEntry fileEntry, String[] communityPermissions,
0326: String[] guestPermissions) throws PortalException,
0327: SystemException {
0328:
0329: resourceLocalService.addModelResources(
0330: fileEntry.getCompanyId(), folder.getGroupId(),
0331: fileEntry.getUserId(), DLFileEntry.class.getName(),
0332: fileEntry.getFileEntryId(), communityPermissions,
0333: guestPermissions);
0334: }
0335:
0336: public DLFileEntry addOrOverwriteFileEntry(long userId,
0337: long folderId, String name, String sourceName,
0338: String title, String description, String[] tagsEntries,
0339: String extraSettings, File file,
0340: boolean addCommunityPermissions, boolean addGuestPermissions)
0341: throws PortalException, SystemException {
0342:
0343: boolean update = false;
0344:
0345: String extension = FileUtil.getExtension(name);
0346:
0347: Iterator itr = dlFileEntryPersistence
0348: .findByF_T(folderId, title).iterator();
0349:
0350: while (itr.hasNext()) {
0351: DLFileEntry fileEntry = (DLFileEntry) itr.next();
0352:
0353: String curExtension = FileUtil.getExtension(fileEntry
0354: .getName());
0355:
0356: if (PropsValues.WEBDAV_LITMUS
0357: && Validator.isNull(extension)) {
0358: if (Validator.isNull(curExtension)) {
0359: update = true;
0360:
0361: name = fileEntry.getName();
0362:
0363: break;
0364: }
0365: } else if (extension.equals(curExtension)) {
0366: update = true;
0367:
0368: break;
0369: }
0370: }
0371:
0372: if (update) {
0373: return updateFileEntry(userId, folderId, folderId, name,
0374: sourceName, title, description, tagsEntries,
0375: extraSettings, file);
0376: } else {
0377: return addFileEntry(userId, folderId, name, title,
0378: description, tagsEntries, extraSettings, file,
0379: addCommunityPermissions, addGuestPermissions);
0380: }
0381: }
0382:
0383: public void deleteFileEntries(long folderId)
0384: throws PortalException, SystemException {
0385:
0386: Iterator itr = dlFileEntryPersistence.findByFolderId(folderId)
0387: .iterator();
0388:
0389: while (itr.hasNext()) {
0390: DLFileEntry fileEntry = (DLFileEntry) itr.next();
0391:
0392: deleteFileEntry(fileEntry);
0393: }
0394: }
0395:
0396: public void deleteFileEntry(long folderId, String name)
0397: throws PortalException, SystemException {
0398:
0399: deleteFileEntry(folderId, name, -1);
0400: }
0401:
0402: public void deleteFileEntry(long folderId, String name,
0403: double version) throws PortalException, SystemException {
0404:
0405: DLFileEntry fileEntry = dlFileEntryPersistence.findByF_N(
0406: folderId, name);
0407:
0408: if (version > 0) {
0409: try {
0410: dlService.deleteFile(fileEntry.getCompanyId(),
0411: PortletKeys.DOCUMENT_LIBRARY, fileEntry
0412: .getFolderId(), fileEntry.getName(),
0413: version);
0414: } catch (Exception e) {
0415: if (_log.isWarnEnabled()) {
0416: _log.warn(e, e);
0417: }
0418: }
0419:
0420: dlFileVersionPersistence.removeByF_N_V(folderId, name,
0421: version);
0422: } else {
0423: deleteFileEntry(fileEntry);
0424: }
0425: }
0426:
0427: public void deleteFileEntry(DLFileEntry fileEntry)
0428: throws PortalException, SystemException {
0429:
0430: // File
0431:
0432: try {
0433: dlService.deleteFile(fileEntry.getCompanyId(),
0434: PortletKeys.DOCUMENT_LIBRARY, fileEntry
0435: .getFolderId(), fileEntry.getName());
0436: } catch (Exception e) {
0437: if (_log.isWarnEnabled()) {
0438: _log.warn(e, e);
0439: }
0440: }
0441:
0442: // File ranks
0443:
0444: dlFileRankLocalService.deleteFileRanks(fileEntry.getFolderId(),
0445: fileEntry.getName());
0446:
0447: // File shortcuts
0448:
0449: dlFileShortcutLocalService.deleteFileShortcuts(fileEntry
0450: .getFolderId(), fileEntry.getName());
0451:
0452: // File versions
0453:
0454: Iterator itr = dlFileVersionPersistence.findByF_N(
0455: fileEntry.getFolderId(), fileEntry.getName())
0456: .iterator();
0457:
0458: while (itr.hasNext()) {
0459: DLFileVersion fileVersion = (DLFileVersion) itr.next();
0460:
0461: dlFileVersionPersistence
0462: .remove(fileVersion.getPrimaryKey());
0463: }
0464:
0465: // Tags
0466:
0467: tagsAssetLocalService.deleteAsset(DLFileEntry.class.getName(),
0468: fileEntry.getFileEntryId());
0469:
0470: // Ratings
0471:
0472: ratingsStatsLocalService.deleteStats(DLFileEntry.class
0473: .getName(), fileEntry.getFileEntryId());
0474:
0475: // Message boards
0476:
0477: mbMessageLocalService
0478: .deleteDiscussionMessages(DLFileEntry.class.getName(),
0479: fileEntry.getFileEntryId());
0480:
0481: // Resources
0482:
0483: resourceLocalService.deleteResource(fileEntry.getCompanyId(),
0484: DLFileEntry.class.getName(),
0485: ResourceImpl.SCOPE_INDIVIDUAL, fileEntry
0486: .getFileEntryId());
0487:
0488: // WebDAVProps
0489:
0490: webDAVPropsLocalService.deleteWebDAVProps(DLFileEntry.class
0491: .getName(), fileEntry.getPrimaryKey());
0492:
0493: // File entry
0494:
0495: dlFileEntryPersistence.remove(fileEntry.getPrimaryKey());
0496: }
0497:
0498: public List getCompanyFileEntries(long companyId, int begin, int end)
0499: throws SystemException {
0500:
0501: return dlFileEntryPersistence.findByCompanyId(companyId, begin,
0502: end);
0503: }
0504:
0505: public List getCompanyFileEntries(long companyId, int begin,
0506: int end, OrderByComparator obc) throws SystemException {
0507:
0508: return dlFileEntryPersistence.findByCompanyId(companyId, begin,
0509: end, obc);
0510: }
0511:
0512: public int getCompanyFileEntriesCount(long companyId)
0513: throws SystemException {
0514:
0515: return dlFileEntryPersistence.countByCompanyId(companyId);
0516: }
0517:
0518: public InputStream getFileAsStream(long companyId, long userId,
0519: long folderId, String name) throws PortalException,
0520: SystemException {
0521:
0522: return getFileAsStream(companyId, userId, folderId, name, 0);
0523: }
0524:
0525: public InputStream getFileAsStream(long companyId, long userId,
0526: long folderId, String name, double version)
0527: throws PortalException, SystemException {
0528:
0529: if (userId > 0) {
0530: DLFolder folder = dlFolderPersistence
0531: .findByPrimaryKey(folderId);
0532:
0533: dlFileRankLocalService.updateFileRank(folder.getGroupId(),
0534: companyId, userId, folderId, name);
0535: }
0536:
0537: DLFileEntry fileEntry = dlFileEntryPersistence.findByF_N(
0538: folderId, name);
0539:
0540: fileEntry.setReadCount(fileEntry.getReadCount() + 1);
0541:
0542: dlFileEntryPersistence.update(fileEntry);
0543:
0544: if ((version > 0) && (fileEntry.getVersion() != version)) {
0545: return dlLocalService.getFileAsStream(companyId, folderId,
0546: name, version);
0547: } else {
0548: return dlLocalService.getFileAsStream(companyId, folderId,
0549: name);
0550: }
0551: }
0552:
0553: public List getFileEntries(long folderId) throws SystemException {
0554: return dlFileEntryPersistence.findByFolderId(folderId);
0555: }
0556:
0557: public List getFileEntries(long folderId, int begin, int end)
0558: throws SystemException {
0559:
0560: return dlFileEntryPersistence.findByFolderId(folderId, begin,
0561: end);
0562: }
0563:
0564: public List getFileEntries(long folderId, int begin, int end,
0565: OrderByComparator obc) throws SystemException {
0566:
0567: return dlFileEntryPersistence.findByFolderId(folderId, begin,
0568: end, obc);
0569: }
0570:
0571: public List getFileEntriesAndShortcuts(long folderId, int begin,
0572: int end) throws SystemException {
0573:
0574: List folderIds = new ArrayList();
0575:
0576: folderIds.add(new Long(folderId));
0577:
0578: return dlFileEntryAndShortcutFinder.findByFolderIds(folderIds,
0579: begin, end);
0580: }
0581:
0582: public List getFileEntriesAndShortcuts(List folderIds, int begin,
0583: int end) throws SystemException {
0584:
0585: return dlFileEntryAndShortcutFinder.findByFolderIds(folderIds,
0586: begin, end);
0587: }
0588:
0589: public int getFileEntriesAndShortcutsCount(long folderId)
0590: throws SystemException {
0591:
0592: List folderIds = new ArrayList();
0593:
0594: folderIds.add(new Long(folderId));
0595:
0596: return dlFileEntryAndShortcutFinder.countByFolderIds(folderIds);
0597: }
0598:
0599: public int getFileEntriesAndShortcutsCount(List folderIds)
0600: throws SystemException {
0601:
0602: return dlFileEntryAndShortcutFinder.countByFolderIds(folderIds);
0603: }
0604:
0605: public int getFileEntriesCount(long folderId)
0606: throws SystemException {
0607: return dlFileEntryPersistence.countByFolderId(folderId);
0608: }
0609:
0610: public DLFileEntry getFileEntry(long fileEntryId)
0611: throws PortalException, SystemException {
0612:
0613: return dlFileEntryPersistence.findByPrimaryKey(fileEntryId);
0614: }
0615:
0616: public DLFileEntry getFileEntry(long folderId, String name)
0617: throws PortalException, SystemException {
0618:
0619: return dlFileEntryPersistence.findByF_N(folderId, name);
0620: }
0621:
0622: public DLFileEntry getFileEntryByUuidAndGroupId(String uuid,
0623: long groupId) throws PortalException, SystemException {
0624:
0625: return dlFileEntryFinder.findByUuid_G(uuid, groupId);
0626: }
0627:
0628: public DLFileEntry getFileEntryByTitle(long folderId,
0629: String titleWithExtension) throws PortalException,
0630: SystemException {
0631:
0632: String title = DLFileEntryImpl.stripExtension(
0633: titleWithExtension, titleWithExtension);
0634: String extension = FileUtil.getExtension(titleWithExtension);
0635:
0636: Iterator itr = dlFileEntryPersistence
0637: .findByF_T(folderId, title).iterator();
0638:
0639: while (itr.hasNext()) {
0640: DLFileEntry fileEntry = (DLFileEntry) itr.next();
0641:
0642: String curExtension = FileUtil.getExtension(fileEntry
0643: .getName());
0644:
0645: if (PropsValues.WEBDAV_LITMUS
0646: && Validator.isNull(extension)) {
0647: if (Validator.isNull(curExtension)) {
0648: return fileEntry;
0649: }
0650: } else if (extension.equals(curExtension)) {
0651: return fileEntry;
0652: }
0653: }
0654:
0655: throw new NoSuchFileEntryException();
0656: }
0657:
0658: public int getFoldersFileEntriesCount(List folderIds)
0659: throws SystemException {
0660:
0661: return dlFileEntryFinder.countByFolderIds(folderIds);
0662: }
0663:
0664: public List getGroupFileEntries(long groupId, int begin, int end)
0665: throws SystemException {
0666:
0667: return dlFileEntryFinder.findByGroupId(groupId, begin, end);
0668: }
0669:
0670: public List getGroupFileEntries(long groupId, int begin, int end,
0671: OrderByComparator obc) throws SystemException {
0672:
0673: return dlFileEntryFinder
0674: .findByGroupId(groupId, begin, end, obc);
0675: }
0676:
0677: public List getGroupFileEntries(long groupId, long userId,
0678: int begin, int end) throws SystemException {
0679:
0680: if (userId <= 0) {
0681: return dlFileEntryFinder.findByGroupId(groupId, begin, end);
0682: } else {
0683: return dlFileEntryFinder.findByG_U(groupId, userId, begin,
0684: end);
0685: }
0686: }
0687:
0688: public List getGroupFileEntries(long groupId, long userId,
0689: int begin, int end, OrderByComparator obc)
0690: throws SystemException {
0691:
0692: if (userId <= 0) {
0693: return dlFileEntryFinder.findByGroupId(groupId, begin, end,
0694: obc);
0695: } else {
0696: return dlFileEntryFinder.findByG_U(groupId, userId, begin,
0697: end, obc);
0698: }
0699: }
0700:
0701: public int getGroupFileEntriesCount(long groupId)
0702: throws SystemException {
0703: return dlFileEntryFinder.countByGroupId(groupId);
0704: }
0705:
0706: public int getGroupFileEntriesCount(long groupId, long userId)
0707: throws SystemException {
0708:
0709: if (userId <= 0) {
0710: return dlFileEntryFinder.countByGroupId(groupId);
0711: } else {
0712: return dlFileEntryFinder.countByG_U(groupId, userId);
0713: }
0714: }
0715:
0716: public List getNoAssetFileEntries() throws SystemException {
0717: return dlFileEntryFinder.findByNoAssets();
0718: }
0719:
0720: public DLFileEntry updateFileEntry(long userId, long folderId,
0721: long newFolderId, String name, String sourceFileName,
0722: String title, String description, String[] tagsEntries,
0723: String extraSettings, File file) throws PortalException,
0724: SystemException {
0725:
0726: InputStream is = null;
0727:
0728: try {
0729: long size = 0;
0730:
0731: if ((file != null) && (file.length() > 0)) {
0732: is = new BufferedInputStream(new FileInputStream(file));
0733: size = file.length();
0734: }
0735:
0736: return updateFileEntry(userId, folderId, newFolderId, name,
0737: sourceFileName, title, description, tagsEntries,
0738: extraSettings, is, size);
0739: } catch (FileNotFoundException fnfe) {
0740: throw new NoSuchFileException();
0741: } finally {
0742: try {
0743: if (is != null) {
0744: is.close();
0745: }
0746: } catch (IOException ioe) {
0747: _log.error(ioe);
0748: }
0749: }
0750: }
0751:
0752: public DLFileEntry updateFileEntry(long userId, long folderId,
0753: long newFolderId, String name, String sourceFileName,
0754: String title, String description, String[] tagsEntries,
0755: String extraSettings, byte[] byteArray)
0756: throws PortalException, SystemException {
0757:
0758: InputStream is = null;
0759: long size = 0;
0760:
0761: if ((byteArray != null) && (byteArray.length > 0)) {
0762: is = new ByteArrayInputStream(byteArray);
0763: size = byteArray.length;
0764: }
0765:
0766: return updateFileEntry(userId, folderId, newFolderId, name,
0767: sourceFileName, title, description, tagsEntries,
0768: extraSettings, is, size);
0769: }
0770:
0771: public DLFileEntry updateFileEntry(long userId, long folderId,
0772: long newFolderId, String name, String sourceFileName,
0773: String title, String description, String[] tagsEntries,
0774: String extraSettings, InputStream is, long size)
0775: throws PortalException, SystemException {
0776:
0777: // File entry
0778:
0779: User user = userPersistence.findByPrimaryKey(userId);
0780: DLFolder folder = dlFolderPersistence
0781: .findByPrimaryKey(folderId);
0782:
0783: if (Validator.isNull(title)) {
0784: title = sourceFileName;
0785:
0786: if (Validator.isNull(title)) {
0787: title = name;
0788: }
0789: }
0790:
0791: title = DLFileEntryImpl.stripExtension(name, title);
0792:
0793: validate(folder.getGroupId(), folderId, newFolderId, name,
0794: title, sourceFileName, is);
0795:
0796: DLFileEntry fileEntry = dlFileEntryPersistence.findByF_N(
0797: folderId, name);
0798:
0799: fileEntry.setTitle(title);
0800: fileEntry.setDescription(description);
0801: fileEntry.setExtraSettings(extraSettings);
0802:
0803: dlFileEntryPersistence.update(fileEntry);
0804:
0805: // Move file entry
0806:
0807: if ((newFolderId > 0) && (folderId != newFolderId)) {
0808: DLFolder newFolder = dlFolderPersistence
0809: .findByPrimaryKey(newFolderId);
0810:
0811: if (folder.getGroupId() != newFolder.getGroupId()) {
0812: throw new NoSuchFolderException();
0813: }
0814:
0815: if (dlLocalService.hasFile(user.getCompanyId(),
0816: newFolderId, name, 0)) {
0817:
0818: throw new DuplicateFileException(name);
0819: }
0820:
0821: long newFileEntryId = counterLocalService.increment();
0822:
0823: DLFileEntry newFileEntry = dlFileEntryPersistence
0824: .create(newFileEntryId);
0825:
0826: newFileEntry.setCompanyId(fileEntry.getCompanyId());
0827: newFileEntry.setUserId(fileEntry.getUserId());
0828: newFileEntry.setUserName(fileEntry.getUserName());
0829: newFileEntry.setVersionUserId(fileEntry.getVersionUserId());
0830: newFileEntry.setVersionUserName(fileEntry
0831: .getVersionUserName());
0832: newFileEntry.setCreateDate(fileEntry.getCreateDate());
0833: newFileEntry.setModifiedDate(fileEntry.getModifiedDate());
0834: newFileEntry.setFolderId(newFolderId);
0835: newFileEntry.setName(name);
0836: newFileEntry.setTitle(fileEntry.getTitle());
0837: newFileEntry.setDescription(fileEntry.getDescription());
0838: newFileEntry.setVersion(fileEntry.getVersion());
0839: newFileEntry.setSize(fileEntry.getSize());
0840: newFileEntry.setReadCount(fileEntry.getReadCount());
0841: newFileEntry.setExtraSettings(extraSettings);
0842:
0843: dlFileEntryPersistence.update(newFileEntry);
0844:
0845: dlFileEntryPersistence.remove(fileEntry);
0846:
0847: fileEntry = newFileEntry;
0848:
0849: Iterator itr = dlFileVersionPersistence.findByF_N(folderId,
0850: name).iterator();
0851:
0852: while (itr.hasNext()) {
0853: DLFileVersion fileVersion = (DLFileVersion) itr.next();
0854:
0855: long newFileVersionId = counterLocalService.increment();
0856:
0857: DLFileVersion newFileVersion = dlFileVersionPersistence
0858: .create(newFileVersionId);
0859:
0860: newFileVersion.setCompanyId(fileVersion.getCompanyId());
0861: newFileVersion.setUserId(fileVersion.getUserId());
0862: newFileVersion.setUserName(fileVersion.getUserName());
0863: newFileVersion.setCreateDate(fileVersion
0864: .getCreateDate());
0865: newFileVersion.setFolderId(newFolderId);
0866: newFileVersion.setName(name);
0867: newFileVersion.setVersion(fileVersion.getVersion());
0868: newFileVersion.setSize(fileVersion.getSize());
0869:
0870: dlFileVersionPersistence.update(newFileVersion);
0871:
0872: dlFileVersionPersistence.remove(fileVersion);
0873: }
0874:
0875: dlFileShortcutLocalService.updateFileShortcuts(folderId,
0876: name, newFolderId, name);
0877:
0878: try {
0879: dlService.updateFile(user.getCompanyId(),
0880: PortletKeys.DOCUMENT_LIBRARY, folder
0881: .getGroupId(), folderId, newFolderId,
0882: name);
0883: } catch (RemoteException re) {
0884: throw new SystemException(re);
0885: }
0886:
0887: folderId = newFolderId;
0888: folder = newFolder;
0889: }
0890:
0891: // Tags
0892:
0893: updateTagsAsset(userId, fileEntry, tagsEntries);
0894:
0895: // File version
0896:
0897: if (is == null) {
0898: return fileEntry;
0899: }
0900:
0901: double oldVersion = fileEntry.getVersion();
0902: double newVersion = MathUtil.format(oldVersion + 0.1, 1, 1);
0903:
0904: long fileVersionId = counterLocalService.increment();
0905:
0906: DLFileVersion fileVersion = dlFileVersionPersistence
0907: .create(fileVersionId);
0908:
0909: long versionUserId = fileEntry.getVersionUserId();
0910:
0911: if (versionUserId <= 0) {
0912: versionUserId = fileEntry.getUserId();
0913: }
0914:
0915: String versionUserName = GetterUtil.getString(fileEntry
0916: .getVersionUserName(), fileEntry.getUserName());
0917:
0918: fileVersion.setCompanyId(fileEntry.getCompanyId());
0919: fileVersion.setUserId(versionUserId);
0920: fileVersion.setUserName(versionUserName);
0921: fileVersion.setCreateDate(fileEntry.getModifiedDate());
0922: fileVersion.setFolderId(folderId);
0923: fileVersion.setName(name);
0924: fileVersion.setVersion(oldVersion);
0925: fileVersion.setSize(fileEntry.getSize());
0926:
0927: dlFileVersionPersistence.update(fileVersion);
0928:
0929: // File entry
0930:
0931: fileEntry.setVersionUserId(user.getUserId());
0932: fileEntry.setVersionUserName(user.getFullName());
0933: fileEntry.setModifiedDate(new Date());
0934: fileEntry.setVersion(newVersion);
0935: fileEntry.setSize((int) size);
0936:
0937: dlFileEntryPersistence.update(fileEntry);
0938:
0939: // File
0940:
0941: dlLocalService.updateFile(user.getCompanyId(),
0942: PortletKeys.DOCUMENT_LIBRARY, folder.getGroupId(),
0943: folderId, name, newVersion, sourceFileName, fileEntry
0944: .getLuceneProperties(), tagsEntries, is);
0945:
0946: // Folder
0947:
0948: folder.setLastPostDate(fileEntry.getModifiedDate());
0949:
0950: dlFolderPersistence.update(folder);
0951:
0952: return fileEntry;
0953: }
0954:
0955: public void updateTagsAsset(long userId, DLFileEntry fileEntry,
0956: String[] tagsEntries) throws PortalException,
0957: SystemException {
0958:
0959: String mimeType = MimeTypesUtil.getContentType(fileEntry
0960: .getName());
0961:
0962: tagsAssetLocalService.updateAsset(userId, fileEntry.getFolder()
0963: .getGroupId(), DLFileEntry.class.getName(), fileEntry
0964: .getFileEntryId(), tagsEntries, null, null, null, null,
0965: mimeType, fileEntry.getTitle(), fileEntry
0966: .getDescription(), null, null, 0, 0, null,
0967: false);
0968: }
0969:
0970: protected long getFolderId(long companyId, long folderId)
0971: throws PortalException, SystemException {
0972:
0973: if (folderId != DLFolderImpl.DEFAULT_PARENT_FOLDER_ID) {
0974:
0975: // Ensure folder exists and belongs to the proper company
0976:
0977: try {
0978: DLFolder folder = dlFolderPersistence
0979: .findByPrimaryKey(folderId);
0980:
0981: if (companyId != folder.getCompanyId()) {
0982: folderId = DLFolderImpl.DEFAULT_PARENT_FOLDER_ID;
0983: }
0984: } catch (NoSuchFolderException nsfe) {
0985: folderId = DLFolderImpl.DEFAULT_PARENT_FOLDER_ID;
0986: }
0987: }
0988:
0989: return folderId;
0990: }
0991:
0992: protected String getName(String name) throws SystemException {
0993: String extension = StringPool.BLANK;
0994:
0995: int pos = name.lastIndexOf(StringPool.PERIOD);
0996:
0997: if (pos != -1) {
0998: extension = name.substring(pos + 1, name.length())
0999: .toLowerCase();
1000: }
1001:
1002: name = String.valueOf(counterLocalService
1003: .increment(DLFileEntry.class.getName()));
1004:
1005: if (Validator.isNotNull(extension)) {
1006: name = "DLFE-" + name + StringPool.PERIOD + extension;
1007: }
1008:
1009: return name;
1010: }
1011:
1012: protected void validate(long groupId, long folderId,
1013: long newFolderId, String name, String title,
1014: String sourceFileName, InputStream is)
1015: throws PortalException, SystemException {
1016:
1017: if (Validator.isNotNull(sourceFileName)) {
1018: dlLocalService.validate(name, sourceFileName, is);
1019: }
1020:
1021: if (newFolderId > 0 && (folderId != newFolderId)) {
1022: folderId = newFolderId;
1023: }
1024:
1025: String extension = FileUtil.getExtension(name);
1026:
1027: try {
1028: String titleWithException = title;
1029:
1030: if (Validator.isNotNull(extension)) {
1031: title += extension;
1032: }
1033:
1034: dlFolderLocalService.getFolder(groupId, newFolderId,
1035: titleWithException);
1036:
1037: throw new DuplicateFolderNameException();
1038: } catch (NoSuchFolderException nsfe) {
1039: }
1040:
1041: Iterator itr = dlFileEntryPersistence
1042: .findByF_T(folderId, title).iterator();
1043:
1044: while (itr.hasNext()) {
1045: DLFileEntry fileEntry = (DLFileEntry) itr.next();
1046:
1047: if (!name.equals(fileEntry.getName())) {
1048: String curExtension = FileUtil.getExtension(fileEntry
1049: .getName());
1050:
1051: if (PropsValues.WEBDAV_LITMUS
1052: && Validator.isNull(extension)) {
1053: if (Validator.isNull(curExtension)) {
1054: throw new DuplicateFileException(fileEntry
1055: .getTitleWithExtension());
1056: }
1057: } else if (extension.equals(curExtension)) {
1058: throw new DuplicateFileException(fileEntry
1059: .getTitleWithExtension());
1060: }
1061: }
1062: }
1063: }
1064:
1065: protected void validate(long groupId, long folderId, String name,
1066: String title, InputStream is) throws PortalException,
1067: SystemException {
1068:
1069: dlLocalService.validate(name, is);
1070:
1071: String extension = FileUtil.getExtension(name);
1072:
1073: try {
1074: String titleWithException = title;
1075:
1076: if (Validator.isNotNull(extension)) {
1077: title += extension;
1078: }
1079:
1080: dlFolderLocalService.getFolder(groupId, folderId,
1081: titleWithException);
1082:
1083: throw new DuplicateFolderNameException();
1084: } catch (NoSuchFolderException nsfe) {
1085: }
1086:
1087: Iterator itr = dlFileEntryPersistence
1088: .findByF_T(folderId, title).iterator();
1089:
1090: while (itr.hasNext()) {
1091: DLFileEntry fileEntry = (DLFileEntry) itr.next();
1092:
1093: String curExtension = FileUtil.getExtension(fileEntry
1094: .getName());
1095:
1096: if (PropsValues.WEBDAV_LITMUS
1097: && Validator.isNull(extension)) {
1098: if (Validator.isNull(curExtension)) {
1099: throw new DuplicateFileException(fileEntry
1100: .getTitleWithExtension());
1101: }
1102: } else if (extension.equals(curExtension)) {
1103: throw new DuplicateFileException(fileEntry
1104: .getTitleWithExtension());
1105: }
1106: }
1107: }
1108:
1109: private static Log _log = LogFactory
1110: .getLog(DLFileEntryLocalServiceImpl.class);
1111:
1112: }
|