List of usage examples for org.hibernate Query setString
@Deprecated @SuppressWarnings("unchecked") default Query<R> setString(String name, String val)
From source file:com.evolveum.midpoint.repo.sql.helpers.ObjectRetriever.java
License:Apache License
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;//w ww. j a v a2s . co m Session session = null; try { session = baseHelper.beginReadOnlyTransaction(); LOGGER.trace("Selecting account shadow owner for account {}.", new Object[] { shadowOid }); Query query = session.getNamedQuery("searchShadowOwner.getOwner"); query.setString("oid", shadowOid); query.setResultTransformer(GetObjectResult.RESULT_TRANSFORMER); List<GetObjectResult> focuses = query.list(); LOGGER.trace("Found {} focuses, transforming data to JAXB types.", new Object[] { (focuses != null ? focuses.size() : 0) }); if (focuses == null || focuses.isEmpty()) { // account shadow owner was not found return null; } if (focuses.size() > 1) { LOGGER.warn("Found {} owners for shadow oid {}, returning first owner.", new Object[] { focuses.size(), shadowOid }); } GetObjectResult focus = focuses.get(0); owner = updateLoadedObject(focus, (Class<F>) FocusType.class, null, options, session, result); session.getTransaction().commit(); } catch (SchemaException | RuntimeException ex) { baseHelper.handleGeneralException(ex, session, result); } finally { baseHelper.cleanupSessionAndResult(session, result); } return owner; }
From source file:com.evolveum.midpoint.repo.sql.helpers.ObjectRetriever.java
License:Apache License
public PrismObject<UserType> listAccountShadowOwnerAttempt(String accountOid, OperationResult result) throws ObjectNotFoundException { LOGGER_PERFORMANCE.debug("> list account shadow owner oid={}", accountOid); PrismObject<UserType> userType = null; Session session = null;//from w w w. j a v a 2s .co m try { session = baseHelper.beginReadOnlyTransaction(); Query query = session.getNamedQuery("listAccountShadowOwner.getUser"); query.setString("oid", accountOid); query.setResultTransformer(GetObjectResult.RESULT_TRANSFORMER); List<GetObjectResult> users = query.list(); LOGGER.trace("Found {} users, transforming data to JAXB types.", new Object[] { (users != null ? users.size() : 0) }); if (users == null || users.isEmpty()) { // account shadow owner was not found return null; } if (users.size() > 1) { LOGGER.warn("Found {} users for account oid {}, returning first user. [interface change needed]", new Object[] { users.size(), accountOid }); } GetObjectResult user = users.get(0); userType = updateLoadedObject(user, UserType.class, null, null, session, result); session.getTransaction().commit(); } catch (SchemaException | RuntimeException ex) { baseHelper.handleGeneralException(ex, session, result); } finally { baseHelper.cleanupSessionAndResult(session, result); } return userType; }
From source file:com.evolveum.midpoint.repo.sql.helpers.ObjectRetriever.java
License:Apache License
/** * This method provides object parsing from String and validation. *//*w ww . ja va 2 s . c o m*/ private <T extends ObjectType> PrismObject<T> updateLoadedObject(GetObjectResult result, Class<T> type, String oid, Collection<SelectorOptions<GetOperationOptions>> options, Session session, OperationResult operationResult) throws SchemaException { String xml = RUtil.getXmlFromByteArray(result.getFullObject(), getConfiguration().isUseZip()); PrismObject<T> prismObject; try { // "Postel mode": be tolerant what you read. We need this to tolerate (custom) schema changes ParsingContext parsingContext = ParsingContext.forMode(XNodeProcessorEvaluationMode.COMPAT); prismObject = prismContext.parseObject(xml, parsingContext); // TODO enable if needed // if (parsingContext.hasWarnings()) { // for (String warning : parsingContext.getWarnings()) { // operationResult.createSubresult("parseObject").recordWarning(warning); // } // } } catch (SchemaException | RuntimeException | Error e) { // This is a serious thing. We have corrupted XML in the repo. This may happen even // during system init. We want really loud and detailed error here. LOGGER.error("Couldn't parse object {} {}: {}: {}\n{}", type.getSimpleName(), oid, e.getClass().getName(), e.getMessage(), xml, e); throw e; } if (FocusType.class.isAssignableFrom(prismObject.getCompileTimeClass())) { if (SelectorOptions.hasToLoadPath(FocusType.F_JPEG_PHOTO, options)) { //todo improve, use user.hasPhoto flag and take options into account [lazyman] //this is called only when options contains INCLUDE user/jpegPhoto Query query = session.getNamedQuery("get.focusPhoto"); query.setString("oid", prismObject.getOid()); byte[] photo = (byte[]) query.uniqueResult(); if (photo != null) { PrismProperty property = prismObject.findOrCreateProperty(FocusType.F_JPEG_PHOTO); property.setRealValue(photo); } } } else if (ShadowType.class.equals(prismObject.getCompileTimeClass())) { //we store it because provisioning now sends it to repo, but it should be transient prismObject.removeContainer(ShadowType.F_ASSOCIATION); LOGGER.debug("Loading definitions for shadow attributes."); Short[] counts = result.getCountProjection(); Class[] classes = GetObjectResult.EXT_COUNT_CLASSES; for (int i = 0; i < classes.length; i++) { if (counts[i] == null || counts[i] == 0) { continue; } applyShadowAttributeDefinitions(classes[i], prismObject, session); } LOGGER.debug("Definitions for attributes loaded. Counts: {}", Arrays.toString(counts)); } else if (LookupTableType.class.equals(prismObject.getCompileTimeClass())) { lookupTableHelper.updateLoadedLookupTable(prismObject, options, session); } else if (AccessCertificationCampaignType.class.equals(prismObject.getCompileTimeClass())) { caseHelper.updateLoadedCampaign(prismObject, options, session); } nameResolutionHelper.resolveNamesIfRequested(session, prismObject.getValue(), options); validateObjectType(prismObject, type); return prismObject; }
From source file:com.evolveum.midpoint.repo.sql.helpers.ObjectRetriever.java
License:Apache License
public <T extends ShadowType> List<PrismObject<T>> listResourceObjectShadowsAttempt(String resourceOid, Class<T> resourceObjectShadowType, OperationResult result) throws ObjectNotFoundException, SchemaException { LOGGER_PERFORMANCE.debug("> list resource object shadows {}, for resource oid={}", new Object[] { resourceObjectShadowType.getSimpleName(), resourceOid }); List<PrismObject<T>> list = new ArrayList<>(); Session session = null;//from w w w. jav a 2 s. com try { session = baseHelper.beginReadOnlyTransaction(); Query query = session.getNamedQuery("listResourceObjectShadows"); query.setString("oid", resourceOid); query.setResultTransformer(GetObjectResult.RESULT_TRANSFORMER); List<GetObjectResult> shadows = query.list(); LOGGER.debug("Query returned {} shadows, transforming to JAXB types.", new Object[] { (shadows != null ? shadows.size() : 0) }); if (shadows != null) { for (GetObjectResult shadow : shadows) { PrismObject<T> prismObject = updateLoadedObject(shadow, resourceObjectShadowType, null, null, session, result); list.add(prismObject); } } session.getTransaction().commit(); } catch (SchemaException | RuntimeException ex) { baseHelper.handleGeneralException(ex, session, result); } finally { baseHelper.cleanupSessionAndResult(session, result); } return list; }
From source file:com.evolveum.midpoint.repo.sql.helpers.ObjectRetriever.java
License:Apache License
public <T extends ObjectType> String getVersionAttempt(Class<T> type, String oid, OperationResult result) throws ObjectNotFoundException, SchemaException { LOGGER_PERFORMANCE.debug("> get version {}, oid={}", new Object[] { type.getSimpleName(), oid }); String version = null;/*from ww w. j a v a2s.co m*/ Session session = null; try { session = baseHelper.beginReadOnlyTransaction(); Query query = session.getNamedQuery("getVersion"); query.setString("oid", oid); Number versionLong = (Number) query.uniqueResult(); if (versionLong == null) { throw new ObjectNotFoundException( "Object '" + type.getSimpleName() + "' with oid '" + oid + "' was not found."); } version = versionLong.toString(); session.getTransaction().commit(); } catch (RuntimeException ex) { baseHelper.handleGeneralRuntimeException(ex, session, result); } finally { baseHelper.cleanupSessionAndResult(session, result); } return version; }
From source file:com.evolveum.midpoint.repo.sql.helpers.ObjectRetriever.java
License:Apache License
public boolean isAnySubordinateAttempt(String upperOrgOid, Collection<String> lowerObjectOids) { Session session = null;//from w w w.jav a 2 s . c o m try { session = baseHelper.beginReadOnlyTransaction(); Query query; if (lowerObjectOids.size() == 1) { query = session.getNamedQuery("isAnySubordinateAttempt.oneLowerOid"); query.setString("dOid", lowerObjectOids.iterator().next()); } else { query = session.getNamedQuery("isAnySubordinateAttempt.moreLowerOids"); query.setParameterList("dOids", lowerObjectOids); } query.setString("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."); }
From source file:com.evolveum.midpoint.repo.sql.helpers.OrgClosureManager.java
License:Apache License
private void addEdgeSimple(String oid, String parent, Session session) { if (parent != null) { long start = System.currentTimeMillis(); Query addToClosureQuery = session .createSQLQuery("insert into " + CLOSURE_TABLE_NAME + " (descendant_oid, ancestor_oid, val) " + "select :oid as descendant_oid, CL.ancestor_oid as ancestor_oid, CL.val as val " + "from " + CLOSURE_TABLE_NAME + " CL " + "where CL.descendant_oid = :parent"); addToClosureQuery.setString("oid", oid); addToClosureQuery.setParameter("parent", parent); int count = addToClosureQuery.executeUpdate(); if (LOGGER.isTraceEnabled()) LOGGER.trace("addEdges simplified: Added {} records to closure table ({} ms).", count, System.currentTimeMillis() - start); }/*from w w w . ja v a 2s. c o m*/ session.flush(); session.clear(); }
From source file:com.evolveum.midpoint.repo.sql.helpers.OrgClosureManager.java
License:Apache License
private void handleDelete(String oid, Context context, Session session) { List<String> livingChildren = getChildren(oid, session); if (livingChildren.isEmpty()) { handleDeleteLeaf(oid, session);//from w w w . j a va 2s .co m return; } // delete all edges "<child> -> OID" from the closure removeChildrenEdges(oid, livingChildren, context, session); if (LOGGER.isTraceEnabled()) LOGGER.trace("Deleted {} 'child' links.", livingChildren.size()); // delete all edges "OID -> <parent>" from the closure List<String> livingParents = retainExistingOids(getParents(oid, session), session); removeParentEdges(oid, livingParents, context, session); if (LOGGER.isTraceEnabled()) LOGGER.trace("Deleted {} 'parent' links.", livingParents.size()); // delete (OID, OID) record Query deleteSelfQuery = session.createSQLQuery( "delete from " + CLOSURE_TABLE_NAME + " " + "where descendant_oid=:oid and ancestor_oid=:oid"); deleteSelfQuery.setString("oid", oid); int count = deleteSelfQuery.executeUpdate(); if (LOGGER.isTraceEnabled()) LOGGER.trace("Removed {} self-record from closure table.", count); }
From source file:com.evolveum.midpoint.repo.sql.helpers.OrgClosureManager.java
License:Apache License
private void handleDeleteLeaf(String oid, Session session) { Query removeFromClosureQuery = session .createSQLQuery("delete from " + CLOSURE_TABLE_NAME + " " + "where descendant_oid = :oid"); removeFromClosureQuery.setString("oid", oid); int count = removeFromClosureQuery.executeUpdate(); if (LOGGER.isTraceEnabled()) LOGGER.trace("DeleteLeaf: Removed {} records from closure table.", count); }
From source file:com.evolveum.midpoint.repo.sql.helpers.OrgClosureManager.java
License:Apache License
private List<String> getParents(String oid, Session session) { Query parentsQuery = session.createQuery( "select distinct targetOid from RObjectReference where ownerOid=:oid and referenceType=0"); parentsQuery.setString("oid", oid); return parentsQuery.list(); }