Code Review Please...

roy_sanu
roy_sanu used Ask the Experts™
on
Dear Experts,

let us know if logger is good for production or we have remove it out ? is that slows down at the applicationl level ?
if we use only after catch is that ok..
logger.warn("proceeding with incomplete request from mobile: {}. cause: {}, message: {}", 
					requestBody.length(), ex.getCause(), ex.getMessage());

Open in new window

let us know  in our application we are using hashmap  is that good or map would be good ?
	@CrossOrigin
	@RequestMapping(value = beneficiaryURL, method = RequestMethod.POST, produces = {"application/json" })
	public HashMap<String, Object> screeningRecord(@RequestBody String requestBody, 
			HttpServletRequest request) throws Exception {
		String remoteAddr = request.getRemoteAddr();
		logger.info("got request from: {}", remoteAddr);
		logger.debug("{}: received message: {}", remoteAddr, requestBody);
		HashMap<String, Object> screeningRespone = new HashMap<>();	
		// Transaction id is generated
		String transactionId = String.format("%s-%s", UUID.randomUUID().toString(), 
				dateTimeFormatter.format(new Date()));
		logger.info("{}: Generated id: {}, url: {}. Saving...", remoteAddr, 
				transactionId, beneficiaryURL);
		SubmissionInfo submissionInfo = new SubmissionInfo(transactionId, requestBody, 
				SubmissionInfo.PENDING, beneficiaryURL);
		Integer partition = null;
		try {
			Map<String, Object> jsonRequest = load(requestBody);
			Long microPlanId = Long.valueOf(jsonRequest.get("microPlanId").toString());
			partition = (int) Math.abs(microPlanId % mobileUploadPartitionSize);
			int beneficiaryCount = ((List) jsonRequest.getOrDefault("beneficiaryData", 
					Arrays.asList())).size();
			submissionInfo.setBeneficiaryCount(beneficiaryCount);
			submissionInfo.setTeamname(jsonRequest.getOrDefault("teamName", UNDECLARED).toString());
			submissionInfo.setInstitutionCode(jsonRequest.getOrDefault("institutionCode", 
					UNDECLARED).toString());
			submissionInfo.setDaySignOffID(jsonRequest.getOrDefault("daySignOffID", 
					UNDECLARED).toString());
			String screeningDateUnix = jsonRequest.get("screeningDate").toString();
			if(screeningDateUnix != null)
				submissionInfo.setScreeningDate(new Date(Long.valueOf(screeningDateUnix)));
		}
		catch(Exception ex) {
			logger.warn("proceeding with incomplete request from mobile: {}. cause: {}, message: {}", 
					requestBody.length(), ex.getCause(), ex.getMessage());
			//ex.printStackTrace();
		}

Open in new window


Thanks
Roy...
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
ste5anSenior Developer

Commented:
Both questions could not be answered sincerely, cause they are performance related. Thus the entire architecture is important as well as how you're hosting your application.

The only things about your actual code:

You're catching a general exception (which is bad).

I would normally use a factory method or a builder in this case. Cause you should for sure refactor your code and extract this part. Then you can control there the amount of warnings logged. Especially as this would be the place for sanitation and validation of the user input. Which currently seems to miss entirely.

Your SubmissionInfo class seems to be a bit of a mess..
Why does it store the actual plain request and decoupled from it its properties? This allows the request and the properties to contain different, contradicting values.
Why do you need to create the transaction ID manually? This should be retrieved from a generator.
roy_sanudeveloper

Author

Commented:

I would normally use a factory method or a builder in this case
can we provide me a sample of your understanding on the code so that  i could take it forward from there..

Thanks
roy
Senior Developer
Commented:
Well, something like

@CrossOrigin
@RequestMapping(value = beneficiaryURL, method = RequestMethod.POST, produces = {"application/json" })
public HashMap<String, Object> screeningRecord(@RequestBody String requestBody, HttpServletRequest request) throws Exception {
	String remoteAddr = request.getRemoteAddr();
	logger.info("got request from: {}", remoteAddr);
	logger.debug("{}: received message: {}", remoteAddr, requestBody);
	HashMap<String, Object> screeningRespone = new HashMap<>();		
	logger.info("{}: Generated id: {}, url: {}. Saving...", remoteAddr, transactionId, beneficiaryURL);
	SubmissionInfo submissionInfo = SubmissionInfo.FromRequestBody(requestBody);
	// [..]
}

class SubmissionInfo 
 {
	 // [..]	 
     public static SubmissionInfo FromRequestBody(string requestBody) 
     {
		// ToDo: Sanitation & Validation.
         Map<String, Object> parsedJson = load(requestBody);		 
		 return FromParsedJson(parsedJson);
     }

     public static SubmissionInfo FromParsedJson(Map<String, Object> parsedJson) 
     {
		// ToDo: Sanitation & Validation.
		SubmissionInfo submissionInfo = new SubmissionInfo();				
		submissionInfo.setBeneficiaryCount(((List) parsedJson.getOrDefault("beneficiaryData", Arrays.asList())).size());
		submissionInfo.setTeamname(parsedJson.getOrDefault("teamName", UNDECLARED).toString());
		submissionInfo.setInstitutionCode(parsedJson.getOrDefault("institutionCode", UNDECLARED).toString());
		submissionInfo.setDaySignOffID(parsedJson.getOrDefault("daySignOffID", UNDECLARED).toString());
		String screeningDateUnix = parsedJson.get("screeningDate").toString();
		if(screeningDateUnix != null)
			submissionInfo.setScreeningDate(new Date(Long.valueOf(screeningDateUnix)));
		return submissionInfo;
     }
}

Open in new window


But as I already said: Your SubmissionInfo class in the current state seems overloaded. The question is: Why is it not a pojo?
roy_sanudeveloper

Author

Commented:
Thanks Stefan

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial