Skip to content

Commit

Permalink
GuideStatistic class Implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
YuriPushchalo committed Oct 27, 2016
1 parent 58cf98f commit 2d8cdf5
Show file tree
Hide file tree
Showing 9 changed files with 279 additions and 99 deletions.
4 changes: 2 additions & 2 deletions .project
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@
</arguments>
</buildCommand>
<buildCommand>
<name>org.springframework.ide.eclipse.core.springbuilder</name>
<name>org.hibernate.eclipse.console.hibernateBuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.hibernate.eclipse.console.hibernateBuilder</name>
<name>org.springframework.ide.eclipse.core.springbuilder</name>
<arguments>
</arguments>
</buildCommand>
Expand Down
7 changes: 5 additions & 2 deletions src/com/softserve/museum/dao/generic/GuideDAO.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.util.List;

import com.softserve.museum.domain.Guide;
import com.softserve.museum.domain.GuideStatisticDTO;
import com.softserve.museum.domain.Position;

/**
Expand All @@ -30,9 +31,11 @@ public interface GuideDAO extends GenericDAO<Guide, Integer> {

public abstract List<Guide> findByTime(Time start, Time end);

@SuppressWarnings("rawtypes")
public abstract List<GuideStatisticDTO> getGuidesStatisticByPeriod(LocalDateTime start, LocalDateTime end);

/* @SuppressWarnings("rawtypes")
public abstract List getCountGuidesByPeriod(LocalDateTime start, LocalDateTime end);

*/
@SuppressWarnings("rawtypes")
public abstract List getCountTotalTimePerGuideByPeriod(LocalDateTime start,
LocalDateTime end);
Expand Down
67 changes: 53 additions & 14 deletions src/com/softserve/museum/dao/impl/GuideDAOimpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
*/
package com.softserve.museum.dao.impl;

import java.time.LocalTime;
import java.sql.Time;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

import javax.transaction.Transactional;
Expand All @@ -22,6 +24,7 @@

import com.softserve.museum.dao.generic.GuideDAO;
import com.softserve.museum.domain.Guide;
import com.softserve.museum.domain.GuideStatisticDTO;
import com.softserve.museum.domain.Position;

/**
Expand Down Expand Up @@ -55,16 +58,39 @@ public List<Guide> findByTime(LocalDateTime start, LocalDateTime end) {
return result.list();
}

@SuppressWarnings("rawtypes")
@SuppressWarnings("unchecked")
@Override
public List getCountGuidesByPeriod(LocalDateTime start, LocalDateTime end) {
public List<GuideStatisticDTO> getGuidesStatisticByPeriod(LocalDateTime start, LocalDateTime end) {
StringBuilder query = new StringBuilder(
"SELECT gd.id, gd.firstName, gd.lastName, gd.position, COUNT(gd) AS cnt FROM Excursion AS XS RIGHT JOIN XS.guide AS gd "
+ "WHERE (XS.start >= :startDate AND XS.end <= :endDate) " + "GROUP BY gd");
"SELECT XS.guide.firstName AS firstName, XS.guide.lastName AS lastName, COUNT(id) AS totalExcursions, "
+ "SUM(XS.details.durationsec) AS excursionsTotalDuration " + "FROM Excursion AS XS "
+ "WHERE (XS.start >= :startDate AND XS.end <= :endDate) GROUP BY XS.guide");
/*
* "SELECT gd.firstName, gd.lastName, COUNT(gd) AS totalExcursions, " +
* "SEC_TO_TIME(SUM(timestampdiff(SECOND, XS.start, XS.end))) AS excursionsTotalDuration "
* + "FROM Excursion AS XS RIGHT JOIN XS.guide AS gd " +
* "WHERE (XS.start >= :startDate AND XS.end <= :endDate) GROUP BY gd");
*/
System.out.println(query.toString());
// Session curSess = sessionFactory.getCurrentSession();
// System.out.println(curSess);
// Query result = curSess.createQuery(query.toString());
Query result = sessionFactory.getCurrentSession().createQuery(query.toString());
result.setTimestamp("startDate", Timestamp.valueOf(start));
result.setTimestamp("endDate", Timestamp.valueOf(end));
return result.list();
List<GuideStatisticDTO> returnResult = new ArrayList<GuideStatisticDTO>();
for (Object[] obj : (List<Object[]>) result.list()) {
GuideStatisticDTO listElement = new GuideStatisticDTO();
listElement.setFirstName(String.valueOf(obj[0]));
listElement.setLastName(String.valueOf(obj[1]));
listElement.setTotalExcursions((long) obj[2]);
listElement.setExcursionsTotalDuration(LocalTime.MIN.plusSeconds((long) obj[3]));
returnResult.add(listElement);

}

return returnResult;
// return result.list();
}

@SuppressWarnings("rawtypes")
Expand All @@ -79,13 +105,14 @@ public List getCountTotalTimePerGuideByPeriod(LocalDateTime start, LocalDateTime
return result.list();
}

