Ändrade query som hämtar event, kör nu två querys, en för projectevents och en för groupevents. Fixade också så att man inte kan spara gruppevents där ett deltagarprojekt inte skapat ett schema än

git-svn-id: svn://svn.dsv.su.se/scipro/scipro/trunk@503 73ecded7-942e-4092-bab0-0e58ef0ee984
This commit is contained in:
dan-kjel 2011-03-28 12:14:31 +00:00
parent 747da4b351
commit ba86d1f14d
6 changed files with 87 additions and 63 deletions

@ -21,6 +21,6 @@ public interface ProjectDao extends Dao<Project>{
public List<Project> searchProjectByTitle(final String searchTerm);
public List<Project> searchProjectByTitle(final String searchTerm, final Integer limit);
public List<Project> searchProjectByTitle(final String searchTerm, final Integer limit, final boolean includeWithoutSchedule);
}

@ -14,6 +14,8 @@ import org.springframework.orm.jpa.JpaCallback;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import edu.emory.mathcs.backport.java.util.Collections;
import se.su.dsv.scipro.data.dao.interfaces.EventDao;
import se.su.dsv.scipro.data.dataobjects.Event;
import se.su.dsv.scipro.data.dataobjects.Project;
@ -37,12 +39,23 @@ public class EventDaoJPAImp extends LazyDeleteAbstractDaoJPAImp<Event> implement
return getJpaTemplate().execute(new JpaCallback<List<Event>>() {
public List<Event> doInJpa(EntityManager em) throws PersistenceException {
String selectClause = "select distinct e ";
String peq = "select e " +
"from ProjectEvent e ";
String fromClause = "from Event e, GroupEvent ge, ProjectEvent pe ";
String peqCond = "and e.projectSchedule.project = :project ";
if(u != null){
peq += ", User u " +
"join u.roles role " +
"join e.participants evPart ";
peqCond += "and u = :user " +
"and evPart = role ";
}
if(isDone != null){
peqCond += "and e.done = :isDone ";
}
String joinClause = "join ge.projectSchedules ps ";
String conditions = "where e.deleted = false ";
if(fromDate != null) {
@ -53,62 +66,56 @@ public class EventDaoJPAImp extends LazyDeleteAbstractDaoJPAImp<Event> implement
conditions += "and e.dueDate < :toDate ";
}
String projectEventConditions = "e = pe " +
"and pe.projectSchedule.project = :project ";
String geq = "select e " +
"from GroupEvent e " +
"join e.projectSchedules ps ";
String geqCond = "and ps.project = :project ";
String orderby = "order by e.dueDate asc ";
String peQueryString = peq + conditions + peqCond + orderby;
String geQueryString = geq + conditions + geqCond + orderby;
TypedQuery<Event> peQuery = em.createQuery(peQueryString, Event.class);
TypedQuery<Event> geQuery = em.createQuery(geQueryString, Event.class);
peQuery.setParameter("project", p);
geQuery.setParameter("project", p);
if(u != null){
fromClause += ", User u ";
joinClause += "join u.roles role " +
"join pe.participants evPart ";
conditions += "and u = :user ";
projectEventConditions += "and evPart = role ";
}
if(isDone != null){
projectEventConditions += "and pe.done = :isDone ";
}
String groupEventConditions = "e = ge " +
"and ps.project = :project ";
String q = selectClause +
fromClause +
joinClause +
conditions +
"and ((" + projectEventConditions + ") or " + "(" + groupEventConditions + ")) ";
q += "order by e.dueDate asc ";
TypedQuery<Event> query = em.createQuery(q, Event.class);
query.setParameter("project", p);
if(u != null){
query.setParameter("user", u);
peQuery.setParameter("user", u);
}
if(isDone != null){
query.setParameter("isDone", isDone);
peQuery.setParameter("isDone", isDone);
}
if(fromDate != null) {
query.setParameter("fromDate", fromDate);
peQuery.setParameter("fromDate", fromDate);
geQuery.setParameter("fromDate", fromDate);
}
if(toDate != null){
query.setParameter("toDate", toDate);
peQuery.setParameter("toDate", toDate);
geQuery.setParameter("toDate", toDate);
}
if(limit != null){
query.setMaxResults(limit);
}
List<Event> resultList = new ArrayList<Event>();
try{
return query.getResultList();
List<Event> eventList = peQuery.getResultList();
resultList.addAll(eventList);
} catch (NoResultException e) {
return new ArrayList<Event>();
}
try{
List<Event> eventList = geQuery.getResultList();
resultList.addAll(eventList);
} catch (NoResultException e) {
}
Collections.sort(resultList);
return resultList;
}
});
}

