//  
//  UpdatePropertiesActionHandler.java
//  xsdwsdl2code
//
//  Created by William Shakour on July 20, 2016.
//  Copyright © 2016 WillShex Limited. All rights reserved.
//
package com.willshex.blogwt.server.api.blog.action;

import java.util.List;
import java.util.logging.Logger;

import com.willshex.blogwt.server.api.ActionHandler;
import com.willshex.blogwt.server.api.validation.ApiValidator;
import com.willshex.blogwt.server.api.validation.PropertyValidator;
import com.willshex.blogwt.server.api.validation.SessionValidator;
import com.willshex.blogwt.server.api.validation.UserValidator;
import com.willshex.blogwt.server.service.property.PropertyServiceProvider;
import com.willshex.blogwt.server.service.user.UserServiceProvider;
import com.willshex.blogwt.shared.api.blog.call.UpdatePropertiesRequest;
import com.willshex.blogwt.shared.api.blog.call.UpdatePropertiesResponse;
import com.willshex.blogwt.shared.api.datatype.Property;
import com.willshex.gson.web.service.server.InputValidationException;

public final class UpdatePropertiesActionHandler extends
		ActionHandler<UpdatePropertiesRequest, UpdatePropertiesResponse> {
	private static final Logger LOG = Logger
			.getLogger(UpdatePropertiesActionHandler.class.getName());

	/* (non-Javadoc)
	 * 
	 * @see
	 * com.willshex.gson.web.service.server.ActionHandler#handle(com.willshex.
	 * gson.web.service.shared.Request,
	 * com.willshex.gson.web.service.shared.Response) */
	@Override
	protected void handle (UpdatePropertiesRequest input,
			UpdatePropertiesResponse output) throws Exception {
		ApiValidator.notNull(input, UpdatePropertiesRequest.class, "input");
		ApiValidator.accessCode(input.accessCode, "input.accessCode");
		output.session = input.session = SessionValidator
				.lookupAndExtend(input.session, "input.session");

		input.session.user = UserServiceProvider.provide()
				.getUser(Long.valueOf(input.session.userKey.getId()));

		UserValidator.authorisation(input.session.user, null,
				"input.session.user");

		List<Property> updatedProperties = PropertyValidator
				.validateAll(input.properties, "input.properties");

		Property existingProperty = null;
		boolean found;
		for (Property property : updatedProperties) {
			found = false;
			try {
				existingProperty = PropertyValidator.lookup(property,
						"input.properties[n]");
				found = true;
			} catch (InputValidationException ex) {
				LOG.info("Property [" + property.name
						+ "] does not exist. Will add with value ["
						+ property.value + "].");
			}

			if (found) {
				existingProperty.value = property.value;
				PropertyServiceProvider.provide()
						.updateProperty(existingProperty);
			} else {
				PropertyServiceProvider.provide().addProperty(property);
			}
		}
	}

	/* (non-Javadoc)
	 * 
	 * @see com.willshex.gson.web.service.server.ActionHandler#newOutput() */
	@Override
	protected UpdatePropertiesResponse newOutput () {
		return new UpdatePropertiesResponse();
	}

	/* (non-Javadoc)
	 * 
	 * @see com.willshex.gson.web.service.server.ActionHandler#logger() */
	@Override
	protected Logger logger () {
		return LOG;
	}
}
@@ -251,24 +251,57 @@
 util.List;%0D%0A
+import java.util.logging.Level;%0D%0A
 import java.
@@ -2483,24 +2483,64 @@
 tion ex) %7B%0D%0A
+%09%09%09%09if (LOG.isLoggable(Level.INFO)) %7B%0D%0A%09
 %09%09%09%09LOG.info
@@ -2576,16 +2576,17 @@
 %0D%0A%09%09%09%09%09%09
+%09
 + %22%5D doe
@@ -2628,16 +2628,17 @@
 %0D%0A%09%09%09%09%09%09
+%09
 + proper
@@ -2652,24 +2652,31 @@
 e + %22%5D.%22);%0D%0A
+%09%09%09%09%7D%0D%0A
 %09%09%09%7D%0D%0A%0D%0A%09%09%09i
@@ -3281,8 +3281,10 @@
 ;%0D%0A%09%7D%0D%0A%7D
+%0D%0A

check level before logging info