Saturday, July 18, 2015

Folder and File upload Programmatically in Document and Media Portlet of Liferay


In the previous tutorial we discuss how to add Folder and Documents in liferay by using GUI.Today we will discuss how to add Folder and Documents/Files in Document and Media Portlet by using API methods provided by liferay.

For this we have two set of classes:-
1)DL Services
2)DLApp Services

Any of the classes can used to achieve folder creation, file upload etc.But:-

DLFileEntry services and DLFolderEntry services are specifically for storing file and folder entries in liferay's database and are totally unaware of the new repository concept introduced in 6.1.

Where as DLApp (DLAppService & DLAppLocalService) services take into account these things i.e. to say that they take care of syncing documents between liferay database and other repositories,and not just store entries in Liferay database.

Here i am discussing both .Before reading this blog it is highly recommended to read my previous blog Document and Media portlet in liferay.

So lets start this step by step:-





Step 1:-Create Liferay Project and Portlet
Create a Liferay Project in eclipse and then create a MVC portlet in the Project.For theses you may refer Step 1 and Step 2 of my previous blog Here .

Step 2:-Create Folder in Document and Media
Open you java Class and paste these methods:-

public void createFolder(RenderRequest renderRequest,ThemeDisplay themeDisplay,String name,String description)
{
long repositoryId = themeDisplay.getScopeGroupId();//repository id is same as groupId
long parentFolderId = DLFolderConstants.DEFAULT_PARENT_FOLDER_ID; // or 0
try {
ServiceContext serviceContext = ServiceContextFactory.getInstance(DLFolder.class.getName(), renderRequest);
Folder folder = DLAppServiceUtil.addFolder(repositoryId, parentFolderId, name, description, serviceContext);
} catch (PortalException e1) {
e1.printStackTrace();
} catch (SystemException e1) {
e1.printStackTrace();
}
}
public void createDLFolder(RenderRequest renderRequest,ThemeDisplay themeDisplay, String folderName,String description)
{
long userId = themeDisplay.getUserId();
long groupId = themeDisplay.getScopeGroupId();
long repositoryId = themeDisplay.getScopeGroupId();//repository id is same as groupId
boolean mountPoint = false; // mountPoint which is a boolean specifying whether the folder is a facade for mounting a third-party repository
long parentFolderId = DLFolderConstants.DEFAULT_PARENT_FOLDER_ID; // or 0
boolean hidden = false; // true if you want to hidden the folder
try {
ServiceContext serviceContext = ServiceContextFactory.getInstance(DLFolder.class.getName(), renderRequest);
DLFolderLocalServiceUtil.addFolder(userId, groupId, repositoryId, mountPoint, parentFolderId, folderName, description, hidden, serviceContext);
} catch (PortalException e1) {
e1.printStackTrace();
} catch (SystemException e1) {
e1.printStackTrace();
}
}
view raw Folder.java hosted with ❤ by GitHub

Explanation:-

1)As you can see createfolder() use DLAppServiceUtil and createDLFolder() use DLFolderLocalServiceUtil.
2)In createDLFolder method we have boolean mountPoint.
Run the method two times with both mountPoint values.



See the location of folder where they create.

Step 3:-Add Files to the Folder
Add the two methods and use any of these:-

public void fileUploadByDL(String folderName ,ThemeDisplay themeDisplay,RenderRequest renderRequest)
{
File file = new File("D:\\JLR\\jag2.png");
System.out.println("Exist=>"+file.exists());
long userId = themeDisplay.getUserId();
long groupId = themeDisplay.getScopeGroupId();
long repositoryId = themeDisplay.getScopeGroupId();
String mimeType = MimeTypesUtil.getContentType(file);
String title = file.getName();
String description = "This file is added via programatically";
String changeLog = "hi";
try
{
DLFolder dlFolder = DLFolderLocalServiceUtil.getFolder(themeDisplay.getScopeGroupId(), 0, folderName);
long fileEntryTypeId = dlFolder.getDefaultFileEntryTypeId();
ServiceContext serviceContext = ServiceContextFactory.getInstance(DLFileEntry.class.getName(), renderRequest);
InputStream is = new FileInputStream( file );
DLFileEntry dlFileEntry =DLFileEntryLocalServiceUtil.addFileEntry(userId, groupId, repositoryId, dlFolder.getFolderId(), file.getName(), mimeType, title, description, changeLog, fileEntryTypeId, null, file, is,
file.getTotalSpace(), serviceContext);
//Change mode of Draft to Approved
DLFileEntryLocalServiceUtil.updateFileEntry(userId, dlFileEntry.getFileEntryId(), file.getName(),
MimeTypesUtil.getContentType(file), title, description, "Draft to save", true,
dlFileEntry.getFileEntryTypeId(), null, file, null, file.getTotalSpace(), serviceContext);
} catch (Exception e)
{
System.out.println("Exception");
e.printStackTrace();
}
}
public void fileUploadByApp(String folderName ,ThemeDisplay themeDisplay,RenderRequest renderRequest)
{
File file = new File("D:\\JLR\\jag5.png");
System.out.println("Exist=>"+file.exists());
long repositoryId = themeDisplay.getScopeGroupId();
String mimeType = MimeTypesUtil.getContentType(file);
String title = file.getName();
String description = "This file is added via programatically";
String changeLog = "hi";
Long parentFolderId = DLFolderConstants.DEFAULT_PARENT_FOLDER_ID;
try
{
Folder folder =DLAppServiceUtil.getFolder(themeDisplay.getScopeGroupId(), parentFolderId, folderName);
ServiceContext serviceContext = ServiceContextFactory.getInstance(DLFileEntry.class.getName(), renderRequest);
InputStream is = new FileInputStream( file );
DLAppServiceUtil.addFileEntry(repositoryId, folder.getFolderId(), file.getName(), mimeType,
title, description, changeLog, is, file.getTotalSpace(), serviceContext);
} catch (Exception e)
{
System.out.println("Exception");
e.printStackTrace();
}
}
view raw AddFile.java hosted with ❤ by GitHub