@ -184,21 +184,27 @@ public class ProjectDaoJPAImp extends AbstractDaoJPAImp<Project> implements Proj
}
@Transactional
public List<Project> searchProjectByTitle(final String searchTerm, final Integer limit) {
public List<Project> searchProjectByTitle(final String searchTerm, final Integer limit, final boolean includeWithoutSchedule) {
return getJpaTemplate().execute(new JpaCallback<List<Project>>() {
public List<Project> doInJpa(EntityManager em)
throws PersistenceException {
String q = "select p " +
"from Project p " +
"where upper(p.title) LIKE upper(:searchTerm) " +
"order by p.title ";
"where upper(p.title) LIKE upper(:searchTerm) ";
if(!includeWithoutSchedule){
q += "and p.projectSchedule != null ";
}
q += "order by p.title ";
TypedQuery<Project> query = em.createQuery(q, Project.class);
query.setParameter("searchTerm", "%" + searchTerm.trim() + "%");
query.setParameter("searchTerm", searchTerm.trim() + "%");
if(limit != null){
query.setMaxResults(limit);
}
System.out.println(q);
try {
return query.getResultList();
} catch (NoResultException e) {
@ -209,6 +215,6 @@ public class ProjectDaoJPAImp extends AbstractDaoJPAImp<Project> implements Proj
}
public List<Project> searchProjectByTitle(final String searchTerm) {
return searchProjectByTitle(searchTerm, null);
return searchProjectByTitle(searchTerm, null, true);
}
}

@ -52,7 +52,7 @@ public class GroupEventForm extends EventForm<GroupEventFormModel>{
@Override
public Iterator<Project> getChoices(String input) {
return projectDao.searchProjectByTitle(input, 10).iterator();
return projectDao.searchProjectByTitle(input, 10, true).iterator();
}
};
@ -105,6 +105,12 @@ public class GroupEventForm extends EventForm<GroupEventFormModel>{
error("The event must have at least one participant");
}
for(Project p : model.getEventProjects()){
if(p.getProjectSchedule() == null){
error(p.getTitle() + " does not have a schedule, please remove the project");
}
}
if(!form.hasError()){
model.persist();
target.addComponent(WiQueryCoreEffectsHelper.fadeIn(parent.getListContainer(), EffectSpeed.FAST));

@ -92,15 +92,18 @@ public class EventPanel extends Panel {
@Override
public boolean isVisible(){
User loggedInUser = SciProSession.get().getUser();
if(event.getDueDate().before(new Date())){
return SciProSession.get().authorizedForRole(Roles.SYSADMIN);
if(SciProSession.get().authorizedForRole(Roles.SYSADMIN)){
return true;
}
if(event.getDueDate().before(new Date())){
return false;
}
if(event instanceof GroupEvent){
User loggedInUser = SciProSession.get().getUser();
if(loggedInUser.equals(event.getCreator())){
return true;
}
return SciProSession.get().authorizedForRole(Roles.SYSADMIN);
}
}
return true;
}
@ -118,15 +121,18 @@ public class EventPanel extends Panel {
@Override
public boolean isVisible(){
User loggedInUser = SciProSession.get().getUser();
if(event.getDueDate().before(new Date())){
return SciProSession.get().authorizedForRole(Roles.SYSADMIN);
if(SciProSession.get().authorizedForRole(Roles.SYSADMIN)){
return true;
}
if(event.getDueDate().before(new Date())){
return false;
}
if(event instanceof GroupEvent){
if(event.getCreator().equals(loggedInUser)){
User loggedInUser = SciProSession.get().getUser();
if(loggedInUser.equals(event.getCreator())){
return true;
}
return SciProSession.get().authorizedForRole(Roles.SYSADMIN);
}
}
return true;
}

@ -235,7 +235,6 @@ public class SchedulePlannerPanel extends Panel implements ISchedulePlannerPanel
}
public WebMarkupContainer getListContainer(){
eventsModel.reload();
return eventListContainer;
}