Monday, March 25, 2013

Liferay Save File to Liferay CMS

Not the best method but it works:

public void saveFileToCMS(ActionRequest aReq, long groupId, String fileName, File filenameWithPath) {
        try {
            ServiceContext serviceContext = ServiceContextFactory.getInstance(
                    Group.class.getName(), aReq);

            // prevents duplicate entries based on unique title name
            Random rand = new Random();
            Integer suffix = new Integer(rand.nextInt(10000));

            // actual method which does inserting, poor liferay documentation on this
            DLAppServiceUtil.addFileEntry(groupId, 0, fileName, "application/vnd.ms-excel",
                    fileName + suffix.toString(), "description goes here", "changelogname",
                    filenameWithPath, serviceContext);
            log.info("Successfully added the new file");

        } catch (PortalException pe) {
            log.error("Portal Exception occurred while saving file to CMS");
            pe.printStackTrace();
        } catch (SystemException e) {
            log.error("System Exception occurred while saving file to CMS");
            e.printStackTrace();
        }
    }

No comments:

Post a Comment