Explanation:-

1)Here in fileUploadByDL method we use addFileEntry method to add the file in the folder but here file is save in draft mode so we use updateFileEntry method to change the mode from draft to approved.
2)Here if you click on image and message appears like "File is too large for preview or thumbnail generation" just go to portal-ext.properties add this line and restart the server:-

dl.file.entry.previewable.processor.max.size=-1

So thumbnail is generated for any size of file.






Step 4:-Get All Folders
Paste theses two methods.Here you see the difference between DL and DLApp methods:-

public void getAllDLFolder()
{
try {
List<DLFolder> dlFolders = DLFolderLocalServiceUtil.getDLFolders(0,DLFolderLocalServiceUtil.getDLFoldersCount());
for (DLFolder folder : dlFolders) {
System.out.println("DLFolder Id >> " + folder.getFolderId());
System.out.println("DLFolder Name >>" + folder.getName());
}
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("-----------------------Finish-----------------------------------");
}
//DLApp deals with folder not DLFolders
//This will provide more specific results associated with repository Id
public void getAllFolder(ThemeDisplay themeDisplay){
long repositoryId = themeDisplay.getScopeGroupId();
try {
List<Folder> folders = DLAppServiceUtil.getFolders(repositoryId, DLFolderConstants.DEFAULT_PARENT_FOLDER_ID);
for (Folder folder : folders) {
System.out.println("Folder Id >> "+folder.getFolderId());
System.out.println("Folder Name >>"+folder.getName());
}
} catch (Exception e) {
e.printStackTrace();
}
}

Explanation:-


1)Output by getAllDLFolder()

DLFolder Id >> 12304
DLFolder Name >>/12709
DLFolder Id >> 12505
DLFolder Name >>/12714
DLFolder Id >> 12333
DLFolder Name >>/12719
DLFolder Id >> 12401
DLFolder Name >>/12729
DLFolder Id >> 12702
DLFolder Name >>/12734
DLFolder Id >> 12766
DLFolder Name >>Liferayiseasy
DLFolder Id >> 12763
DLFolder Name >>MountPointFalse
DLFolder Id >> 12760
DLFolder Name >>MountPointTrue


2)Output by getAllFolder()

Folder Id >> 12766
Folder Name >>Liferayiseasy
Folder Id >> 12763
Folder Name >>MountPointFalse
Folder Id >> 12760
Folder Name >>MountPointTrue

Here you can see that DLAppServiceUtil give more specific results however DLFolderLocalServiceUtil give all the folders which are even deleted or in recycle bin.

Step 5:-Get a Particular Folder
Paste theses two methods and just provide the Folder name:-

public void getDLFolder(String folderName,ThemeDisplay themeDisplay)
{
long groupId = themeDisplay.getScopeGroupId();
Long parentFolderId = DLFolderConstants.DEFAULT_PARENT_FOLDER_ID;
try {
DLFolder dir = DLFolderLocalServiceUtil.getFolder(groupId, parentFolderId, folderName);
System.out.println("Folder Id==>"+dir.getFolderId());
} catch (Exception e) {
e.printStackTrace();
}
}
public void getFolder(String folderName,ThemeDisplay themeDisplay)
{
Long parentFolderId = DLFolderConstants.DEFAULT_PARENT_FOLDER_ID;
try {
Folder dir =DLAppServiceUtil.getFolder(themeDisplay.getScopeGroupId(), parentFolderId, folderName);
System.out.println("Folder Id==>"+dir.getFolderId());
} catch (Exception e) {
e.printStackTrace();
}
}
view raw GetFolder.java hosted with ❤ by GitHub


Step 6:-Get download link for files in Folder
Paste this code and pass the folder name to the methods:-

view raw GetLink.java hosted with ❤ by GitHub


In the next blog How to Upload file in Liferay we create a simple application that can browse the documents and upload in document and media portlet and we can download those documents by clicking on the links.

You can Download Source code from Folder and File upload Programmatically in Document and Media Portlet of Liferay

Hope this will Help....

No comments:

Post a Comment

Total Pageviews

1042521

Number Of Unique Visitor

Free counters!