This repository has been archived by the owner on Jan 21, 2021. It is now read-only.
forked from SEL-Columbia/dristhi
-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added endpoint to push reports from client to server
- Loading branch information
manu
committed
Jul 5, 2017
1 parent
6bcc7e8
commit 134db72
Showing
1 changed file
with
75 additions
and
0 deletions.
There are no files selected for viewing
75 changes: 75 additions & 0 deletions
75
opensrp-web/src/main/java/org/opensrp/web/rest/ReportResource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package org.opensrp.web.rest; | ||
|
||
import static java.text.MessageFormat.format; | ||
import static org.springframework.http.HttpStatus.BAD_REQUEST; | ||
import static org.springframework.http.HttpStatus.CREATED; | ||
import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR; | ||
import static org.springframework.web.bind.annotation.RequestMethod.POST; | ||
|
||
import java.util.ArrayList; | ||
|
||
import org.joda.time.DateTime; | ||
import org.json.JSONObject; | ||
import org.opensrp.domain.Report; | ||
import org.opensrp.service.ReportService; | ||
import org.opensrp.util.DateTimeTypeConverter; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.stereotype.Controller; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
|
||
import com.google.gson.Gson; | ||
import com.google.gson.GsonBuilder; | ||
import com.google.gson.reflect.TypeToken; | ||
|
||
@Controller | ||
@RequestMapping(value = "/rest/report/") | ||
public class ReportResource { | ||
|
||
private static Logger logger = LoggerFactory.getLogger(ReportResource.class.toString()); | ||
private ReportService reportService; | ||
|
||
Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ") | ||
.registerTypeAdapter(DateTime.class, new DateTimeTypeConverter()).create(); | ||
|
||
@Autowired | ||
public ReportResource(ReportService reportService) { | ||
this.reportService = reportService; | ||
} | ||
|
||
/** | ||
* adding dhis2 reports to opensrp | ||
* | ||
* @param report model payload | ||
* @return report object | ||
*/ | ||
@SuppressWarnings("unchecked") | ||
@RequestMapping(headers = { "Accept=application/json" }, method = POST, value = "/add") | ||
public ResponseEntity<HttpStatus> save(@RequestBody String data) { | ||
try { | ||
JSONObject syncData = new JSONObject(data); | ||
if (!syncData.has("reports")) { | ||
return new ResponseEntity<>(BAD_REQUEST); | ||
} | ||
ArrayList<Report> reports = (ArrayList<Report>) gson.fromJson(syncData.getString("reports"), | ||
new TypeToken<ArrayList<Report>>() {}.getType()); | ||
for (Report report : reports) { | ||
try { | ||
reportService.addorUpdateReport(report); | ||
} | ||
catch (Exception e) { | ||
logger.error("Report" + report.getId()+" failed to sync", e); | ||
} | ||
} | ||
} | ||
catch (Exception e) { | ||
logger.error(format("Sync data processing failed with exception {0}.- ", e)); | ||
return new ResponseEntity<>(INTERNAL_SERVER_ERROR); | ||
} | ||
return new ResponseEntity<>(CREATED); | ||
} | ||
} |