use of org.hibernate.query.Query in project midpoint by Evolveum.
the class AbstractOrgClosureTest method _test410RandomUnloadOrgStructure.
protected void _test410RandomUnloadOrgStructure() throws Exception {
OperationResult opResult = new OperationResult("===[ test410RandomUnloadOrgStructure ]===");
long start = System.currentTimeMillis();
randomRemoveOrgStructure(opResult);
System.out.println("Removed in " + (System.currentTimeMillis() - start) + " ms");
try (Session session = openSession()) {
Query q = session.createNativeQuery("select count(*) from m_org_closure");
Object count = q.list().get(0);
System.out.println("OrgClosure table has " + count + " rows");
assertEquals("Closure is not empty", "0", count.toString());
}
logger.info("Finish.");
}
use of org.hibernate.query.Query in project midpoint by Evolveum.
the class AbstractOrgClosureTest method getOrgChildren.
private List<String> getOrgChildren(String oid) {
try (Session session = openSession()) {
Query childrenQuery = session.createQuery("select distinct parentRef.ownerOid from RObjectReference as parentRef" + " join parentRef.owner as owner where parentRef.targetOid=:oid and parentRef.referenceType=0" + " and owner.objectTypeClass = :orgType");
// TODO eliminate use of parameter here
childrenQuery.setParameter("orgType", RObjectType.ORG);
childrenQuery.setParameter("oid", oid);
// noinspection unchecked
return childrenQuery.list();
}
}
use of org.hibernate.query.Query in project midpoint by Evolveum.
the class ObjectRetriever method searchShadowOwnerAttempt.
public <F extends FocusType> PrismObject<F> searchShadowOwnerAttempt(String shadowOid, Collection<SelectorOptions<GetOperationOptions>> options, OperationResult result) {
LOGGER_PERFORMANCE.debug("> search shadow owner for oid={}", shadowOid);
PrismObject<F> owner = null;
Session session = null;
try {
session = baseHelper.beginReadOnlyTransaction();
LOGGER.trace("Selecting account shadow owner for account {}.", shadowOid);
Query query = session.getNamedQuery("searchShadowOwner.getOwner");
query.setParameter("oid", shadowOid);
query.setResultTransformer(GetObjectResult.RESULT_STYLE.getResultTransformer());
@SuppressWarnings({ "unchecked", "raw" }) List<GetObjectResult> focuses = query.list();
LOGGER.trace("Found {} focuses, transforming data to JAXB types.", focuses != null ? focuses.size() : 0);
if (focuses == null || focuses.isEmpty()) {
// account shadow owner was not found
return null;
} else if (focuses.size() > 1) {
LOGGER.warn("Found {} owners for shadow oid {}, returning first owner.", focuses.size(), shadowOid);
}
GetObjectResult focus = focuses.get(0);
owner = updateLoadedObject(focus, (Class<F>) FocusType.class, null, options, null, session);
session.getTransaction().commit();
} catch (SchemaException | RuntimeException | ObjectNotFoundException ex) {
baseHelper.handleGeneralException(ex, session, result);
} finally {
baseHelper.cleanupSessionAndResult(session, result);
}
return owner;
}
use of org.hibernate.query.Query in project midpoint by Evolveum.
the class ObjectRetriever method isAnySubordinateAttempt.
public boolean isAnySubordinateAttempt(String upperOrgOid, Collection<String> lowerObjectOids) {
Session session = null;
try {
session = baseHelper.beginReadOnlyTransaction();
Query query;
if (lowerObjectOids.size() == 1) {
query = session.getNamedQuery("isAnySubordinateAttempt.oneLowerOid");
query.setParameter("dOid", lowerObjectOids.iterator().next());
} else {
query = session.getNamedQuery("isAnySubordinateAttempt.moreLowerOids");
query.setParameterList("dOids", lowerObjectOids);
}
query.setParameter("aOid", upperOrgOid);
Number number = (Number) query.uniqueResult();
session.getTransaction().commit();
return number != null && number.longValue() != 0L;
} catch (RuntimeException ex) {
baseHelper.handleGeneralException(ex, session, null);
} finally {
baseHelper.cleanupSessionAndResult(session, null);
}
throw new SystemException("isAnySubordinateAttempt failed somehow, this really should not happen.");
}
use of org.hibernate.query.Query in project midpoint by Evolveum.
the class LookupTableHelper method updateLoadedLookupTable.
<T extends ObjectType> void updateLoadedLookupTable(PrismObject<T> object, Collection<SelectorOptions<GetOperationOptions>> options, Session session) throws SchemaException {
if (!SelectorOptions.hasToLoadPath(LookupTableType.F_ROW, options)) {
return;
}
LOGGER.debug("Loading lookup table data.");
GetOperationOptions getOption = findLookupTableGetOption(options);
RelationalValueSearchQuery queryDef = getOption == null ? null : getOption.getRelationalValueSearchQuery();
Query query = setupLookupTableRowsQuery(session, queryDef, object.getOid());
if (queryDef != null && queryDef.getPaging() != null) {
ObjectPaging paging = queryDef.getPaging();
if (paging.getOffset() != null) {
query.setFirstResult(paging.getOffset());
}
if (paging.getMaxSize() != null) {
query.setMaxResults(paging.getMaxSize());
}
}
// noinspection unchecked
List<RLookupTableRow> rows = query.list();
if (CollectionUtils.isNotEmpty(rows)) {
LookupTableType lookup = (LookupTableType) object.asObjectable();
List<LookupTableRowType> jaxbRows = lookup.getRow();
for (RLookupTableRow row : rows) {
LookupTableRowType jaxbRow = row.toJAXB(prismContext);
jaxbRows.add(jaxbRow);
}
PrismContainer<LookupTableRowType> rowContainer = object.findContainer(LookupTableType.F_ROW);
rowContainer.setIncomplete(false);
} else {
PrismContainer<LookupTableRowType> rowContainer = object.findContainer(LookupTableType.F_ROW);
if (rowContainer != null) {
// just in case
rowContainer.clear();
rowContainer.setIncomplete(false);
}
}
}
Aggregations