Skip to content

Instantly share code, notes, and snippets.

@dnebing
Last active December 6, 2018 23:20
Show Gist options
  • Save dnebing/9c0c5463a9a36b8a94e474fef2cd01e3 to your computer and use it in GitHub Desktop.
Save dnebing/9c0c5463a9a36b8a94e474fef2cd01e3 to your computer and use it in GitHub Desktop.
Touch script for both basic and advanced filesystem stores.
import com.liferay.portal.kernel.service.GroupLocalServiceUtil
import com.liferay.portal.kernel.util.PortalUtil
import com.liferay.document.library.kernel.exception.NoSuchFileException
import com.liferay.document.library.kernel.service.DLFileEntryLocalServiceUtil;
import com.liferay.document.library.kernel.service.DLFolderLocalServiceUtil;
import com.liferay.document.library.kernel.model.DLFileEntry;
import com.liferay.document.library.kernel.model.DLFolder;
import com.liferay.portal.kernel.util.StackTraceUtil
import java.io.File
import java.io.FileOutputStream
long count = 0;
try {
boolean breakOnFirstError = false;
boolean readOnly = true;
boolean usingAdvancedFilesystemStore = true;
// String siteName = "Nathan test" // default site
String siteName = "HRB Intranet" // default site
long companyId = PortalUtil.getDefaultCompanyId();
long groupId = GroupLocalServiceUtil.getGroup(companyId, siteName).getGroupId()
// need the root file object, start from the list of files at the root...
List<DLFileEntry> list = DLFileEntryLocalServiceUtil.getGroupFileEntries(groupId, 0, 0, -1, -1, null);
if (! list.isEmpty()) {
// get the file for from the entity, will use this to find the root folder
File root = DLFileEntryLocalServiceUtil.getFile(list.get(0).getFileEntryId(), list.get(0).getVersion(), false);
if (usingAdvancedFilesystemStore) {
// go up 4 files to get the root folder
root = root.getParentFile().getParentFile().getParentFile().getParentFile();
root = root.getParentFile().getParentFile();
} else {
// TODO: Need to figure out how to get the root from the given File.
}
println "Root: " + root.getAbsolutePath()
list = DLFileEntryLocalServiceUtil.getGroupFileEntries(groupId, 0, 0, -1, -1, null);
println String.format("Found %s DLFileEntry(s) in group %s with groupId %s to check...", list.size(), siteName, groupId);
File target
for (DLFileEntry entry : list) {
long fileEntryId = entry.getFileEntryId();
String version = entry.getVersion();
try {
// DLFileEntryLocalServiceUtil.getFileAsStream(fileEntryId, version, false);
target = DLFileEntryLocalServiceUtil.getFile(fileEntryId, version, false);
if ((target == null) || (! target.exists())) {
count ++;
String path;
if (usingAdvancedFilesystemStore) {
path = String.format("/%s/%s", entry.companyId, entry.repositoryId);
// now need to tack on the possible subdirs...
int pathLen = entry.name.length();
if (pathLen > 2) {
String nameDup = entry.name;
// need to create subdirs
for (int idx = 0; idx < 2; idx++) {
if (nameDup.length() < 2) {
break;
}
// need a substring addition...
path = String.format("%s/%s", path, nameDup.substring(0,2));
nameDup = nameDup.substring(2);
}
}
// now need to add the rest of the path stuff
path = String.format("%s/%s.afsh/%s_%s.afsh", path, entry.name, entry.name, version);
} else {
path = String.format("/%s/%s/%s/%s", entry.companyId, entry.repositoryId, entry.name, version )
}
String msg = String.format("dlFileEntryId [%s], version %s, title [%s], folderName [%s], path %s", fileEntryId, version, entry.getTitle(), entry.getFolder().getName(), path);
println "File missing: " + msg
println "Full path: " + target.getAbsolutePath();
if (! readOnly) {
// make the parent directories
target.getParentFile().mkdirs();
// next create the actual file.
new FileOutputStream(target).close();
}
}
} catch (Exception e) {
count ++;
String path;
if (usingAdvancedFilesystemStore) {
path = String.format("/%s/%s", entry.companyId, entry.repositoryId);
// now need to tack on the possible subdirs...
int pathLen = entry.name.length();
if (pathLen > 2) {
String nameDup = entry.name;
// need to create subdirs
for (int idx = 0; idx < 2; idx++) {
if (nameDup.length() < 2) {
break;
}
// need a substring addition...
path = String.format("%s/%s", path, nameDup.substring(0,2));
nameDup = nameDup.substring(2);
}
}
// now need to add the rest of the path stuff
path = String.format("%s/%s.afsh/%s_%s.afsh", path, entry.name, entry.name, version);
} else {
path = String.format("/%s/%s/%s/%s", entry.companyId, entry.repositoryId, entry.name, version )
}
String msg = String.format("dlFileEntryId [%s], version %s, title [%s], folderName [%s], path %s", fileEntryId, version, entry.getTitle(), entry.getFolder().getName(), path);
if (! e instanceof NoSuchFileException) {
println "Unable to retrieve file" + msg + " - " + e
} else {
println "File missing: " + msg
target = new File(root, path);
println "Full path: " + target.getAbsolutePath();
if (! readOnly) {
// make the parent directories
target.getParentFile().mkdirs();
// next create the actual file.
new FileOutputStream(target).close();
}
}
if (breakOnFirstError) {
break;
}
continue;
}
}
}
} catch (Exception e) {
println "Script failed: " + e.toString();
println StackTraceUtil.getStackTrace(e);
}
println String.format("Found %s missing documents.", count);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment