Code Review Please...

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...
roy_sanudeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ste5anSenior DeveloperCommented:
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.
0
roy_sanudeveloperAuthor 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
0
ste5anSenior DeveloperCommented:
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?
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
roy_sanudeveloperAuthor Commented:
Thanks Stefan
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.