/**
* Copyright 2012 cubiculus.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.coroptis.cubiculus.rest.mappers;
import com.coroptis.cubiculus.rest.model.AjUser;
import com.sdicons.json.mapper.JSONMapper;
import com.sdicons.json.mapper.MapperException;
import com.sdicons.json.mapper.helper.SimpleMapperHelper;
import com.sdicons.json.mapper.helper.impl.ObjectMapper;
import com.sdicons.json.model.JSONObject;
import com.sdicons.json.model.JSONValue;
public class JsonMapperAjUser implements SimpleMapperHelper {
@Override
public Class getHelpedClass() {
return AjUser.class;
}
@Override
public Object toJava(JSONValue aValue, Class aRequestedClass) throws MapperException {
if (aValue.isObject()) {
final JSONObject jsonObject = (JSONObject) aValue;
final AjUser out = new AjUser();
out.setIdUser(Helper.getIntRequired(jsonObject, "idUser",
"AjUser property idUser was not found in json data"));
out.setIdImageIcon(Helper.getIntRequired(jsonObject, "idImageIcon",
"AjUser property IdImageIcon was not found in json data"));
out.setBirthDate(Helper.getDate(jsonObject, "birthDate"));
out.setUserName(Helper.getStringRequired(jsonObject, "userName",
"AjUser property userName was not found in json data"));
out.setEmail(Helper.getStringRequired(jsonObject, "email",
"AjUser property email was not found in json data"));
out.setIdBlog(Helper.getIntRequired(jsonObject, "idBlog",
"AjUser property idBlog was not found in json data"));
out.setLastLogin(Helper.getDate(jsonObject, "lastLogin"));
out.setRegistrationDate(Helper.getDate(jsonObject, "registrationDate"));
return out;
}
throw new MapperException("AjPropertyValue cannot map: " + aValue.getClass().getName());
}
@Override
public JSONValue toJSON(Object aPojo) throws MapperException {
if (aPojo instanceof AjUser) {
AjUser user = (AjUser) aPojo;
final JSONObject lElements = new JSONObject();
lElements.getValue().put("idUser", JSONMapper.toJSON(user.getIdUser()));
lElements.getValue().put("idImageIcon", JSONMapper.toJSON(user.getIdImageIcon()));
lElements.getValue().put("birthDate", JSONMapper.toJSON(user.getBirthDate()));
lElements.getValue().put("userName", JSONMapper.toJSON(user.getUserName()));
lElements.getValue().put("email", JSONMapper.toJSON(user.getEmail()));
lElements.getValue().put("idBlog", JSONMapper.toJSON(user.getIdBlog()));
lElements.getValue().put("lastLogin", JSONMapper.toJSON(user.getLastLogin()));
lElements.getValue().put("registrationDate",
JSONMapper.toJSON(user.getRegistrationDate()));
return lElements;
} else {
throw new MapperException();
}
}
}