@Override public Object load(String entityName, Serializable id) throws DataAccessException { return load(entityName, id, null); }
@Override public <T> T load(Class<T> entityClass, Serializable id) throws DataAccessException { return load(entityClass, id, null); }
@Override public Object load(String entityName, Serializable id) throws DataAccessException { return load(entityName, id, null); }
@Override public <T> T load(Class<T> entityClass, Serializable id) throws DataAccessException { return load(entityClass, id, null); }
@Override public <T> T load(final Class<T> entityClass, final Serializable id, @Nullable final LockMode lockMode) throws DataAccessException { return nonNull(executeWithNativeSession(session -> { if (lockMode != null) { return session.load(entityClass, id, new LockOptions(lockMode)); } else { return session.load(entityClass, id); } })); }
/** * Object will be marked as deleted (boolean flag), therefore undelete is always possible without any loss of data. * * @param obj */ @Override @Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW, isolation = Isolation.REPEATABLE_READ) public void markAsDeleted(final O obj) throws AccessException { Validate.notNull(obj); if (obj.getId() == null) { final String msg = "Could not delete object unless id is not given:" + obj.toString(); log.error(msg); throw new RuntimeException(msg); } final O dbObj = hibernateTemplate.load(clazz, obj.getId(), LockMode.PESSIMISTIC_WRITE); checkPartOfCurrentTenant(obj, OperationType.DELETE); checkLoggedInUserDeleteAccess(obj, dbObj); accessChecker.checkRestrictedOrDemoUser(); internalMarkAsDeleted(obj); }
@Test public void testUpdate() { String dn = "cn=Some Person,ou=company1,ou=Sweden"; OrgPerson person = (OrgPerson) this.hibernateTemplate.load(OrgPerson.class, new Integer(1)); person.setLastname("Updated Person"); person.setDescription("Updated description"); dummyDao.update(person); log.debug("Verifying result"); Object ldapResult = ldapTemplate.lookup(dn, new AttributesMapper() { public Object mapFromAttributes(Attributes attributes) throws NamingException { assertThat(attributes.get("sn").get()).isEqualTo("Updated Person"); assertThat(attributes.get("description").get()).isEqualTo("Updated description"); return new Object(); } }); OrgPerson updatedPerson = (OrgPerson) this.hibernateTemplate.load(OrgPerson.class, new Integer(1)); assertThat(updatedPerson.getLastname()).isEqualTo("Updated Person"); assertThat(updatedPerson.getDescription()).isEqualTo("Updated description"); assertThat(ldapResult).isNotNull(); }
@Test public void testUnbindWithException() { String dn = "cn=Some Person,ou=company1,ou=Sweden"; OrgPerson person = (OrgPerson) this.hibernateTemplate.load(OrgPerson.class, new Integer(1)); try { // Perform test dummyDao.unbindWithException(person); fail("DummyException expected"); } catch (DummyException expected) { assertThat(true).isTrue(); } person = null; // Verify result - check that the operation was properly rolled back Object ldapResult = ldapTemplate.lookup(dn, new AttributesMapper() { public Object mapFromAttributes(Attributes attributes) throws NamingException { // Just verify that the entry still exists. return new Object(); } }); person = (OrgPerson) this.hibernateTemplate.load(OrgPerson.class, new Integer(1)); // will // throw // exception // of // person // does // not // exist assertThat(ldapResult).isNotNull(); }
@Test public void testUnbindWithException() { String dn = "cn=Some Person,ou=company1,ou=Sweden"; OrgPerson person = (OrgPerson) this.hibernateTemplate.load(OrgPerson.class, new Integer(1)); try { // Perform test dummyDao.unbindWithException(person); fail("DummyException expected"); } catch (DummyException expected) { assertThat(true).isTrue(); } person = null; // Verify result - check that the operation was properly rolled back Object ldapResult = ldapTemplate.lookup(dn, new AttributesMapper() { public Object mapFromAttributes(Attributes attributes) throws NamingException { // Just verify that the entry still exists. return new Object(); } }); person = (OrgPerson) this.hibernateTemplate.load(OrgPerson.class, new Integer(1)); // will // throw // exception // of // person // does // not // exist assertThat(ldapResult).isNotNull(); }
@Test public void testUpdate() { String dn = "cn=Some Person,ou=company1,ou=Sweden"; OrgPerson person = (OrgPerson) this.hibernateTemplate.load(OrgPerson.class, new Integer(1)); person.setLastname("Updated Person"); person.setDescription("Updated description"); dummyDao.update(person); log.debug("Verifying result"); Object ldapResult = ldapTemplate.lookup(dn, new AttributesMapper() { public Object mapFromAttributes(Attributes attributes) throws NamingException { assertThat(attributes.get("sn").get()).isEqualTo("Updated Person"); assertThat(attributes.get("description").get()).isEqualTo("Updated description"); return new Object(); } }); OrgPerson updatedPerson = (OrgPerson) this.hibernateTemplate.load(OrgPerson.class, new Integer(1)); assertThat(updatedPerson.getLastname()).isEqualTo("Updated Person"); assertThat(updatedPerson.getDescription()).isEqualTo("Updated description"); assertThat(ldapResult).isNotNull(); }
@Test public void testUpdateWithException() { String dn = "cn=Some Person,ou=company1,ou=Sweden"; OrgPerson originalPerson = (OrgPerson) this.hibernateTemplate.load(OrgPerson.class, new Integer(1)); originalPerson.setLastname("fooo"); try { dummyDao.updateWithException(originalPerson); fail("DummyException expected"); } catch (DummyException expected) { assertThat(true).isTrue(); } log.debug("Verifying result"); Object ldapResult = ldapTemplate.lookup(dn, new AttributesMapper() { public Object mapFromAttributes(Attributes attributes) throws NamingException { assertThat(attributes.get("sn").get()).as("Person").isNotNull(); assertThat(attributes.get("description").get()).isEqualTo("Sweden, Company1, Some Person"); return new Object(); } }); OrgPerson notUpdatedPerson = (OrgPerson) this.hibernateTemplate.load(OrgPerson.class, new Integer(1)); assertThat(notUpdatedPerson.getLastname()).isEqualTo("Person"); assertThat(notUpdatedPerson.getDescription()).isEqualTo("Sweden, Company1, Some Person"); assertThat(ldapResult).isNotNull(); // no need to assert if notUpdatedPerson exists }
@Test public void testUpdateWithException() { String dn = "cn=Some Person,ou=company1,ou=Sweden"; OrgPerson originalPerson = (OrgPerson) this.hibernateTemplate.load(OrgPerson.class, new Integer(1)); originalPerson.setLastname("fooo"); try { dummyDao.updateWithException(originalPerson); fail("DummyException expected"); } catch (DummyException expected) { assertThat(true).isTrue(); } log.debug("Verifying result"); Object ldapResult = ldapTemplate.lookup(dn, new AttributesMapper() { public Object mapFromAttributes(Attributes attributes) throws NamingException { assertThat(attributes.get("sn").get()).as("Person").isNotNull(); assertThat(attributes.get("description").get()).isEqualTo("Sweden, Company1, Some Person"); return new Object(); } }); OrgPerson notUpdatedPerson = (OrgPerson) this.hibernateTemplate.load(OrgPerson.class, new Integer(1)); assertThat(notUpdatedPerson.getLastname()).isEqualTo("Person"); assertThat(notUpdatedPerson.getDescription()).isEqualTo("Sweden, Company1, Some Person"); assertThat(ldapResult).isNotNull(); // no need to assert if notUpdatedPerson exists }
@Test public void testUnbind() { String dn = "cn=Some Person,ou=company1,ou=Sweden"; // Perform test OrgPerson person = (OrgPerson) this.hibernateTemplate.load(OrgPerson.class, new Integer(1)); dummyDao.unbind(person); try { // Verify result - check that the operation was not rolled back ldapTemplate.lookup(dn); fail("NameNotFoundException expected"); } catch (NameNotFoundException expected) { assertThat(true).isTrue(); } person = (OrgPerson) this.hibernateTemplate.get(OrgPerson.class, new Integer(1)); assertThat(person).isNull(); } }
/** * User can modify own setting, this method ensures that only such properties will be updated, the user's are allowed * to. * * @param user */ @Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW, isolation = Isolation.REPEATABLE_READ) public void updateMyAccount(final PFUserDO user) { accessChecker.checkRestrictedOrDemoUser(); final PFUserDO contextUser = ThreadLocalUserContext.getUser(); Validate.isTrue(user.getId().equals(contextUser.getId()) == true); final PFUserDO dbUser = getHibernateTemplate().load(clazz, user.getId(), LockMode.PESSIMISTIC_WRITE); final String[] ignoreFields = { "deleted", "password", "lastLogin", "loginFailures", "username", "stayLoggedInKey", "authenticationToken", "rights" }; final ModificationStatus result = HistoryBaseDaoAdapter.wrappHistoryUpdate(dbUser, () -> copyValues(user, dbUser, ignoreFields)); if (result != ModificationStatus.NONE) { dbUser.setLastUpdate(); log.info("Object updated: " + dbUser.toString()); copyValues(user, contextUser, ignoreFields); } else { log.info("No modifications detected (no update needed): " + dbUser.toString()); } getUserGroupCache().updateUser(user); }
@Test public void testUnbind() { String dn = "cn=Some Person,ou=company1,ou=Sweden"; // Perform test OrgPerson person = (OrgPerson) this.hibernateTemplate.load(OrgPerson.class, new Integer(1)); dummyDao.unbind(person); try { // Verify result - check that the operation was not rolled back ldapTemplate.lookup(dn); fail("NameNotFoundException expected"); } catch (NameNotFoundException expected) { assertThat(true).isTrue(); } person = (OrgPerson) this.hibernateTemplate.get(OrgPerson.class, new Integer(1)); assertThat(person).isNull(); } }
@Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW, isolation = Isolation.REPEATABLE_READ) public void internalMarkAsDeleted(final O obj) { if (HistoryBaseDaoAdapter.isHistorizable(obj) == false) { log.error( "Object is not historizable. Therefore marking as deleted is not supported. Please use delete instead."); throw new InternalErrorException(); } onDelete(obj); final O dbObj = hibernateTemplate.load(clazz, obj.getId(), LockMode.PESSIMISTIC_WRITE); onSaveOrModify(obj); HistoryBaseDaoAdapter.wrappHistoryUpdate(dbObj, () -> { BaseDaoJpaAdapter.beforeUpdateCopyMarkDelete(dbObj, obj); copyValues(obj, dbObj, "deleted"); // If user has made additional changes. dbObj.setDeleted(true); dbObj.setLastUpdate(); flushSession(); flushSearchSession(); return null; }); afterSaveOrModify(obj); afterDelete(obj); flushSession(); log.info("Object marked as deleted: " + dbObj.toString()); }
@Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW, isolation = Isolation.REPEATABLE_READ) public void internalUndelete(final O obj) { final O dbObj = hibernateTemplate.load(clazz, obj.getId(), LockMode.PESSIMISTIC_WRITE); onSaveOrModify(obj); HistoryBaseDaoAdapter.wrappHistoryUpdate(dbObj, () -> { BaseDaoJpaAdapter.beforeUpdateCopyMarkUnDelete(dbObj, obj); copyValues(obj, dbObj, "deleted"); // If user has made additional changes. dbObj.setDeleted(false); obj.setDeleted(false); dbObj.setLastUpdate(); obj.setLastUpdate(dbObj.getLastUpdate()); flushSession(); flushSearchSession(); return null; }); afterSaveOrModify(obj); afterUndelete(obj); log.info("Object undeleted: " + dbObj.toString()); }
String dn = "cn=Some Person2,ou=company1,ou=Sweden"; String newDn = "cn=Some Person2,ou=company2,ou=Sweden"; OrgPerson person = (OrgPerson) this.hibernateTemplate.load(OrgPerson.class, new Integer(1)); person.setLastname("Updated Person"); person.setDescription("Updated description");
/** * @param obj * @return true, if already existing entry was updated, otherwise false (e. g. if no entry exists for update). */ private boolean internalUpdate(final PersonalContactDO obj) { PersonalContactDO dbObj = null; if (obj.getId() != null) { dbObj = hibernateTemplate.load(PersonalContactDO.class, obj.getId(), LockMode.PESSIMISTIC_WRITE); } if (dbObj == null) { dbObj = getByContactId(obj.getContactId()); } if (dbObj == null) { return false; } checkAccess(dbObj); Validate.isTrue(ObjectUtils.equals(dbObj.getContactId(), obj.getContactId())); obj.setId(dbObj.getId()); // Copy all values of modified user to database object. final ModificationStatus modified = dbObj.copyValuesFrom(obj, "owner", "address", "id"); if (modified == ModificationStatus.MAJOR) { dbObj.setLastUpdate(); log.info("Object updated: " + dbObj.toString()); } return true; }
/** * @param obj * @return true, if already existing entry was updated, otherwise false (e. g. if no entry exists for update). */ private boolean internalUpdate(final PersonalAddressDO obj) { PersonalAddressDO dbObj = null; if (obj.getId() != null) { dbObj = hibernateTemplate.load(PersonalAddressDO.class, obj.getId(), LockMode.PESSIMISTIC_WRITE); } if (dbObj == null) { dbObj = getByAddressId(obj.getAddressId()); } if (dbObj == null) { return false; } checkAccess(dbObj); Validate.isTrue(ObjectUtils.equals(dbObj.getAddressId(), obj.getAddressId())); obj.setId(dbObj.getId()); // Copy all values of modified user to database object. final ModificationStatus modified = dbObj.copyValuesFrom(obj, "owner", "address", "id"); if (modified == ModificationStatus.MAJOR) { dbObj.setLastUpdate(); log.info("Object updated: " + dbObj.toString()); } return true; }