use of io.lumeer.storage.api.dao.GroupDao in project engine by Lumeer.
the class CollectionFacade method runRule.
public void runRule(final Collection collection, final String ruleId) {
if (adapter.getDocumentsCountByCollection(collection.getId()) > 2_000) {
throw new UnsuccessfulOperationException("Too many documents in the source collection");
}
final Rule rule = collection.getRules().get(ruleId);
if (rule != null && rule.getType() == Rule.RuleType.AUTO_LINK) {
final AutoLinkRule autoLinkRule = new AutoLinkRule(rule);
final String otherCollectionId = autoLinkRule.getCollection2().equals(collection.getId()) ? autoLinkRule.getCollection1() : autoLinkRule.getCollection2();
final String attributeId = autoLinkRule.getCollection1().equals(collection.getId()) ? autoLinkRule.getAttribute1() : autoLinkRule.getAttribute2();
final Attribute attribute = collection.getAttributes().stream().filter(a -> a.getId().equals(attributeId)).findFirst().orElse(null);
final Collection otherCollection = getCollection(otherCollectionId);
final String otherAttributeId = autoLinkRule.getCollection2().equals(collection.getId()) ? autoLinkRule.getAttribute1() : autoLinkRule.getAttribute2();
final Attribute otherAttribute = otherCollection.getAttributes().stream().filter(a -> a.getId().equals(otherAttributeId)).findFirst().orElse(null);
final Map<String, AllowedPermissions> permissions = permissionsChecker.getCollectionsPermissions(List.of(collection, otherCollection));
if (adapter.getDocumentsCountByCollection(otherCollectionId) > 10_000) {
throw new UnsuccessfulOperationException("Too many documents in the target collection");
}
final LinkType linkType = linkTypeDao.getLinkType(autoLinkRule.getLinkType());
final AutoLinkBatchTask task = taskFactory.getInstance(AutoLinkBatchTask.class);
task.setupBatch(autoLinkRule, linkType, collection, attribute, otherCollection, otherAttribute, getCurrentUser(), permissions);
taskExecutor.submitTask(task);
} else if (rule != null && rule.getType() == Rule.RuleType.CRON) {
final CronRule cronRule = new CronRule(rule);
List<Document> documents = new ArrayList<>();
if (cronRule.getViewId() != null) {
try {
final View view = viewDao.getViewById(cronRule.getViewId());
final User user = AuthenticatedUser.getMachineUser();
final AllowedPermissions allowedPermissions = AllowedPermissions.allAllowed();
documents = DocumentUtils.getDocuments(collectionDao, documentDao, dataDao, userDao, groupDao, selectionListDao, getOrganization(), getProject(), view.getQuery(), user, cronRule.getLanguage(), allowedPermissions, null);
documents = documents.stream().filter(document -> document.getCollectionId().equals(collection.getId())).collect(Collectors.toList());
} catch (ResourceNotFoundException ignore) {
}
}
RuleTask task = taskFactory.getInstance(RuleTask.class);
task.setRule(rule.getName(), rule, collection, documents);
taskExecutor.submitTask(task);
}
}
use of io.lumeer.storage.api.dao.GroupDao in project engine by Lumeer.
the class PermissionsCheckerTest method preparePermissionsChecker.
@Before
public void preparePermissionsChecker() {
User user = Mockito.mock(User.class);
Mockito.when(user.getId()).thenReturn(USER);
Mockito.when(user.getOrganizations()).thenReturn(Collections.singleton("LMR"));
AuthenticatedUser authenticatedUser = Mockito.mock(AuthenticatedUser.class);
Mockito.when(authenticatedUser.getCurrentUserId()).thenReturn(USER);
Mockito.when(authenticatedUser.getUserEmail()).thenReturn(USER);
Organization organization = Mockito.mock(Organization.class);
preparePermissions(organization, Collections.singleton(new Role(RoleType.Read)), Collections.emptySet());
Mockito.when(organization.getId()).thenReturn("LMR");
Project project = Mockito.mock(Project.class);
preparePermissions(project, Collections.singleton(new Role(RoleType.Read)), Collections.emptySet());
Mockito.when(project.getId()).thenReturn("LMR");
WorkspaceKeeper workspaceKeeper = Mockito.mock(WorkspaceKeeper.class);
Mockito.when(workspaceKeeper.getOrganization()).thenReturn(Optional.of(organization));
Mockito.when(workspaceKeeper.getProject()).thenReturn(Optional.of(project));
CollectionDao collectionDao = Mockito.mock(CollectionDao.class);
LinkTypeDao linkTypeDao = Mockito.mock(LinkTypeDao.class);
ViewDao viewDao = Mockito.mock(ViewDao.class);
UserDao userDao = Mockito.mock(UserDao.class);
Mockito.when(userDao.getUserById(USER)).thenReturn(user);
GroupDao groupDao = Mockito.mock(GroupDao.class);
Group group = Mockito.mock(Group.class);
Mockito.when(group.getId()).thenReturn(GROUP);
Mockito.when(group.getUsers()).thenReturn(Collections.singletonList(USER));
Mockito.when(groupDao.getAllGroups("LMR")).thenReturn(Collections.singletonList(group));
FavoriteItemDao favoriteItemDao = Mockito.mock(FavoriteItemDao.class);
DocumentDao documentDao = Mockito.mock(DocumentDao.class);
permissionsChecker = new PermissionsChecker(authenticatedUser, workspaceKeeper, userDao, groupDao, collectionDao, viewDao, linkTypeDao, favoriteItemDao, documentDao);
permissionsChecker.init();
}
Aggregations