01: /*
02: * Copyright (c) Mateusz Prokopowicz. All Rights Reserved.
03: */
04:
05: package com.technoetic.xplanner.domain.repository;
06:
07: import java.util.Iterator;
08: import java.util.List;
09:
10: import net.sf.hibernate.Hibernate;
11: import net.sf.hibernate.HibernateException;
12: import net.sf.hibernate.type.Type;
13:
14: import com.technoetic.xplanner.domain.DomainObject;
15: import com.technoetic.xplanner.domain.Person;
16: import com.technoetic.xplanner.security.SecurityHelper;
17: import com.technoetic.xplanner.security.auth.Permission;
18:
19: /**
20: * User: mprokopowicz
21: * Date: Feb 15, 2006
22: * Time: 4:09:12 PM
23: */
24: public class PersonHibernateObjectRepository extends
25: HibernateObjectRepository {
26: static final String CHECK_PERSON_UNIQUENESS_QUERY = "com.technoetic.xplanner.domain.CheckPersonUniquenessQuery";
27:
28: public PersonHibernateObjectRepository(Class objectClass)
29: throws HibernateException {
30: super (objectClass);
31: }
32:
33: public int insert(DomainObject domainObject)
34: throws RepositoryException {
35: Person person = (Person) domainObject;
36: checkPersonUniquness(person);
37: int id = super .insert(person);
38: setUpEditPermission(person);
39: return id;
40: }
41:
42: public void update(DomainObject domainObject)
43: throws DuplicateUserIdException {
44: Person person = (Person) domainObject;
45: checkPersonUniquness(person);
46: super .update(person);
47: }
48:
49: public void delete(final int objectIdentifier)
50: throws RepositoryException {
51: super .delete(objectIdentifier);
52: getHibernateTemplate().delete(
53: "from " + Permission.class.getName()
54: + " where resource_id = ?",
55: new Integer(objectIdentifier), Hibernate.INTEGER);
56: }
57:
58: void setUpEditPermission(DomainObject person) {
59: getHibernateTemplate().save(
60: new Permission("system.person", person.getId(), person
61: .getId(), "edit%"));
62: // session.save(new Permission("system.person",person.getId(),person.getId(),"read%"));
63: getHibernateTemplate().save(
64: new Permission("system.person", 0, person.getId(),
65: "read%"));
66: // roleAssociationRepository.insertForPersonOnProject("viewer",person.getId(),0);
67: }
68:
69: void checkPersonUniquness(Person person)
70: throws DuplicateUserIdException {
71: List potentialDuplicatePeople = getHibernateTemplate()
72: .findByNamedQuery(
73: CHECK_PERSON_UNIQUENESS_QUERY,
74: new Object[] { new Integer(person.getId()),
75: person.getUserId() },
76: new Type[] { Hibernate.INTEGER,
77: Hibernate.STRING });
78:
79: Iterator iterator = potentialDuplicatePeople.iterator();
80: if (iterator.hasNext()) {
81: while (iterator.hasNext()) {
82: Person potentialDuplicatePerson = (Person) iterator
83: .next();
84: if (hasSameUserId(person, potentialDuplicatePerson)) {
85: throw new DuplicateUserIdException();
86: }
87: }
88: }
89: }
90:
91: boolean hasSameUserId(Person person1, Person person2) {
92: // if(SecurityHelper.isAuthenticationCaseSensitive())
93: // return person1.getUserId().equals(person2.getUserId());
94: // else
95: return person1.getUserId()
96: .equalsIgnoreCase(person2.getUserId());
97: }
98: }
|