protected String getProjectId() { return userContext != null ? userContext.getProjectId() : null; }
public void checkBasePolicies() { if (classifyDocumentApi instanceof ClassifyDocumentApiDirectImpl) { logger.debug("Registering Handlers"); UserContext userContext = corePolicyApplicationContext.getBean(UserContext.class); // Dont run under a particular user for this! Its about system base data. userContext.setProjectId(null); ServiceLoader<WorkerPolicyHandler> loader = ServiceLoader.load(WorkerPolicyHandler.class); // This is per user registration of policy handlers per policy type. // not to be confused with the base data creation on startup. for (WorkerPolicyHandler handler : loader) { register.checkAndRegisterHandlerPolicyTypes(handler); } } }
@Override public SequenceWorkflowEntryRepository.Item preSave(SequenceWorkflowEntryRepository.Item item, Session session){ //The collectionSequence on the entry is only used for filter/sorting, remove before updating. if(item.sequenceWorkflowEntry.collectionSequence!=null){ item.sequenceWorkflowEntry.collectionSequence = null; } if (!Strings.isNullOrEmpty(userContext.getProjectId())) { return item; } throw new RuntimeException("Unable to save item - projectId is null"); }
/** * Get the handlers for a given user context. This will internally register either * a user specific type or a base data type implicitally. * * @param projectId */ private void registerHandlers(String projectId) { if (classifyDocumentApi instanceof ClassifyDocumentApiDirectImpl) { UserContext userContext = corePolicyApplicationContext.getBean(UserContext.class); userContext.setProjectId(projectId); if (!registeredUsers.contains(projectId)) { ServiceLoader<WorkerPolicyHandler> loader = ServiceLoader.load(WorkerPolicyHandler.class); for (WorkerPolicyHandler handler : loader) { setUpWorkerHandler(handler, projectId); } registeredUsers.add(projectId); } } }
@Override protected Collection<NameValuePair> getApiParams() { Collection<NameValuePair> params = new ArrayList<>(1); params.add(new BasicNameValuePair("project_id", context.getProjectId())); return params; } }
public IntegrationTestBase() { // in certain cases we now have to shutdown the main ApiApplicationInstance as once a profile has been activated // it doesn't remove itself, it has to be rebuilt. As such a quick request here will ensure we have a new context. // if it has been removed. if (genericApplicationContext == null) { genericApplicationContext = new CorePolicyApplicationContext(); genericApplicationContext.refresh(); } apiProperties = genericApplicationContext.getBean(ApiProperties.class); try { // let our deployment type setup the usercontext correctly. depending on iod/web switch (apiProperties.getMode()) { case "direct": case "web": { // set the real user context now! userContext = genericApplicationContext.getBean(UserContext.class); userContext.setProjectId(UUID.randomUUID().toString()); break; } } } catch (Exception ex) { getLogger().trace("Exception in IntegrationTestBase... ", ex); throw new RuntimeException(ex); } classificationApi = genericApplicationContext.getBean(ClassificationApi.class); }
@Override protected Collection<NameValuePair> getApiParams() { Collection<NameValuePair> params = new ArrayList<>(1); params.add(new BasicNameValuePair("project_id", context.getProjectId())); return params; }
userContext.setProjectId(taskData.getProjectId());
@Override protected Collection<NameValuePair> getApiParams() { Collection<NameValuePair> params = new ArrayList<>(1); params.add(new BasicNameValuePair("project_id", context.getProjectId())); return params; } }
@Override protected Collection<NameValuePair> getApiParams() { Collection<NameValuePair> params = new ArrayList<>(1); params.add(new BasicNameValuePair("project_id", context.getProjectId())); return params; } }
@Override protected Collection<NameValuePair> getApiParams() { Collection<NameValuePair> params = new ArrayList<>(1); params.add(new BasicNameValuePair("project_id", context.getProjectId())); return params; } }
@Override protected Collection<NameValuePair> getApiParams() { Collection<NameValuePair> params = new ArrayList<>(1); params.add(new BasicNameValuePair("project_id", context.getProjectId())); return params; } }
private String getProjectIdAllowingForAdminApi( ){ // Get the thread scoped admin context. AdminUserContext adminUserContext = applicationContext.getBean(AdminUserContext.class); if ( adminUserContext != null && adminUserContext.isAllowAdministration() ) { return null; } return userContext.getProjectId(); } }
protected <R extends T> R preSave(T item, Session session){ // Check in preSave that we are allowed to add items with a projectId of null. // This is only allowed for certain types, when api.admin.basedata=true if (!Strings.isNullOrEmpty(userContext.getProjectId())) { return (R)item; } // check is the adminUserContext allowing administration of base data. AdminUserContext adminUserContext = applicationContext.getBean(AdminUserContext.class); if ( adminUserContext != null && adminUserContext.isAllowAdministration() ) { return (R) item; } throw new RuntimeException("Unable to save item - projectId is null"); }
protected void delete(Session session, Long id) { // Check in preSave that we are allowed to add items with a projectId of null. // This is only allowed for certain types, when api.admin.basedata=true if (Strings.isNullOrEmpty(userContext.getProjectId())) { throw new RuntimeException("Unable to delete item " + objectName + " - projectId is null"); } //noinspection JpaQlInspection String hql = "delete from " + objectName + " where id= :id and projectId= :projectId"; int i = session.createQuery(hql) .setLong("id", id) .setString("projectId", userContext.getProjectId()) .executeUpdate(); if (i == 0) { throw new RuntimeException("Item could not be deleted"); } }
private Criteria createItemBaseCriteriaForParentConditionId(Session session, Filter filter, Long parentConditionId, Boolean isFragment){ // by default we use the type parameter class of this repository. // if we have a filter, it may be that we need the real concrete class which is // used as a discriminator at the backend. Class<?> criteriaClass = ( filter == null ) ? typeParameterClass : AnnotationHelper.getRealRequestClassType(typeParameterClass, filter); Criteria criteria = session.createCriteria(criteriaClass); criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY) .add(Restrictions.eq("projectId", userContext.getProjectId())) .add(parentConditionId == null ? Restrictions.isNull("parentConditionId") : Restrictions.eq("parentConditionId", parentConditionId)); if ( isFragment != null ) { criteria.add(Restrictions.eq("isFragment", isFragment)); } return criteria; }
@Override public Set<Long> getCollectionIdsForPolicy(ExecutionContext executionContext, long policyId) { Session session = getSession(executionContext); String hql = "select collectionId from HibernateCollectionPolicy where policyId= :policyId and projectId= :projectId"; List<Long> list = session.createQuery(hql) .setLong("policyId", policyId) .setString("projectId", userContext.getProjectId()) .list(); HashSet<Long> ids = new HashSet<>(); if (list != null) { ids.addAll(list); } return ids; }
@Override public Collection<Item> retrieve(ExecutionContext executionContext, Collection<Long> ids, Boolean includeChildren) { Session session = getSession(executionContext); Criteria criteria = session.createCriteria(typeParameterClass) .add(Restrictions.in("id", ids)) .add(Restrictions.eq("projectId", userContext.getProjectId())) .setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY); List<Condition> conditions = criteria.list(); //check all items are returned if(!ids.stream().distinct().allMatch(u->conditions.stream().anyMatch(i-> Objects.equals(i.id, u)))){ throw new RuntimeException("Could not return conditions for all ids"); } loadChildren(session, conditions, includeChildren); return toItem(conditions); }
@Override public Set<Long> getPolicyIdsForCollection(ExecutionContext executionContext, long collectionId) { Session session = getSession(executionContext); String hql = "select policyId from HibernateCollectionPolicy where collectionId= :collectionId and projectId= :projectId"; List<Long> list = session.createQuery(hql) .setLong("collectionId", collectionId) .setString("projectId", userContext.getProjectId()) .list(); HashSet<Long> ids = new HashSet<>(); if (list != null) { ids.addAll(list); } return ids; }
@Override protected void delete(Session session, Long id) { String hql = "delete from " + objectName + " where id= :id and collection.projectId= :projectId"; int i = session.createQuery(hql) .setLong("id", id) .setString("projectId", userContext.getProjectId()) .executeUpdate(); if(i == 0){ throw new RuntimeException("Item could not be deleted"); } }