/**
   * Perform a search over all child and sub orgs within the given organisation
   *
   * <p>If a search query is given it will be split by space and the key words searched in the
   * title, orgid, address, addressLine2 and postcode fields.
   *
   * @param q - search query, which is a space seperated list of key words. Optional
   * @param organisation - search is for orgs inside this
   * @param orgType - optional, if given results are limited to organisations of this type
   * @param session
   * @return
   */
  public static List<Organisation> search(
      String q, Organisation organisation, OrgType orgType, Session session) {
    Criteria crit = session.createCriteria(Organisation.class);
    crit.setCacheable(true);
    Disjunction notDeleted = Restrictions.disjunction();
    notDeleted.add(Restrictions.isNull("deleted"));
    notDeleted.add(Restrictions.eq("deleted", Boolean.FALSE));
    crit.add(notDeleted);

    if (q != null) {
      String[] arr = q.split(" ");
      Conjunction con = Restrictions.conjunction();
      for (String queryPart : arr) {
        Disjunction dis = Restrictions.disjunction();
        String s = "%" + queryPart + "%";
        dis.add(Restrictions.ilike("title", s));
        dis.add(Restrictions.ilike("orgId", s));
        dis.add(Restrictions.ilike("address", s));
        dis.add(Restrictions.ilike("addressLine2", s));
        dis.add(Restrictions.ilike("postcode", s));
        con.add(dis);
      }
      crit.add(con);
    }

    if (orgType != null) {
      crit.add(Restrictions.eq("orgType", orgType));
    }
    // TODO: add other properties like address
    Criteria critParentLink = crit.createCriteria("parentOrgLinks");
    critParentLink.add(Restrictions.eq("owner", organisation));
    crit.addOrder(Order.asc("title"));
    return DbUtils.toList(crit, Organisation.class);
  }
 public static List<Organisation> findByOrgType(OrgType orgType, Session session) {
   Criteria crit = session.createCriteria(Organisation.class);
   crit.setCacheable(true);
   crit.add(Restrictions.eq("orgType", orgType));
   crit.addOrder(Order.asc("title"));
   return DbUtils.toList(crit, Organisation.class);
 }
Beispiel #3
0
 public static List<Group> findByOrg(Organisation org, Session session) {
   Criteria crit = session.createCriteria(Group.class);
   crit.setCacheable(true);
   crit.add(Restrictions.eq("organisation", org));
   crit.addOrder(Order.asc("name"));
   return DbUtils.toList(crit, Group.class);
 }
Beispiel #4
0
 public static List<Profile> findByGroup(Group group, Session session) {
   Criteria crit = session.createCriteria(Profile.class);
   crit.setCacheable(true);
   // join to group membership, then subordinate, then restrict on org
   Criteria critMembership = crit.createCriteria("memberships");
   critMembership.add(Restrictions.eq("groupEntity", group));
   return DbUtils.toList(crit, Profile.class);
 }
Beispiel #5
0
 public GroupMembership getGroupMembership(BaseEntity u, Organisation withinOrg, Session session) {
   Criteria crit = session.createCriteria(GroupMembership.class);
   crit.setCacheable(true);
   crit.add(Restrictions.eq("member", u));
   crit.add(Restrictions.eq("groupEntity", this));
   crit.add(Restrictions.eq("withinOrg", withinOrg));
   return DbUtils.unique(crit);
 }
Beispiel #6
0
 /**
  * Is the given entity a member of this group, regardless of organisation
  *
  * @param entity
  * @return
  */
 public boolean isMember(BaseEntity entity) {
   Criteria crit = SessionManager.session().createCriteria(GroupMembership.class);
   crit.setCacheable(true);
   crit.add(Restrictions.eq("member", entity));
   crit.add(Restrictions.eq("groupEntity", this));
   boolean b = !DbUtils.toList(crit, GroupMembership.class).isEmpty();
   return b;
 }
Beispiel #7
0
 public static List<Profile> findByBusinessUnit(Organisation organisation, Session session) {
   Criteria crit = session.createCriteria(Profile.class);
   crit.setCacheable(true);
   // join to group membership, then subordinate, then restrict on org
   Criteria critMembership = crit.createCriteria("memberships");
   critMembership.add(Restrictions.eq("withinOrg", organisation));
   return DbUtils.toList(crit, Profile.class);
 }
