Search in sources :

Example 1 with Document

use of com.ableneo.liferay.portal.setup.domain.Document in project liferay-db-setup-core by ableneo.

the class SetupDocuments method setupSiteDocuments.

public static void setupSiteDocuments(final Site site, long groupId) {
    for (Document doc : site.getDocument()) {
        String folderPath = doc.getDocumentFolderName();
        String documentName = doc.getDocumentFilename();
        String documentTitle = doc.getDocumentTitle();
        String filenameInFilesystem = doc.getFileSystemName();
        long userId = SetupConfigurationThreadLocal.getRunAsUserId();
        long company = SetupConfigurationThreadLocal.getRunInCompanyId();
        switch(doc.getFileUploadType()) {
            case GENERAL:
                // groupid == site group (as-is)
                break;
            case LOGO_IMAGE:
                // groupid = 'Control Panel' groups id..
                try {
                    Group controlPanelGroup = GroupLocalServiceUtil.loadGetGroup(company, GroupConstants.CONTROL_PANEL);
                    groupId = controlPanelGroup.getGroupId();
                } catch (PortalException e) {
                    LOG.error("Can not get group for " + GroupConstants.CONTROL_PANEL + " and company " + company + ":", e);
                    return;
                }
                break;
            default:
                LOG.error("Can not understand enum value '" + doc.getFileUploadType() + "' as upload-type.. check dependencies, implement on need!");
                return;
        }
        long repoId = groupId;
        FileEntry fe = DocumentUtil.findDocument(documentName, folderPath, groupId, groupId);
        byte[] fileBytes = null;
        try {
            fileBytes = ResourcesUtil.getFileBytes(filenameInFilesystem);
        } catch (IOException e) {
            LOG.error(String.format("Can not read file: %1$s. Skipping file", filenameInFilesystem));
            continue;
        }
        if (fileBytes != null) {
            if (fe == null) {
                Folder folder = null;
                if (Validator.isBlank(folderPath)) {
                    folder = FolderUtil.findFolder(groupId, repoId, folderPath, true);
                } else {
                    try {
                        folder = DLAppLocalServiceUtil.getMountFolder(repoId);
                    } catch (PortalException e) {
                        LOG.warn("Mount folder not found for file [{}], stopped creating the document.", documentName, e);
                    }
                }
                if (folder != null) {
                    LOG.info("{} is not found! It will be created! (c: {},grp: {}", documentName, company, groupId);
                    fe = DocumentUtil.createDocument(groupId, folder.getFolderId(), documentName, documentTitle, userId, repoId, fileBytes);
                }
            } else {
                LOG.info(documentName + " is found! Content will be updated! (c:" + company + ",grp:" + groupId + " ");
                DocumentUtil.updateFile(fe, fileBytes, userId, documentName);
            }
            SetupPermissions.updatePermission(String.format("Document %1$s/%2$s", folderPath, documentName), company, fe.getFileEntryId(), DLFileEntry.class, doc.getRolePermissions(), DEFAULT_PERMISSIONS);
        }
    }
}
Also used : Group(com.liferay.portal.kernel.model.Group) DLFileEntry(com.liferay.document.library.kernel.model.DLFileEntry) FileEntry(com.liferay.portal.kernel.repository.model.FileEntry) PortalException(com.liferay.portal.kernel.exception.PortalException) IOException(java.io.IOException) Document(com.ableneo.liferay.portal.setup.domain.Document) Folder(com.liferay.portal.kernel.repository.model.Folder)

Aggregations

Document (com.ableneo.liferay.portal.setup.domain.Document)1 DLFileEntry (com.liferay.document.library.kernel.model.DLFileEntry)1 PortalException (com.liferay.portal.kernel.exception.PortalException)1 Group (com.liferay.portal.kernel.model.Group)1 FileEntry (com.liferay.portal.kernel.repository.model.FileEntry)1 Folder (com.liferay.portal.kernel.repository.model.Folder)1 IOException (java.io.IOException)1