diff --git a/fineract-provider/src/main/java/org/apache/fineract/spm/api/SpmApiResource.java b/fineract-provider/src/main/java/org/apache/fineract/spm/api/SpmApiResource.java index 46afed0ed..1e00a71f4 100644 --- a/fineract-provider/src/main/java/org/apache/fineract/spm/api/SpmApiResource.java +++ b/fineract-provider/src/main/java/org/apache/fineract/spm/api/SpmApiResource.java @@ -30,8 +30,10 @@ import javax.ws.rs.PUT; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; +import javax.ws.rs.QueryParam; import javax.ws.rs.core.MediaType; +import org.apache.fineract.infrastructure.core.exception.UnrecognizedQueryParamException; import org.apache.fineract.infrastructure.security.service.PlatformSecurityContext; import org.apache.fineract.spm.data.SurveyData; import org.apache.fineract.spm.domain.Survey; @@ -62,10 +64,15 @@ public class SpmApiResource { @Consumes({ MediaType.APPLICATION_JSON }) @Produces({ MediaType.APPLICATION_JSON }) @Transactional - public List fetchActiveSurveys() { + public List fetchAllSurveys(@QueryParam("isActive") final Boolean isActive) { this.securityContext.authenticatedUser(); final List result = new ArrayList<>(); - final List surveys = this.spmService.fetchValidSurveys(); + List surveys = null; + if(isActive != null && isActive){ + surveys = this.spmService.fetchValidSurveys(); + }else{ + surveys = this.spmService.fetchAllSurveys(); + } if (surveys != null) { for (final Survey survey : surveys) { result.add(SurveyMapper.map(survey)); @@ -110,14 +117,21 @@ public class SpmApiResource { return getResponse(survey.getId()); } - @DELETE + @POST @Path("/{id}") @Consumes({ MediaType.APPLICATION_JSON }) @Produces({ MediaType.APPLICATION_JSON }) @Transactional - public void deactivateSurvey(@PathParam("id") final Long id) { + public void activateOrDeactivateSurvey(@PathParam("id") final Long id, @QueryParam("command") final String command) { this.securityContext.authenticatedUser(); - this.spmService.deactivateSurvey(id); + if(command != null && command.equalsIgnoreCase("activate")){ + this.spmService.activateSurvey(id);; + }else if(command != null && command.equalsIgnoreCase("deactivate")){ + this.spmService.deactivateSurvey(id); + }else{ + throw new UnrecognizedQueryParamException("command", command); + } + } private String getResponse(Long id) { diff --git a/fineract-provider/src/main/java/org/apache/fineract/spm/exception/SurveyResponseNotAvailableException.java b/fineract-provider/src/main/java/org/apache/fineract/spm/exception/SurveyResponseNotAvailableException.java new file mode 100644 index 000000000..a194b6cf3 --- /dev/null +++ b/fineract-provider/src/main/java/org/apache/fineract/spm/exception/SurveyResponseNotAvailableException.java @@ -0,0 +1,30 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.fineract.spm.exception; + +import org.apache.fineract.infrastructure.core.exception.AbstractPlatformDomainRuleException; + + +public class SurveyResponseNotAvailableException extends AbstractPlatformDomainRuleException { + + public SurveyResponseNotAvailableException() { + super("error.msg.no.survey.response","No response available for survey."); + } + +} diff --git a/fineract-provider/src/main/java/org/apache/fineract/spm/repository/SurveyRepository.java b/fineract-provider/src/main/java/org/apache/fineract/spm/repository/SurveyRepository.java index 6c6344ac9..4e60376ae 100644 --- a/fineract-provider/src/main/java/org/apache/fineract/spm/repository/SurveyRepository.java +++ b/fineract-provider/src/main/java/org/apache/fineract/spm/repository/SurveyRepository.java @@ -30,6 +30,9 @@ public interface SurveyRepository extends JpaRepository { @Query("select s from Survey s where :pointInTime between s.validFrom and s.validTo") List fetchActiveSurveys(@Param("pointInTime") final Date pointInTime); + + @Query("select s from Survey s ") + List fetchAllSurveys(); @Query("select s from Survey s where s.key = :key and :pointInTime between s.validFrom and s.validTo") Survey findByKey(@Param("key") final String key, @Param("pointInTime") final Date pointInTime); diff --git a/fineract-provider/src/main/java/org/apache/fineract/spm/service/SpmService.java b/fineract-provider/src/main/java/org/apache/fineract/spm/service/SpmService.java index 8d6f0fa24..a1e837a0b 100644 --- a/fineract-provider/src/main/java/org/apache/fineract/spm/service/SpmService.java +++ b/fineract-provider/src/main/java/org/apache/fineract/spm/service/SpmService.java @@ -61,6 +61,12 @@ public class SpmService { return this.surveyRepository.fetchActiveSurveys(new Date()); } + + public List fetchAllSurveys() { + this.securityContext.authenticatedUser(); + + return this.surveyRepository.fetchAllSurveys(); + } public Survey findById(final Long id) { this.securityContext.authenticatedUser(); @@ -127,6 +133,21 @@ public class SpmService { this.surveyRepository.save(survey); } + public void activateSurvey(final Long id) { + this.securityContext.authenticatedUser(); + + final Survey survey = findById(id); + LocalDate validFrom = DateUtils.getLocalDateOfTenant() ; + Calendar cal = Calendar.getInstance() ; + cal.setTime(validFrom.toDate()); + cal.add(Calendar.YEAR, 100); + survey.setValidFrom(validFrom.toDate()); + survey.setValidTo(cal.getTime()); + + this.surveyRepository.save(survey); + } + + public static DateTime getStartOfToday() { return DateTime.now().withHourOfDay(0).withMinuteOfHour(0).withSecondOfMinute(0).withMillisOfSecond(0); } diff --git a/fineract-provider/src/main/java/org/apache/fineract/spm/util/ScorecardMapper.java b/fineract-provider/src/main/java/org/apache/fineract/spm/util/ScorecardMapper.java index 60588d8ea..573c52d51 100644 --- a/fineract-provider/src/main/java/org/apache/fineract/spm/util/ScorecardMapper.java +++ b/fineract-provider/src/main/java/org/apache/fineract/spm/util/ScorecardMapper.java @@ -29,6 +29,7 @@ import org.apache.fineract.spm.domain.Question; import org.apache.fineract.spm.domain.Response; import org.apache.fineract.spm.domain.Scorecard; import org.apache.fineract.spm.domain.Survey; +import org.apache.fineract.spm.exception.SurveyResponseNotAvailableException; import org.apache.fineract.useradministration.domain.AppUser; public class ScorecardMapper { @@ -43,7 +44,7 @@ public class ScorecardMapper { final List scorecardValues = scorecardData.getScorecardValues(); - if (scorecardValues != null) { + if (scorecardValues != null && !scorecardValues.isEmpty()) { for (ScorecardValue scorecardValue : scorecardValues) { final Scorecard scorecard = new Scorecard(); scorecards.add(scorecard); @@ -54,6 +55,8 @@ public class ScorecardMapper { scorecard.setCreatedOn(DateUtils.getLocalDateOfTenant().toDate()); scorecard.setValue(scorecardValue.getValue()); } + }else{ + throw new SurveyResponseNotAvailableException(); } return scorecards; }