Beispiel #8
0
 public static Profile find(String name, Session session) {
   Criteria crit = session.createCriteria(Profile.class);
   crit.setCacheable(true);
   crit.add(
       Restrictions.disjunction()
           .add(Restrictions.eq("name", name))
           .add(Restrictions.eq("email", name)));
   return DbUtils.unique(crit);
 }
  /**
   * Find the given orgId within an administrative org
   *
   * @param adminOrg
   * @param orgId
   * @param session
   * @return
   */
  public static Organisation findByOrgId(Organisation adminOrg, String orgId, Session session) {
    Criteria crit = session.createCriteria(Organisation.class);
    Criteria critSubOrg = crit.createCriteria("parentOrgLinks");

    crit.setCacheable(true);
    crit.add(Restrictions.eq("orgId", orgId));
    critSubOrg.add(Restrictions.eq("owner", adminOrg));
    return DbUtils.unique(crit);
  }
Beispiel #10
0
 /**
  * Find a profile by email address, but only looking within the given organisation or subordinate
  * orgs
  *
  * @param email
  * @param org
  * @param session
  * @return
  */
 public static Profile findByEmail(String email, Organisation org, Session session) {
   Criteria crit = session.createCriteria(Profile.class);
   crit.setCacheable(true);
   // join to group membership, then subordinate, then restrict on org
   Criteria critMembership = crit.createCriteria("memberships");
   Criteria critSubordinate = critMembership.createCriteria("subordinates");
   crit.add(Restrictions.eq("email", email));
   critSubordinate.add(Restrictions.eq("withinOrg", org));
   return DbUtils.unique(crit);
 }
Beispiel #11
0
 public static Organisation getRootOrg(Session session) {
   Criteria crit = session.createCriteria(Organisation.class);
   crit.setCacheable(true);
   crit.add(Restrictions.isNull("organisation"));
   List<Organisation> list = DbUtils.toList(crit, Organisation.class);
   if (!list.isEmpty()) {
     return list.get(0);
   }
   return null;
 }
Beispiel #12
0
 public static String getUniqueName(final String baseName, Session session) {
   String name = baseName;
   Profile r = find(name, session);
   int cnt = 0;
   boolean isFirst = true;
   while (r != null) {
     cnt++;
     name = DbUtils.incrementFileName(name, isFirst);
     isFirst = false;
     r = find(name, session);
   }
   return name;
 }
Beispiel #13
0
 public static boolean isUniqueName(String name, Session session) {
   log.info("isUniqueName");
   long tm = System.currentTimeMillis();
   Criteria crit = session.createCriteria(BaseEntity.class);
   crit.add(Restrictions.eq("name", name));
   Object result = DbUtils.unique(crit);
   log.info(
       "isUniqueName name={} result={} durationMs={}",
       name,
       result,
       (System.currentTimeMillis() - tm));
   return result == null;
 }
Beispiel #14
0
 public static List<Commit> findByBranch(
     Branch branch, Date from, Date to, boolean orderReverseDate, Integer limit, Session session) {
   Criteria c = session.createCriteria(Commit.class);
   c.add(Restrictions.eq("branch", branch));
   if (from != null) {
     c.add(Restrictions.gt("createdDate", from));
   }
   if (to != null) {
     c.add(Restrictions.le("createdDate", to));
   }
   if (limit != null) {
     c.setMaxResults(limit);
   }
   if (orderReverseDate) {
     c.addOrder(Order.desc("createdDate"));
   } else {
     c.addOrder(Order.asc("createdDate"));
   }
   return DbUtils.toList(c, Commit.class);
 }
Beispiel #15
0
 public static List<Profile> findAll(Session session) {
   Criteria crit = session.createCriteria(Profile.class);
   crit.setCacheable(true);
   return DbUtils.toList(crit, Profile.class);
 }
Beispiel #16
0
 public static List<Commit> findByBranch(Branch branch, Session session) {
   Criteria c = session.createCriteria(Commit.class);
   c.add(Restrictions.eq("branch", branch));
   c.addOrder(Order.desc("createdDate"));
   return DbUtils.toList(c, Commit.class);
 }
Beispiel #17
0
 public static String findAutoName(String nickName, Session session) {
   String nameToUse = DbUtils.replaceYuckyChars(nickName);
   nameToUse = getUniqueName(nameToUse, session);
   return nameToUse;
 }
Beispiel #18
0
 public static Profile findByEmail(String email, Session session) {
   Criteria crit = session.createCriteria(Profile.class);
   crit.setCacheable(true);
   crit.add(Restrictions.eq("email", email));
   return DbUtils.unique(crit);
 }
Beispiel #19
0
 public static Organisation findByAdminDomain(String adminDomain, Session session) {
   Criteria crit = session.createCriteria(Organisation.class);
   crit.setCacheable(true);
   crit.add(Restrictions.eq("adminDomain", adminDomain));
   return DbUtils.unique(crit);
 }