Can not save my objects list return from Openmrs in a json file

Hello All,

I have been trying to save the user list in the basic example module into a json file but I always find the file empty.

Any help would be appreciated. Thanks !

/**

  • This Source Code Form is subject to the terms of the Mozilla Public License,
  • v. 2.0. If a copy of the MPL was not distributed with this file, You can
  • obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under
  • the terms of the Healthcare Disclaimer located at http://openmrs.org/license.
  • Copyright © OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS
  • graphic logo is a trademark of OpenMRS Inc. */ package org.openmrs.module.gson.web.controller;

import java.util.List; import javax.servlet.http.HttpSession; import com.fasterxml.jackson.databind.ObjectMapper; import java.io.File; import java.io.IOException;

import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.openmrs.User; import org.openmrs.api.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.validation.BindingResult; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod;

/**

  • 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

    • @param httpSession

    • @param anyRequestObject

    • @param errors

    • @return */ @RequestMapping(method = RequestMethod.POST) public String onPost(HttpSession httpSession, @ModelAttribute(“anyRequestObject”) Object anyRequestObject, BindingResult errors) {

      if (errors.hasErrors()) { // return error view }

      return VIEW; }

    /**

    • 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
    

    } }

Instead of pasting the code here, can you upload your module to github and then share a link to the class that we should look at?

1 Like

Thank you fro reaching out @dkayiwa. Here it is the Github link: https://github.com/Plaisir93/org.openmrs-module-readencounters/blob/master/omod/src/main/java/org/openmrs/module/theencounters/web/controller/TheencountersController.java

I want to save my objects in a json file but I always end up getting:

[{“uuid”:“b1fc4943-5197-4568-8005-2447899a7858”,“creator”:{“uuid”:“A4F30A1B-5EB9-11DF-A648-37A07F9C90FB”,“name”:null,“description”:null,“creator”:{“uuid”:“1c3db49d-440a-11e6-a65c-00e04c680037”,“name”:null,“description”:null

Thanks,

That does not look like an empty file.

1 Like

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 ?

Thanks

@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.

1 Like

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;
}

When I do that I get:

[{“uuid”:“1c3db49d-440a-11e6-a65c-00e04c680037”,“name”:null,“description”:null

Thanks for your help.

This is the content of the users.json file. But in my jsp everything is fine, I can display all the fields.

I mean the intended use case. What are you trying to accomplish for the end user?

1 Like

I just got something working.

The use case is the following:

  • 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.

Thanks

What do you plan to use the extracted data for (From the end user perspective)?

1 Like

@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.

1 Like

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.

I will check it. Thanks