use of org.structr.core.app.App in project structr by structr.
the class DirectFileImportCommand method execute.
@Override
public void execute(final Map<String, Object> attributes) throws FrameworkException {
indexer = StructrApp.getInstance(securityContext).getFulltextIndexer();
final String sourcePath = getParameterValueAsString(attributes, "source", null);
final String modeString = getParameterValueAsString(attributes, "mode", Mode.COPY.name()).toUpperCase();
final String existingString = getParameterValueAsString(attributes, "existing", Existing.SKIP.name()).toUpperCase();
final boolean doIndex = Boolean.parseBoolean(getParameterValueAsString(attributes, "index", Boolean.TRUE.toString()));
if (StringUtils.isBlank(sourcePath)) {
throw new FrameworkException(422, "Please provide 'source' attribute for deployment source directory path.");
}
if (!EnumUtils.isValidEnum(Mode.class, modeString)) {
throw new FrameworkException(422, "Unknown value for 'mode' attribute. Valid values are: copy, move");
}
if (!EnumUtils.isValidEnum(Existing.class, existingString)) {
throw new FrameworkException(422, "Unknown value for 'existing' attribute. Valid values are: skip, overwrite, rename");
}
// use actual enums
final Existing existing = Existing.valueOf(existingString);
final Mode mode = Mode.valueOf(modeString);
final List<Path> paths = new ArrayList<>();
if (sourcePath.contains(PathHelper.PATH_SEP)) {
final String folderPart = PathHelper.getFolderPath(sourcePath);
final String namePart = PathHelper.getName(sourcePath);
if (StringUtils.isNotBlank(folderPart)) {
final Path source = Paths.get(folderPart);
if (!Files.exists(source)) {
throw new FrameworkException(422, "Source path " + sourcePath + " does not exist.");
}
if (!Files.isDirectory(source)) {
throw new FrameworkException(422, "Source path " + sourcePath + " is not a directory.");
}
try {
try (final DirectoryStream<Path> stream = Files.newDirectoryStream(source, namePart)) {
for (final Path entry : stream) {
paths.add(entry);
}
} catch (final DirectoryIteratorException ex) {
throw ex.getCause();
}
} catch (final IOException ioex) {
throw new FrameworkException(422, "Unable to parse source path " + sourcePath + ".");
}
}
} else {
// Relative path
final Path source = Paths.get(Settings.BasePath.getValue()).resolve(sourcePath);
if (!Files.exists(source)) {
throw new FrameworkException(422, "Source path " + sourcePath + " does not exist.");
}
paths.add(source);
}
final SecurityContext ctx = SecurityContext.getSuperUserInstance();
final App app = StructrApp.getInstance(ctx);
String targetPath = getParameterValueAsString(attributes, "target", "/");
Folder targetFolder = null;
ctx.setDoTransactionNotifications(false);
if (StringUtils.isNotBlank(targetPath) && !("/".equals(targetPath))) {
try (final Tx tx = app.tx()) {
targetFolder = app.nodeQuery(Folder.class).and(StructrApp.key(Folder.class, "path"), targetPath).getFirst();
if (targetFolder == null) {
throw new FrameworkException(422, "Target path " + targetPath + " does not exist.");
}
tx.success();
}
}
String msg = "Starting direct file import from source directory " + sourcePath + " into target path " + targetPath;
logger.info(msg);
publishProgressMessage(msg);
paths.forEach((path) -> {
try {
final String newTargetPath;
// If path is a directory, create it and use it as the new target folder
if (Files.isDirectory(path)) {
Path parentPath = path.getParent();
if (parentPath == null) {
parentPath = path;
}
createFileOrFolder(ctx, app, parentPath, path, Files.readAttributes(path, BasicFileAttributes.class), sourcePath, targetPath, mode, existing, doIndex);
newTargetPath = targetPath + PathHelper.PATH_SEP + PathHelper.clean(path.getFileName().toString());
} else {
newTargetPath = targetPath;
}
Files.walkFileTree(path, new FileVisitor<Path>() {
@Override
public FileVisitResult preVisitDirectory(final Path dir, final BasicFileAttributes attrs) throws IOException {
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFile(final Path file, final BasicFileAttributes attrs) throws IOException {
return createFileOrFolder(ctx, app, path, file, attrs, sourcePath, newTargetPath, mode, existing, doIndex);
}
@Override
public FileVisitResult visitFileFailed(final Path file, final IOException exc) throws IOException {
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult postVisitDirectory(final Path dir, final IOException exc) throws IOException {
return FileVisitResult.CONTINUE;
}
});
} catch (final IOException ex) {
logger.debug("Mode: " + modeString + ", path: " + sourcePath, ex);
}
});
msg = "Finished direct file import from source directory " + sourcePath + ". Imported " + folderCount + " folders and " + fileCount + " files.";
logger.info(msg);
publishProgressMessage(msg);
}
use of org.structr.core.app.App in project structr by structr.
the class DOMNode method cloneNode.
public static Node cloneNode(final DOMNode thisNode, boolean deep) {
final SecurityContext securityContext = thisNode.getSecurityContext();
if (deep) {
return cloneAndAppendChildren(securityContext, thisNode);
} else {
final PropertyMap properties = new PropertyMap();
for (Iterator<PropertyKey> it = thisNode.getPropertyKeys(PropertyView.Ui).iterator(); it.hasNext(); ) {
final PropertyKey key = it.next();
// skip blacklisted properties
if (cloneBlacklist.contains(key.jsonName())) {
continue;
}
if (!key.isUnvalidated()) {
properties.put(key, thisNode.getProperty(key));
}
}
// htmlView is necessary for the cloning of DOM nodes - otherwise some properties won't be cloned
for (Iterator<PropertyKey> it = thisNode.getPropertyKeys(PropertyView.Html).iterator(); it.hasNext(); ) {
final PropertyKey key = it.next();
// skip blacklisted properties
if (cloneBlacklist.contains(key.jsonName())) {
continue;
}
if (!key.isUnvalidated()) {
properties.put(key, thisNode.getProperty(key));
}
}
if (thisNode instanceof LinkSource) {
final LinkSource linkSourceElement = (LinkSource) thisNode;
properties.put(StructrApp.key(LinkSource.class, "linkable"), linkSourceElement.getLinkable());
}
final App app = StructrApp.getInstance(securityContext);
try {
return app.create(thisNode.getClass(), properties);
} catch (FrameworkException ex) {
ex.printStackTrace();
throw new DOMException(DOMException.INVALID_STATE_ERR, ex.toString());
}
}
}
use of org.structr.core.app.App in project structr by structr.
the class Page method createNewPage.
/**
* Creates a new Page entity with the given name in the database.
*
* @param securityContext the security context to use
* @param uuid the UUID of the new page or null
* @param name the name of the new page, defaults to "page"
* "ownerDocument" if not set
*
* @return the new ownerDocument
* @throws FrameworkException
*/
public static Page createNewPage(final SecurityContext securityContext, final String uuid, final String name) throws FrameworkException {
final PropertyKey<String> contentTypeKey = StructrApp.key(Page.class, "contentType");
final PropertyKey<Boolean> hideOnDetailKey = StructrApp.key(Page.class, "hideOnDetail");
final PropertyKey<Boolean> hideOnIndexKey = StructrApp.key(Page.class, "hideOnIndex");
final PropertyKey<Boolean> enableBasicAuthKey = StructrApp.key(Page.class, "enableBasicAuth");
final App app = StructrApp.getInstance(securityContext);
final PropertyMap properties = new PropertyMap();
// set default values for properties on creation to avoid them
// being set separately when indexing later
properties.put(AbstractNode.name, name != null ? name : "page");
properties.put(AbstractNode.type, Page.class.getSimpleName());
properties.put(contentTypeKey, "text/html");
properties.put(hideOnDetailKey, false);
properties.put(hideOnIndexKey, false);
properties.put(enableBasicAuthKey, false);
if (uuid != null) {
properties.put(Page.id, uuid);
}
return app.create(Page.class, properties);
}
use of org.structr.core.app.App in project structr by structr.
the class Page method createDocumentFragment.
public static DocumentFragment createDocumentFragment(final Page thisPage) {
final App app = StructrApp.getInstance(thisPage.getSecurityContext());
try {
// create new content element
org.structr.web.entity.dom.DocumentFragment fragment = app.create(org.structr.web.entity.dom.DocumentFragment.class);
fragment.setOwnerDocument(thisPage);
return fragment;
} catch (FrameworkException fex) {
// FIXME: what to do with the exception here?
logger.warn("", fex);
}
return null;
}
use of org.structr.core.app.App in project structr by structr.
the class PathProperty method getSearchAttribute.
@Override
public SearchAttribute getSearchAttribute(final SecurityContext securityContext, final Occurrence occur, final String searchValue, final boolean exactMatch, final Query query) {
final App app = StructrApp.getInstance(securityContext);
final SourceSearchAttribute attr = new SourceSearchAttribute(occur);
try {
final ArrayList<String> parts = new ArrayList<>(Arrays.asList(PathHelper.getParts(searchValue)));
if (!parts.isEmpty()) {
searchRecursively(app, null, attr, parts);
}
} catch (FrameworkException ex) {
logger.error("", ex);
}
return attr;
}
Aggregations