use of com.haulmont.cuba.core.entity.Folder in project cuba by cuba-platform.
the class FoldersServiceBean method loadAppFolders.
@Override
public List<AppFolder> loadAppFolders() {
log.debug("Loading AppFolders");
StopWatch stopWatch = new Slf4JStopWatch("AppFolders");
stopWatch.start();
List<AppFolder> resultList;
try (Transaction tx = persistence.createTransaction()) {
String metaClassName = metadata.getExtendedEntities().getEffectiveMetaClass(AppFolder.class).getName();
TypedQuery<AppFolder> q = persistence.getEntityManager().createQuery("select f from " + metaClassName + " f order by f.sortOrder, f.name", AppFolder.class);
resultList = q.getResultList();
// fetch parent folder
resultList.forEach(Folder::getParent);
tx.commit();
} finally {
stopWatch.stop();
}
if (CollectionUtils.isNotEmpty(resultList)) {
Binding binding = new Binding();
binding.setVariable("persistence", persistence);
binding.setVariable("metadata", metadata);
binding.setVariable("userSession", userSessionSource.getUserSession());
Iterator<AppFolder> iterator = resultList.iterator();
while (iterator.hasNext()) {
AppFolder folder = iterator.next();
try (Transaction tx = persistence.createTransaction()) {
boolean evaluatedVisibilityScript = true;
try {
if (!StringUtils.isBlank(folder.getVisibilityScript())) {
binding.setVariable("folder", folder);
Boolean visible = runScript(folder.getVisibilityScript(), binding);
if (BooleanUtils.isFalse(visible)) {
iterator.remove();
continue;
}
}
} catch (Exception e) {
log.warn("Unable to evaluate AppFolder visibility script for folder: id: {} name: {}", folder.getId(), folder.getName(), e);
// because EclipseLink Query marks transaction as rollback-only on JPQL syntax errors
evaluatedVisibilityScript = false;
}
boolean evaluatedQuantityScript = loadFolderQuantity(binding, folder);
if (evaluatedVisibilityScript && evaluatedQuantityScript) {
tx.commit();
}
}
}
}
return resultList;
}
use of com.haulmont.cuba.core.entity.Folder in project cuba by cuba-platform.
the class MetadataTest method testPersistentAndTransientProperties.
@Test
public void testPersistentAndTransientProperties() throws Exception {
MetadataTools tools = cont.metadata().getTools();
// User
MetaClass metaClass = cont.metadata().getSession().getClassNN(User.class);
assertTrue(tools.isPersistent(metaClass.getPropertyNN("id")));
assertTrue(tools.isPersistent(metaClass, metaClass.getPropertyNN("id")));
assertTrue(tools.isPersistent(metaClass.getPropertyNN("createTs")));
assertTrue(tools.isPersistent(metaClass.getPropertyNN("login")));
assertTrue(tools.isPersistent(metaClass.getPropertyNN("group")));
assertTrue(tools.isPersistent(metaClass.getPropertyNN("userRoles")));
// EntityLogItem
metaClass = cont.metadata().getSession().getClassNN(EntityLogItem.class);
assertTrue(tools.isPersistent(metaClass.getPropertyNN("user")));
assertFalse(tools.isPersistent(metaClass.getPropertyNN("attributes")));
assertTrue(tools.isNotPersistent(metaClass.getPropertyNN("attributes")));
// Folder
metaClass = cont.metadata().getSession().getClassNN(Folder.class);
assertTrue(tools.isPersistent(metaClass.getPropertyNN("name")));
assertTrue(tools.isNotPersistent(new Folder(), "itemStyle"));
// UserSessionEntity
metaClass = cont.metadata().getSession().getClassNN(UserSessionEntity.class);
// see JavaDocs on isPersistent() for the reason
assertTrue(tools.isPersistent(metaClass.getPropertyNN("id")));
assertTrue(tools.isNotPersistent(metaClass, metaClass.getPropertyNN("id")));
assertTrue(tools.isNotPersistent(metadata.create(metaClass), "id"));
assertTrue(tools.isNotPersistent(metaClass.getPropertyNN("login")));
assertTrue(tools.isNotPersistent(metaClass, metaClass.getPropertyNN("login")));
// TestTransientEntity
metaClass = cont.metadata().getSession().getClassNN(TestNotPersistentEntity.class);
// see JavaDocs on isPersistent() for the reason
assertTrue(tools.isPersistent(metaClass.getPropertyNN("id")));
assertTrue(tools.isNotPersistent(metaClass, metaClass.getPropertyNN("id")));
assertTrue(tools.isNotPersistent(metadata.create(metaClass), "id"));
assertTrue(tools.isNotPersistent(metaClass.getPropertyNN("name")));
assertTrue(tools.isNotPersistent(metaClass.getPropertyNN("info")));
assertTrue(tools.isNotPersistent(metaClass.getPropertyNN("embeddedRef")));
assertFalse(tools.isEmbedded(metaClass.getPropertyNN("embeddedRef")));
}
use of com.haulmont.cuba.core.entity.Folder in project cuba by cuba-platform.
the class FolderEditWindow method commit.
protected void commit() {
SearchFolder folder = (SearchFolder) FolderEditWindow.this.folder;
if (StringUtils.trimToNull(nameField.getValue()) == null) {
String msg = messages.getMainMessage("folders.folderEditWindow.emptyName");
App.getInstance().getWindowManager().showNotification(msg, Frame.NotificationType.TRAY);
return;
}
folder.setName(nameField.getValue());
folder.setTabName(tabNameField.getValue());
if (sortOrderField.getValue() == null || "".equals(sortOrderField.getValue())) {
folder.setSortOrder(null);
} else {
String value = sortOrderField.getValue();
int sortOrder;
try {
sortOrder = Integer.parseInt(value);
} catch (NumberFormatException e) {
String msg = messages.getMainMessage("folders.folderEditWindow.invalidSortOrder");
App.getInstance().getWindowManager().showNotification(msg, Frame.NotificationType.WARNING);
return;
}
folder.setSortOrder(sortOrder);
}
Object parent = parentSelect.getValue();
if (parent instanceof Folder)
folder.setParent((Folder) parent);
else
folder.setParent(null);
folder.setApplyDefault(Boolean.valueOf(applyDefaultCb.getValue().toString()));
if (globalCb != null) {
if (BooleanUtils.isTrue(globalCb.getValue())) {
folder.setUser(null);
} else {
folder.setUser(userSessionSource.getUserSession().getCurrentOrSubstitutedUser());
}
} else {
folder.setUser(userSessionSource.getUserSession().getCurrentOrSubstitutedUser());
}
if (presentation != null) {
folder.setPresentation((Presentation) presentation.getValue());
}
FolderEditWindow.this.commitHandler.run();
close();
}
use of com.haulmont.cuba.core.entity.Folder in project cuba by cuba-platform.
the class FoldersServiceBean method importFolder.
@Override
public Folder importFolder(Folder parentFolder, byte[] bytes) throws IOException {
if (!security.isEntityOpPermitted(Folder.class, EntityOp.CREATE)) {
throw new AccessDeniedException(PermissionType.ENTITY_OP, Folder.class.getSimpleName());
}
Folder folder = null;
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
ZipArchiveInputStream archiveReader;
archiveReader = new ZipArchiveInputStream(byteArrayInputStream);
ZipArchiveEntry archiveEntry;
while (((archiveEntry = archiveReader.getNextZipEntry()) != null) && (folder == null)) {
if (archiveEntry.getName().equals("folder.xml")) {
String xml = new String(IOUtils.toByteArray(archiveReader), StandardCharsets.UTF_8);
folder = (Folder) createXStream().fromXML(xml);
}
}
byteArrayInputStream.close();
if (folder != null) {
checkImportPermissions(folder);
folder.setParent(parentFolder);
Transaction tx = persistence.createTransaction();
try {
EntityManager em = persistence.getEntityManager();
em.setSoftDeletion(false);
Folder existingFolder = em.find(Folder.class, folder.getId());
if (existingFolder != null) {
checkImportPermissions(existingFolder);
folder.setVersion(existingFolder.getVersion());
folder.setCreateTs(existingFolder.getCreateTs());
folder.setCreatedBy(existingFolder.getCreatedBy());
} else {
User user = userSessionSource.getUserSession().getUser();
folder.setCreatedBy(user.getLoginLowerCase());
folder.setCreateTs(timeSource.currentTimestamp());
folder.setUpdatedBy(null);
folder.setUpdateTs(null);
folder.setVersion(0);
}
em.merge(folder);
tx.commit();
} finally {
tx.end();
}
}
return folder;
}
use of com.haulmont.cuba.core.entity.Folder in project cuba by cuba-platform.
the class FoldersServiceBean method checkImportPermissions.
protected void checkImportPermissions(Folder folder) {
UserSession userSession = userSessionSource.getUserSession();
if (folder instanceof SearchFolder) {
SearchFolder searchFolder = (SearchFolder) folder;
User currentUser = userSession.getCurrentOrSubstitutedUser();
if (searchFolder.getUser() != null && !currentUser.equals(searchFolder.getUser())) {
throw new AccessDeniedException(PermissionType.ENTITY_OP, Folder.class.getSimpleName());
}
if (searchFolder.getUser() == null && !userSession.isSpecificPermitted("cuba.gui.searchFolder.global")) {
throw new AccessDeniedException(PermissionType.ENTITY_OP, Folder.class.getSimpleName());
}
}
if (folder instanceof AppFolder) {
if (!userSession.isSpecificPermitted("cuba.gui.appFolder.global")) {
throw new AccessDeniedException(PermissionType.ENTITY_OP, Folder.class.getSimpleName());
}
}
}
Aggregations