Skip to content

Commit

Permalink
Add more generics to Guestbook related classes (IQSS#775)
Browse files Browse the repository at this point in the history
Changes include converting `Query` to `TypedQuery<...>`.
  • Loading branch information
bencomp committed Jul 31, 2015
1 parent 7dfd71a commit a3c5dd8
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 26 deletions.
4 changes: 2 additions & 2 deletions src/main/java/edu/harvard/iq/dataverse/Guestbook.java
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public Guestbook copyGuestbook(Guestbook source, Dataverse dataverse) {
newGuestbook.setNameRequired(source.isNameRequired());
newGuestbook.setPositionRequired(source.isPositionRequired());
newGuestbook.setInstitutionRequired(source.isInstitutionRequired());
newGuestbook.setCustomQuestions(new ArrayList());
newGuestbook.setCustomQuestions(new ArrayList<CustomQuestion>());
if (!source.getCustomQuestions().isEmpty()) {
for (CustomQuestion sq: source.getCustomQuestions()){
CustomQuestion target = new CustomQuestion();
Expand All @@ -163,7 +163,7 @@ public Guestbook copyGuestbook(Guestbook source, Dataverse dataverse) {
target.setDisplayOrder(sq.getDisplayOrder());
target.setQuestionString(sq.getQuestionString());
if(!sq.getCustomQuestionValues().isEmpty()){
target.setCustomQuestionValues(new ArrayList());
target.setCustomQuestionValues(new ArrayList<CustomQuestionValue>());
for (CustomQuestionValue scqv: sq.getCustomQuestionValues()){
CustomQuestionValue newVal = new CustomQuestionValue();
newVal.setValueString(scqv.getValueString());
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/edu/harvard/iq/dataverse/GuestbookPage.java
Original file line number Diff line number Diff line change
Expand Up @@ -129,15 +129,15 @@ public void init() {
}
guestbook.setDataverse(dataverse);
if (guestbook.getCustomQuestions() == null || guestbook.getCustomQuestions().isEmpty()) {
guestbook.setCustomQuestions(new ArrayList());
guestbook.setCustomQuestions(new ArrayList<CustomQuestion>());
initCustomQuestion();
}
} else if (ownerId != null && editMode.equals(GuestbookPage.EditMode.CREATE)) {
// create mode for a new template
dataverse = dataverseService.find(ownerId);
guestbook = new Guestbook();
guestbook.setDataverse(dataverse);
guestbook.setCustomQuestions(new ArrayList());
guestbook.setCustomQuestions(new ArrayList<CustomQuestion>());
initCustomQuestion();
} else if (ownerId != null && sourceId != null && editMode.equals(GuestbookPage.EditMode.CLONE)) {
// create mode for a new template
Expand All @@ -149,7 +149,7 @@ public void init() {
guestbook.setUsageCount(new Long(0));
guestbook.setCreateTime(new Timestamp(new Date().getTime()));
if (guestbook.getCustomQuestions() == null || guestbook.getCustomQuestions().isEmpty()) {
guestbook.setCustomQuestions(new ArrayList());
guestbook.setCustomQuestions(new ArrayList<CustomQuestion>());
initCustomQuestion();
}

Expand All @@ -170,7 +170,7 @@ public List<GuestbookResponse> getGuestbookResponses(){
private void initCustomQuestion(){
CustomQuestion toAdd = new CustomQuestion();
toAdd.setQuestionType("text");
toAdd.setCustomQuestionValues(new ArrayList());
toAdd.setCustomQuestionValues(new ArrayList<CustomQuestionValue>());
toAdd.setGuestbook(guestbook);
int index = guestbook.getCustomQuestions().size();
guestbook.addCustomQuestion(index, toAdd);
Expand All @@ -179,7 +179,7 @@ private void initCustomQuestion(){
public void addCustomQuestion(Integer indexIn){
CustomQuestion toAdd = new CustomQuestion();
toAdd.setQuestionType("text");
toAdd.setCustomQuestionValues(new ArrayList());
toAdd.setCustomQuestionValues(new ArrayList<CustomQuestionValue>());
toAdd.setGuestbook(guestbook);
guestbook.addCustomQuestion(indexIn, toAdd);
}
Expand All @@ -198,7 +198,7 @@ public void removeCustomQuestionValue(CustomQuestion cq, Long index){
public void toggleQuestionType(CustomQuestion questionIn) {
if (questionIn.getCustomQuestionValues() != null && questionIn.getCustomQuestionValues().isEmpty()
&& questionIn.getQuestionType() !=null && questionIn.getQuestionType().equals("options")){
questionIn.setCustomQuestionValues(new ArrayList());
questionIn.setCustomQuestionValues(new ArrayList<CustomQuestionValue>());
CustomQuestionValue addCQV = new CustomQuestionValue();
addCQV.setCustomQuestion(questionIn);
questionIn.getCustomQuestionValues().add(addCQV);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public GuestbookResponse(GuestbookResponse source){
this.setDatasetVersion(source.getDatasetVersion());
this.setAuthenticatedUser(source.getAuthenticatedUser());
this.setSessionId(source.getSessionId());
List <CustomQuestionResponse> customQuestionResponses = new ArrayList();
List <CustomQuestionResponse> customQuestionResponses = new ArrayList<>();
if (!source.getCustomQuestionResponses().isEmpty()){
for (CustomQuestionResponse customQuestionResponse : source.getCustomQuestionResponses() ){
CustomQuestionResponse customQuestionResponseAdd = new CustomQuestionResponse();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.TypedQuery;

/**
*
Expand All @@ -33,7 +34,7 @@ public class GuestbookResponseServiceBean {
private EntityManager em;

public List<GuestbookResponse> findAll() {
return em.createQuery("select object(o) from GuestbookResponse as o order by o.responseTime desc").getResultList();
return em.createQuery("select object(o) from GuestbookResponse as o order by o.responseTime desc", GuestbookResponse.class).getResultList();
}

public List<Long> findAllIds() {
Expand All @@ -42,16 +43,16 @@ public List<Long> findAllIds() {

public List<Long> findAllIds(Long dataverseId) {
if (dataverseId == null) {
return em.createQuery("select o.id from GuestbookResponse as o order by o.responseTime desc").getResultList();
return em.createQuery("select o.id from GuestbookResponse as o order by o.responseTime desc", Long.class).getResultList();
}
return em.createQuery("select o.id from GuestbookResponse o, Dataset d where o.dataset.id = d.id and d.owner.id = " + dataverseId + " order by o.responseTime desc").getResultList();
return em.createQuery("select o.id from GuestbookResponse o, Dataset d where o.dataset.id = d.id and d.owner.id = " + dataverseId + " order by o.responseTime desc", Long.class).getResultList();
}

public List<GuestbookResponse> findAllByGuestbookId(Long guestbookId) {

if (guestbookId == null) {
} else {
return em.createQuery("select o from GuestbookResponse as o where o.guestbook.id = " + guestbookId + " order by o.responseTime desc").getResultList();
return em.createQuery("select o from GuestbookResponse as o where o.guestbook.id = " + guestbookId + " order by o.responseTime desc", GuestbookResponse.class).getResultList();
}
return null;
}
Expand All @@ -64,7 +65,7 @@ public Long findCountByGuestbookId(Long guestbookId) {
Query query = em.createNativeQuery(queryString);
return (Long) query.getSingleResult();
}
return new Long(0);
return 0L;
}

public List<Long> findAllIds30Days() {
Expand All @@ -88,9 +89,8 @@ public List<Long> findAllIds30Days(Long dataverseId) {
queryString += " o.responseTime >='" + beginTime + "'";
queryString += " and o.responseTime<='" + endTime + "'";
queryString += " order by o.responseTime desc";
Query query = em.createQuery(queryString);

return query.getResultList();
return em.createQuery(queryString, Long.class).getResultList();
}

public Long findCount30Days() {
Expand Down Expand Up @@ -134,7 +134,7 @@ public Long findCountAll(Long dataverseId) {
}

public List<GuestbookResponse> findAllByDataverse(Long dataverseId) {
return em.createQuery("select object(o) from GuestbookResponse o, Dataset d where o.dataset.id = d.id and d.owner.id = " + dataverseId + " order by o.responseTime desc").getResultList();
return em.createQuery("select object(o) from GuestbookResponse o, Dataset d where o.dataset.id = d.id and d.owner.id = " + dataverseId + " order by o.responseTime desc", GuestbookResponse.class).getResultList();
}

public List<GuestbookResponse> findAllWithin30Days() {
Expand All @@ -158,7 +158,7 @@ public List<GuestbookResponse> findAllWithin30Days(Long dataverseId) {
queryString += " o.responseTime >='" + beginTime + "'";
queryString += " and o.responseTime<='" + endTime + "'";
queryString += " order by o.responseTime desc";
Query query = em.createQuery(queryString);
TypedQuery<GuestbookResponse> query = em.createQuery(queryString, GuestbookResponse.class);

return query.getResultList();
}
Expand All @@ -180,12 +180,12 @@ private String generateTempTableString(List<Long> datasetIds) {
return "select tempid from tempid";
}

private String generateIDsforTempInsert(List idList) {
private String generateIDsforTempInsert(List<Long> idList) {
int count = 0;
StringBuffer sb = new StringBuffer();
Iterator iter = idList.iterator();
Iterator<Long> iter = idList.iterator();
while (iter.hasNext()) {
Long id = (Long) iter.next();
Long id = iter.next();
sb.append("(").append(id).append(",").append(count++).append(")");
if (iter.hasNext()) {
sb.append(",");
Expand Down Expand Up @@ -218,7 +218,7 @@ public List<Object[]> findDownloadInfoAll(List<Long> gbrIds) {
+ " vdc.name, s.protocol, s.authority, m.title, fmd.label, gbr.responsetime, gbr.position, gbr.study_id, gbr.id, s.id, gbr.downloadType "
+ "order by s.id, gbr.id";
System.out.print(gbrDownloadQueryString);
Query query = em.createNativeQuery(gbrDownloadQueryString);
TypedQuery<Object[]> query = em.createQuery(gbrDownloadQueryString, Object[].class);

return convertIntegerToLong(query.getResultList(), 14);
}
Expand All @@ -231,19 +231,18 @@ public List<Object[]> findCustomResponsePerGuestbookResponse(Long gbrId) {
+ " and gbr.id = cqr.guestbookresponse_id "
+ "and cq.id = cqr.customquestion_id "
+ " and cqr.guestbookresponse_id = " + gbrId;
Query query = em.createNativeQuery(gbrCustomQuestionQueryString);
TypedQuery<Object[]> query = em.createQuery(gbrCustomQuestionQueryString, Object[].class);

return convertIntegerToLong(query.getResultList(), 1);
}

private Guestbook findDefaultGuestbook() {
Guestbook guestbook = new Guestbook();
String queryStr = "SELECT object(o) FROM Guestbook as o WHERE o.dataverse.id = null";
Query query = em.createQuery(queryStr);
List resultList = query.getResultList();
List<Guestbook> resultList = em.createQuery(queryStr, Guestbook.class).getResultList();

if (resultList.size() >= 1) {
guestbook = (Guestbook) resultList.get(0);
guestbook = resultList.get(0);
}
return guestbook;

Expand Down

0 comments on commit a3c5dd8

Please sign in to comment.