This class configured as controller using annotation and mapped with the URL of
‘module/gson/gsonLink.form’.
*/
@Controller("${rootrootArtifactId}.GsonController")
@RequestMapping(value = “module/gson/gson.form”)
public class GsonController {
/** Logger for this class and subclasses */
protected final Log log = LogFactory.getLog(getClass());
@Autowired
UserService userService;
/** Success form view name */
private final String VIEW = “/module/gson/gson”;
/**
Initially called after the getUsers method to get the landing form name
@return String form view name
*/
@RequestMapping(method = RequestMethod.GET)
public String onGet() {
return VIEW;
}
/**
All the parameters are optional based on the necessity
This class returns the form backing object. This can be a string, a boolean, or a normal java
pojo. The bean name defined in the ModelAttribute annotation and the type can be just defined
by the return type of this method
*/
@ModelAttribute(“users”)
protected List getUsers() throws Exception {
List users = userService.getAllUsers();
ObjectMapper mapper = new ObjectMapper();
File file = new File("users.json");
try {
// Serialize Java object into JSON file.
mapper.writeValue(file, users);
}
catch (IOException e) {
e.printStackTrace();
}
return users;
// this object will be made available to the jsp page under the variable name
// that is defined in the @ModuleAttribute tag
It had been empty for a time and then I got this while I have many encounters that I can display in my JSP. Is there something I should know about serializing OpenMRS Objects ?
@pcp do you mind sharing details of what you ultimately want to achieve by this? That way, we shall be in a better position to guide you with what we feel as the best approach to use.
For instance, I would like to serialize the list of users returned by userService.getAllUsers();
@ModelAttribute(“users”)
protected List getUsers() throws Exception {
List users = userService.getAllUsers();
Gson gson = new Gson();
FileWriter writer = null;
ArrayList<User> arrayUsers = new ArrayList<User>();
try {
writer = new FileWriter("users.json");
for (User usr : users) {
arrayUsers.add(usr);
}
}
catch (IOException e) {
e.printStackTrace();
}
gson.toJson(arrayUsers, writer);
// this object will be made available to the jsp page under the variable name
// that is defined in the @ModuleAttribute tag
return users;
}
I want to extract data (a cohort of patients as example) from OpenMRS for a period of time (startDate and endDate)
I want to expose those data in Json format as a web service.
As the rest module does not allow getting a cohort so I was trying to build a module and get those data and then serialize them in a json file. I used both Gson and Jackson library respectively but I always ended up getting something like this when trying serializing the list of users: [{“uuid”:“1c3db49d-440a-11e6-a65c-00e04c680037”,“name”:null,“description”:null
So I have just tried to create my own class which represents a user and get the fields needed from OpenMRS users list and construct a json string. I finally serialize my string using Jackson.
I think there should be an easier way to do that, any suggestion would be really appreciated.
The best option would be to use the rest module to get a cohort of patients.
@pcp, there is a “cohort” resource in the reportingrest module I believe. Have you tried using that? I’m not sure how the relates to serializing user data - that would likely need something different.
For an application module not running on OpenMRS yet which is getting data from OpenMRS and looking for patients lost to follow-up. There are agents seeking those patients and providing updates using a mobile app interacting with this module. After we need to automatically update OpenMRS data.