/** * Entry point to the fluent API for loading entities. * <p> * Usage example: * <pre> * Customer customer = dataManager.load(customerId).view("with-grade").one(); * </pre> * @param entityId {@link Id} of entity that needs to be loaded */ default <E extends Entity<K>, K> FluentLoader.ById<E, K> load(Id<E, K> entityId) { return new FluentLoader<>(entityId.getEntityClass(), this).id(entityId.getValue()); }
/** * Loads a list of entities. */ public List<E> list() { LoadContext<E> loadContext = createLoadContext(); return dataManager.loadList(loadContext); }
/** * Finds active user by login. Used for client authentication. * @param login - user login * @return user by login */ public @Nullable User findUserByLogin(String login) { if (login == null) { throw new IllegalArgumentException("Login is null"); } Optional<User> result = dataManager.load(User.class) .view(View.LOCAL) .query("select u from sec$User u where u.loginLowerCase = :login and (u.active = true or u.active is null)") .parameter("login", login.toLowerCase()) .optional(); if (!result.isPresent()) { log.debug("Unable to find user: {}", login); } return result.orElse(null); } }
@Override public <E extends Entity<K>, K> FluentLoader<E, K> load(Class<E> entityClass) { return new FluentLoader<>(entityClass, this); }
LoadContext<E> createLoadContext() { LoadContext<E> loadContext = LoadContext.create(entityClass); initCommonLoadContextParameters(loadContext); String entityName = AppBeans.get(Metadata.class).getClassNN(entityClass).getName(); String queryString = String.format("select e from %s e", entityName); loadContext.setQuery(LoadContext.createQuery(queryString)); return loadContext; }
protected List<Entity> loadEntities(List<Object> entityIds, MetaClass metaClass) { MetaProperty primaryKeyForFts = manager.getPrimaryKeyPropertyForFts(metaClass); List<Entity> result = new ArrayList<>(); for (List<Object> partition : Lists.partition(entityIds, ftsConfig.getLoadSize())) { List<Entity<Object>> partitionResult = dataManager.secure() .load(metaClass.getJavaClass()) .view(View.MINIMAL) .query(String.format("select e from %s e where e.%s in :ids", metaClass.getName(), primaryKeyForFts.getName())) .parameter("ids", partition) .list(); result.addAll(partitionResult); } return result; } }
@Override public <E extends Entity<K>, K> FluentLoader<E, K> load(Class<E> entityClass) { return new FluentLoader<>(entityClass, dataManager, true); }
@Override public <E extends Entity<K>, K> FluentLoader.ById<E, K> load(Id<E, K> entityId) { return new FluentLoader<>(entityId.getEntityClass(), dataManager, true).id(entityId.getValue()); }
/** * Entry point to the fluent API for loading entities. * <p> * Usage examples: * <pre> * Customer customer = dataManager.load(Customer.class).id(someId).one(); * * List<Customer> customers = dataManager.load(Customer.class) * .query("select c from sample$Customer c where c.name = :name") * .parameter("name", "Smith") * .view("customer-view") * .list(); * </pre> * @param entityClass class of entity that needs to be loaded */ default <E extends Entity<K>, K> FluentLoader<E, K> load(Class<E> entityClass) { return new FluentLoader<>(entityClass, this); }
/** * Loads a single instance and wraps it in Optional. */ public Optional<E> optional() { LoadContext<E> loadContext = createLoadContext(); loadContext.getQuery().setMaxResults(1); return Optional.ofNullable(dataManager.load(loadContext)); }
/** * Loads a single instance. * * @throws IllegalStateException if nothing was loaded */ public E one() { LoadContext<E> loadContext = createLoadContext(); loadContext.getQuery().setMaxResults(1); E entity = dataManager.load(loadContext); if (entity != null) return entity; else throw new IllegalStateException("No results"); }