@SuppressWarnings("unchecked")
@Override
public List<Guide> findByPosition(Position thePosition) {
Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Guide.class);
criteria.add(Restrictions.eq("position", thePosition));
return criteria.list();
}
@SuppressWarnings("unchecked")
@Override
public List<Guide> findByPosition(Position thePosition) {
Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Guide.class);
criteria.add(Restrictions.eq("position", thePosition));
return criteria.list();
}

@SuppressWarnings("unchecked")
@Override
public List<Guide> findByTime(Time start, Time end) {
Expand All @@ -98,6 +125,18 @@ public List<Guide> findByTime(Time start, Time end) {
Query result = sessionFactory.getCurrentSession().createQuery(query.toString());

return result.list();
}
}

}
/*
* public List<GuideStatisticDTO> getCountGuidesByPeriod(LocalDateTime start,
* LocalDateTime end) { StringBuilder query = new StringBuilder(
* "SELECT gd.firstName, gd.lastName, COUNT(gd) AS totalExcursions, " +
* "SEC_TO_TIME(SUM(timestampdiff(SECOND, XS.start, XS.end))) AS excursionsTotalDuration "
* + "FROM Excursion AS XS RIGHT JOIN XS.guide AS gd " +
* "WHERE (XS.start >= :startDate AND XS.end <= :endDate) " + "GROUP BY gd");
* Query result =
* sessionFactory.getCurrentSession().createQuery(query.toString());
* result.setTimestamp("startDate", Timestamp.valueOf(start));
* result.setTimestamp("endDate", Timestamp.valueOf(end)); return result.list();
*/
4 changes: 2 additions & 2 deletions src/com/softserve/museum/domain/Excursion.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ public class Excursion {
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;

@Column
@Column(name = "start")
@Convert(converter = LocalDateTimeConverter.class)
private LocalDateTime start;

@Column
@Column(name = "end")
@Convert(converter = LocalDateTimeConverter.class)
private LocalDateTime end;

Expand Down
10 changes: 10 additions & 0 deletions src/com/softserve/museum/domain/ExcursionDetails.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ public class ExcursionDetails {
@Convert(converter = LocalTimeConverter.class)
private LocalTime duration;

@Column
private long durationSec;

/**
* Default no-args constructor
*/
Expand Down Expand Up @@ -73,4 +76,11 @@ public final void setDuration(LocalTime duration) {
this.duration = duration;
}

public final long getDurationSec() {
return durationSec;
}

public final void setDuration(long duration) {
this.durationSec = duration;
}
}
77 changes: 77 additions & 0 deletions src/com/softserve/museum/domain/GuideStatisticDTO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* This is a simple web application utilizing Spring MVC and Hibernate.
* Developed by Lv-205 group of Softserve Academy.
*
* Copyright (c) 1993-2016 Softserve, Inc.
* This software is the confidential and proprietary information of Softserve.
*
*/

/**
*
* Proxy class, represents museum's guide statistic results.
*
* @author Yuri Pushchalo
* @version 1.0
* @since 27.10.2016
*
*/
package com.softserve.museum.domain;

import java.time.LocalTime;

public class GuideStatisticDTO {

private String firstName;

private String lastName;

private long totalExcursions;

private LocalTime excursionsTotalDuration;

/**
* Default no-args constructor
*/
public GuideStatisticDTO() {
}

public String getFirstName() {
return firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public String getLastName() {
return lastName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}

public long getTotalExcursions() {
return totalExcursions;
}

public void setTotalExcursions(long totalExcursions) {
this.totalExcursions = totalExcursions;
}

public LocalTime getExcursionsTotalDuration() {
return excursionsTotalDuration;
}

public void setExcursionsTotalDuration(LocalTime excursionsTotalDuration) {
this.excursionsTotalDuration = excursionsTotalDuration;
}

@Override
public String toString() {
return "Guide [First Name=" + firstName + ", " +
"Last Name=" + lastName + ", " + "Total Excursions Number=" + totalExcursions +
"Excursion Total Duration=" + excursionsTotalDuration + "]";
}
}
7 changes: 5 additions & 2 deletions src/com/softserve/museum/service/GuideService.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.util.List;

import com.softserve.museum.domain.Guide;
import com.softserve.museum.domain.GuideStatisticDTO;

/**
*
Expand Down Expand Up @@ -66,9 +67,11 @@ public interface GuideService {
*/
public abstract List<Guide> findByTime(String start, String end);

@SuppressWarnings("rawtypes")
public abstract List getCountGuidesByPeriod(LocalDateTime start, LocalDateTime end);
public abstract List<GuideStatisticDTO> getGuidesStatisticByPeriod(LocalDateTime start, LocalDateTime end);

/* @SuppressWarnings("rawtypes")
public abstract List getCountGuidesByPeriod(LocalDateTime start, LocalDateTime end);
*/
@SuppressWarnings("rawtypes")
public abstract List getCountTotalTimePerGuideByPeriod(LocalDateTime start, LocalDateTime end);

Expand Down
Loading

0 comments on commit 2d8cdf5

Please sign in to comment.