Skip to content

Commit

Permalink
Merge branch 'master' into Production
Browse files Browse the repository at this point in the history
  • Loading branch information
jezekp committed May 29, 2014
2 parents 2160e1e + e010440 commit bea8b2b
Show file tree
Hide file tree
Showing 79 changed files with 4,382 additions and 222 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,6 @@ public interface ResearchGroupDao extends GenericDao<ResearchGroup, Integer> {
public int getCountForList();

List<ResearchGroup> getGroupsForList(int start, int limit);

ResearchGroup getResearchGroupById(int id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public SimpleArticleCommentDao() {
public List<ArticleComment> getCommentsForArticle(int articleId) {
String query = "select distinct c from ArticleComment c left join fetch c.children join fetch c.person " +
"where " +
"c.article.id = :id " +
"c.article.id = :id and c.parent is null " +
"order by c.time desc";
return getSessionFactory().getCurrentSession().createQuery(query).setParameter("id", articleId).list();
}
Expand Down
62 changes: 34 additions & 28 deletions src/main/java/cz/zcu/kiv/eegdatabase/data/dao/SimpleArticleDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,11 @@

package cz.zcu.kiv.eegdatabase.data.dao;

import java.util.Collections;
import java.util.List;

import cz.zcu.kiv.eegdatabase.data.pojo.Article;
import cz.zcu.kiv.eegdatabase.data.pojo.Person;
import org.hibernate.Hibernate;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
* @author Jiri Vlasimsky
Expand All @@ -54,51 +51,62 @@ public List<Article> getAllArticles() {
@Override
public List<Article> getArticlesForUser(Person person) {
String query;
List articles = null;
List articles = Collections.EMPTY_LIST;

if (person.getAuthority().equals("ROLE_ADMIN")) {
// We can simply load the newest articles
query = "from Article a left join fetch a.researchGroup r " +
"order by a.time desc";
articles = getHibernateTemplate().find(query);
query = "select DISTINCT a from Article a left join fetch a.articleComments order by a.time desc";
articles = getSession().createQuery(query).list();
} else {
// We need to load only articles which can be viewed by the logged user.
// That is, we need to load only public articles or articles from the groups the logged user is member of.
query = "from Article a left join fetch a.researchGroup r " +
"where " +
query = "select DISTINCT a from Article a left join fetch a.articleComments where " +
"a.researchGroup.researchGroupId is null or " +
"a.researchGroup.researchGroupId in " +
"(select rm.id.researchGroupId from ResearchGroupMembership rm where rm.id.personId = :personId) " +
"order by a.time desc";
articles = getHibernateTemplate().findByNamedParam(query, "personId", person.getPersonId());
articles = getSession().createQuery(query).setParameter("personId", person.getPersonId()).list();
}

return articles;
}

@Override
public List<Article> getArticlesForUser(Person person, int limit) {
getHibernateTemplate().setMaxResults(limit);
List<Article> articles = getArticlesForUser(person);
getHibernateTemplate().setMaxResults(0);
String query;
List articles = Collections.EMPTY_LIST;

if (person.getAuthority().equals("ROLE_ADMIN")) {
// We can simply load the newest articles
query = "select DISTINCT a from Article a left join fetch a.articleComments order by a.time desc";
articles = getSession().createQuery(query).setMaxResults(limit).list();
} else {
// We need to load only articles which can be viewed by the logged user.
// That is, we need to load only public articles or articles from the groups the logged user is member of.
query = "select DISTINCT a from Article a left join fetch a.articleComments where " +
"a.researchGroup.researchGroupId is null or " +
"a.researchGroup.researchGroupId in " +
"(select rm.id.researchGroupId from ResearchGroupMembership rm where rm.id.personId = :personId) " +
"order by a.time desc";
articles = getSession().createQuery(query).setParameter("personId", person.getPersonId()).setMaxResults(limit).list();
}

return articles;
}

@Override
public List<Article> getArticlesForList(Person person, int min, int count) {
String query;
List articles = null;
List articles = Collections.EMPTY_LIST;

if (person.getAuthority().equals("ROLE_ADMIN")) {
// We can simply load the newest articles
query = "from Article a left join fetch a.researchGroup r join fetch a.person p " +
"order by a.time desc";
query = "from Article a order by a.time desc";
articles = getSession().createQuery(query).setFirstResult(min).setMaxResults(count).list();
} else {
// We need to load only articles which can be viewed by the logged user.
// That is, we need to load only public articles or articles from the groups the logged user is member of.
query = "from Article a left join fetch a.researchGroup r join fetch a.person p " +
"where " +
query = "from Article a where " +
"a.researchGroup.researchGroupId is null or " +
"a.researchGroup.researchGroupId in " +
"(select rm.id.researchGroupId from ResearchGroupMembership rm where rm.id.personId = :personId) " +
Expand All @@ -114,7 +122,7 @@ public int getArticleCountForPerson(Person person) {
if (person.getAuthority().equals("ROLE_ADMIN")) {
return ((Long) getSession().createQuery("select count(*) from Article").uniqueResult()).intValue();
}
String query = "select count(*) from Article a left join a.person p where " +
String query = "select count(*) from Article a where " +
"a.researchGroup.researchGroupId is null or " +
"a.researchGroup.researchGroupId in " +
"(select rm.id.researchGroupId from ResearchGroupMembership rm where rm.id.personId = :personId)";
Expand All @@ -133,14 +141,12 @@ public int getArticleCountForPerson(Person person) {
public Article getArticleDetail(int id, Person loggedPerson) {

if (loggedPerson.getAuthority().equals("ROLE_ADMIN")) {
String query = "from Article a join fetch a.person left join fetch a.researchGroup " +
"where " +
"a.articleId = :id";
String query = "from Article a left join fetch a.subscribers left join fetch a.articleComments "
+ "where a.articleId = :id";
return (Article) getSession().createQuery(query).setParameter("id", id).uniqueResult();
} else {
String query = "from Article a join fetch a.person left join fetch a.researchGroup " +
"where " +
"a.articleId = :id and (" +
String query = "from Article a left join fetch a.subscribers left join fetch a.articleComments "+
"where a.articleId = :id and (" +
"a.researchGroup.researchGroupId is null or " +
"a.researchGroup.researchGroupId in " +
"(select rm.id.researchGroupId from ResearchGroupMembership rm where rm.id.personId = :personId))";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,22 @@ public List<History> getHistory(ChoiceHistory historyType, boolean isGroupAdmin,
* @return string left join for hql query
*/
private String getLeftJoin(boolean isGroupAdmin, int groupId) {

String joinedTables = " left join fetch h.scenario left join fetch h.person as person left join fetch h.dataFile";

if (isGroupAdmin && groupId > 0) {
return " left join h.person.researchGroupMemberships m";
return joinedTables + " left join person.researchGroupMemberships m";
}
return "";
return joinedTables;
}

private String getLeftJoinForTopDownload(boolean isGroupAdmin, int groupId){
if (isGroupAdmin && groupId > 0) {
return " join h.person as person join person.researchGroupMemberships m";
}
return "";
}

/**
* Returns group condition for hql query
* @param isGroupAdmin - determined role GROUP_ADMIN
Expand All @@ -107,7 +118,7 @@ public long getCountOfFilesHistory(ChoiceHistory historyType, boolean isGroupAdm
List<DownloadStatistic> dCount = null;
String leftJoin = "";
String groupCondition = "";
leftJoin = getLeftJoin(isGroupAdmin, groupId);
leftJoin = getLeftJoinForTopDownload(isGroupAdmin, groupId);
whereCondition = getWhereCondition(historyType);
groupCondition = getGroupCondition(isGroupAdmin, groupId);

Expand Down Expand Up @@ -174,15 +185,15 @@ private String getWhereCondition(ChoiceHistory historyType) {
String whereCondition = "";
switch (historyType) {
case DAILY:
whereCondition = " where h.dateOfDownload > trunc(sysdate)";
whereCondition = " where h.dateOfDownload >= date_trunc('day', current_date)";
break;

case WEEKLY:
whereCondition = " where h.dateOfDownload >= trunc(sysdate, 'iw')";
whereCondition = " where h.dateOfDownload >= date_trunc('week', current_date)";
break;

case MONTHLY:
whereCondition = " where h.dateOfDownload > trunc(sysdate,'mm')";
whereCondition = " where h.dateOfDownload >= date_trunc('month', current_date)";
break;

default:
Expand All @@ -207,7 +218,7 @@ public List<DownloadStatistic> getTopDownloadHistory(ChoiceHistory historyType,
List<DownloadStatistic> topHistory = null;
String leftJoin = "";
String groupCondition = "";
leftJoin = getLeftJoin(isGroupAdmin, groupId);
leftJoin = getLeftJoinForTopDownload(isGroupAdmin, groupId);
groupCondition = getGroupCondition(isGroupAdmin, groupId);
String selectAndCreateObject = "select distinct new cz.zcu.kiv.eegdatabase.logic.controller.history.DownloadStatistic";
session = getHibernateTemplate().getSessionFactory().getCurrentSession();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,4 +170,10 @@ public List getGroupsForList(int start, int limit) {
public List<ResearchGroup> getAllRecordsFull() {
return super.getAllRecordsFull();
}

@Override
public ResearchGroup getResearchGroupById(int id) {
String query = "from ResearchGroup g left join fetch g.articlesSubscribers where g.researchGroupId = :id";
return (ResearchGroup) getSessionFactory().getCurrentSession().createQuery(query).setParameter("id", id).uniqueResult();
}
}
37 changes: 26 additions & 11 deletions src/main/java/cz/zcu/kiv/eegdatabase/data/pojo/Article.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,33 @@
*/
package cz.zcu.kiv.eegdatabase.data.pojo;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.Lob;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;

import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.Type;

import cz.zcu.kiv.eegdatabase.data.annotation.Indexed;
import cz.zcu.kiv.eegdatabase.data.annotation.SolrField;
import cz.zcu.kiv.eegdatabase.data.annotation.SolrId;
import cz.zcu.kiv.eegdatabase.logic.indexing.IndexField;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.Type;

import javax.persistence.*;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.HashSet;
import java.util.Set;

/**
* Article generated by hbm2java
Expand Down Expand Up @@ -92,7 +106,7 @@ public void setArticleId(int articleId) {
this.articleId = articleId;
}

@ManyToOne(fetch = FetchType.LAZY)
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "PERSON_ID", nullable = false)
public Person getPerson() {
return this.person;
Expand All @@ -102,7 +116,7 @@ public void setPerson(Person person) {
this.person = person;
}

@ManyToOne(fetch = FetchType.LAZY)
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "RESEARCH_GROUP_ID")
public ResearchGroup getResearchGroup() {
return this.researchGroup;
Expand Down Expand Up @@ -141,7 +155,8 @@ public void setTitle(String title) {
this.title = title;
}

@OneToMany(fetch = FetchType.EAGER, mappedBy = "article")
@OneToMany(fetch = FetchType.LAZY, mappedBy = "article")
@OrderBy("time ASC")
public Set<ArticleComment> getArticleComments() {
return this.articleComments;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,8 @@ public void setParent(ArticleComment parent) {
this.parent = parent;
}

@OneToMany(fetch = FetchType.LAZY, mappedBy = "parent")
@OneToMany(fetch = FetchType.EAGER, mappedBy = "parent")
@OrderBy("time ASC")
public Set<ArticleComment> getChildren() {
return this.children;
}
Expand All @@ -148,7 +149,7 @@ public void setTime(Timestamp time) {
this.time = time;
}

@ManyToOne(fetch = FetchType.LAZY)
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "PERSON_ID", nullable = false)
public Person getPerson() {
return this.person;
Expand Down
Loading

0 comments on commit bea8b2b

Please sign in to comment.