import java.io.Serializable;
import java.util.Arrays;
import javax.xml.bind.annotation.XmlRootElement;
import org.codehaus.jackson.annotate.JsonIgnore;
import org.codehaus.jackson.map.annotate.JsonSerialize;
import com.varra.identity.json.annotate.ResponseJsonIgnore;
import com.varra.identity.json.serializer.AccountJsonSerializer;
/**
*
* @author Rajakrishna V. Reddy
* @version 1.0
*/
@XmlRootElement(name = "Account")
@JsonSerialize(using = AccountJsonSerializer.class)
public class Account implements Serializable
{
/** The Constant serialVersionUID. */
private static final long serialVersionUID = 2077532866344069471L;
/** The Constant URL_PARAMS_DEFAULT_LENGTH. */
private static final int URL_PARAMS_DEFAULT_LENGTH = 255;
/** The id. */
private int id = 0;
/** The acc id. */
private int accId = 0;
/** The account name. */
private String accountName = "";
/** The company name. */
private String companyName = "";
/** The company telephone. */
private String companyTelephone = "";
/** The company web. */
private String companyWeb = "";
/** The type. */
private String type = "";
/** The admin username. */
private String adminUsername = "";
/** The admin password. */
private String adminPassword = "";
/** The admin password c. */
private String adminPasswordC = "";
/** The admin first name. */
private String adminFirstName = "";
/** The admin last name. */
private String adminLastName = "";
/** The email address. */
private String emailAddress = "";
/** The uuid. */
private int uuid = 0;
/** The admin user uuid. */
private String adminUserUuid = "";
/** The is active. */
private boolean isActive = true;
/** The approve status. */
private int approveStatus = 0;
/** The tag ids. */
private String[] tagIds;
/** The account tag. */
private String accountTag = "";
/** The tag created by. */
private String tagCreatedBy = "";
/** The enterprise. */
private boolean enterprise;
private String companyAddress;
/**
* Checks if is enterprise.
*
* @return true, if is enterprise
*/
@JsonIgnore
public boolean isEnterprise()
{
return enterprise;
}
/**
* Sets the enterprise.
*
* @param enterprise
* the new enterprise
*/
public void setEnterprise(boolean enterprise)
{
this.enterprise = enterprise;
}
/**
* Gets the company address.
*
* @return the company address
*/
public String getCompanyAddress()
{
return companyAddress;
}
/**
* Sets the company address.
*
* @param companyAddress
* the new company address
*/
public void setCompanyAddress(String companyAddress)
{
this.companyAddress = companyAddress;
}
/**
* Gets the id.
*
* @return the id
*/
public int getId()
{
return id;
}
/**
* Sets the id.
*
* @param id
* the new id
*/
public void setId(int id)
{
this.id = id;
}
/**
* Gets the acc id.
*
* @return the acc id
*/
public int getAccId()
{
return accId;
}
/**
* Sets the acc id.
*
* @param accId
* the new acc id
*/
public void setAccId(int accId)
{
this.accId = accId;
}
/**
* Gets the account name.
*
* @return the account name
*/
public String getAccountName()
{
return accountName;
}
/**
* Sets the account name.
*
* @param accountName
* the new account name
*/
public void setAccountName(String accountName)
{
this.accountName = accountName;
}
/**
* Gets the company name.
*
* @return the company name
*/
public String getCompanyName()
{
return companyName;
}
/**
* Sets the company name.
*
* @param companyName
* the new company name
*/
public void setCompanyName(String companyName)
{
this.companyName = companyName;
}
/**
* Gets the company telephone.
*
* @return the company telephone
*/
public String getCompanyTelephone()
{
return companyTelephone;
}
/**
* Sets the company telephone.
*
* @param companyTelephone
* the new company telephone
*/
public void setCompanyTelephone(String companyTelephone)
{
this.companyTelephone = companyTelephone;
}
/**
* Gets the company web.
*
* @return the company web
*/
public String getCompanyWeb()
{
return companyWeb;
}
/**
* Sets the company web.
*
* @param companyWeb
* the new company web
*/
public void setCompanyWeb(String companyWeb)
{
this.companyWeb = companyWeb;
}
/**
* Gets the type.
*
* @return the type
*/
public String getType()
{
return type;
}
/**
* Sets the type.
*
* @param type
* the new type
*/
public void setType(String type)
{
this.type = type;
}
/**
* Gets the admin username.
*
* @return the admin username
*/
public String getAdminUsername()
{
return adminUsername;
}
/**
* Sets the admin username.
*
* @param adminUsername
* the new admin username
*/
public void setAdminUsername(String adminUsername)
{
this.adminUsername = adminUsername;
}
/**
* Gets the admin password.
*
* @return the admin password
*/
@ResponseJsonIgnore
public String getAdminPassword()
{
return adminPassword;
}
/**
* Sets the admin password.
*
* @param adminPassword
* the new admin password
*/
public void setAdminPassword(String adminPassword)
{
this.adminPassword = adminPassword;
}
/**
* Gets the admin password c.
*
* @return the admin password c
*/
@ResponseJsonIgnore
public String getAdminPasswordC()
{
return adminPasswordC;
}
/**
* Sets the admin password c.
*
* @param adminPasswordC
* the new admin password c
*/
public void setAdminPasswordC(String adminPasswordC)
{
this.adminPasswordC = adminPasswordC;
}
/**
* Gets the admin first name.
*
* @return the admin first name
*/
public String getAdminFirstName()
{
return adminFirstName;
}
/**
* Sets the admin first name.
*
* @param adminFirstName
* the new admin first name
*/
public void setAdminFirstName(String adminFirstName)
{
this.adminFirstName = adminFirstName;
}
/**
* Gets the admin last name.
*
* @return the admin last name
*/
public String getAdminLastName()
{
return adminLastName;
}
/**
* Sets the admin last name.
*
* @param adminLastName
* the new admin last name
*/
public void setAdminLastName(String adminLastName)
{
this.adminLastName = adminLastName;
}
/**
* Gets the email address.
*
* @return the email address
*/
public String getEmailAddress()
{
return emailAddress;
}
/**
* Sets the email address.
*
* @param emailAddress
* the new email address
*/
public void setEmailAddress(String emailAddress)
{
this.emailAddress = emailAddress;
}
/**
* Gets the uuid.
*
* @return the uuid
*/
@ResponseJsonIgnore
public int getUuid()
{
return uuid;
}
/**
* Sets the uuid.
*
* @param uuid
* the new uuid
*/
public void setUuid(int uuid)
{
this.uuid = uuid;
}
/**
* Gets the account tag.
*
* @return the account tag
*/
@ResponseJsonIgnore
public String getAccountTag()
{
return accountTag;
}
/**
* Sets the account tag.
*
* @param accountTag
* the new account tag
*/
public void setAccountTag(String accountTag)
{
this.accountTag = accountTag;
}
/**
* Gets the tag ids.
*
* @return the tag ids
*/
@ResponseJsonIgnore
public String[] getTagIds()
{
return tagIds;
}
/**
* Sets the tag ids.
*
* @param tagIds
* the new tag ids
*/
public void setTagIds(String[] tagIds)
{
this.tagIds = tagIds;
}
/**
* Gets the tag created by.
*
* @return the tag created by
*/
@ResponseJsonIgnore
public String getTagCreatedBy()
{
return tagCreatedBy;
}
/**
* Sets the tag created by.
*
* @param tagCreatedBy
* the new tag created by
*/
public void setTagCreatedBy(String tagCreatedBy)
{
this.tagCreatedBy = tagCreatedBy;
}
/**
* Checks if is active.
*
* @return true, if is active
*/
public boolean isActive()
{
return isActive;
}
/**
* Sets the active.
*
* @param isActive
* the new active
*/
public void setActive(boolean isActive)
{
this.isActive = isActive;
}
/**
* Gets the approve status.
*
* @return the approve status
*/
public int getApproveStatus()
{
return approveStatus;
}
/**
* Sets the approve status.
*
* @param approveStatus
* the new approve status
*/
public void setApproveStatus(int approveStatus)
{
this.approveStatus = approveStatus;
}
/**
* Gets the admin user uuid.
*
* @return the admin user uuid
*/
public String getAdminUserUuid()
{
return adminUserUuid;
}
/**
* Sets the admin user uuid.
*
* @param adminUserUuid
* the new admin user uuid
*/
public void setAdminUserUuid(String adminUserUuid)
{
this.adminUserUuid = adminUserUuid;
}
@Override
public String toString()
{
StringBuilder builder = new StringBuilder();
builder.append("Account [id=");
builder.append(id);
builder.append(", accId=");
builder.append(accId);
builder.append(", accountName=");
builder.append(accountName);
builder.append(", companyName=");
builder.append(companyName);
builder.append(", companyTelephone=");
builder.append(companyTelephone);
builder.append(", companyWeb=");
builder.append(companyWeb);
builder.append(", type=");
builder.append(type);
builder.append(", adminUsername=");
builder.append(adminUsername);
builder.append(", adminPassword=");
builder.append(adminPassword);
builder.append(", adminPasswordC=");
builder.append(adminPasswordC);
builder.append(", adminFirstName=");
builder.append(adminFirstName);
builder.append(", adminLastName=");
builder.append(adminLastName);
builder.append(", emailAddress=");
builder.append(emailAddress);
builder.append(", uuid=");
builder.append(uuid);
builder.append(", adminUserUuid=");
builder.append(adminUserUuid);
builder.append(", isActive=");
builder.append(isActive);
builder.append(", approveStatus=");
builder.append(approveStatus);
builder.append(", tagIds=");
builder.append(Arrays.toString(tagIds));
builder.append(", accountTag=");
builder.append(accountTag);
builder.append(", tagCreatedBy=");
builder.append(tagCreatedBy);
builder.append(", enterprise=");
builder.append(enterprise);
builder.append("]");
return builder.toString();
}
/**
* Gets the as url params.
*
* @return the as url params
*/
@JsonIgnore
public String getAsUrlParams()
{
StringBuilder stringBuilder = new StringBuilder(URL_PARAMS_DEFAULT_LENGTH);
stringBuilder.append("accountName=").append(accountName);
stringBuilder.append("&companyName=").append(companyName);
stringBuilder.append("&companyTelephone=").append(companyTelephone);
stringBuilder.append("&companyWeb=").append(companyWeb);
stringBuilder.append("&type=").append(type);
stringBuilder.append("&adminUsername=").append(adminUsername);
stringBuilder.append("&adminFirstName=").append(adminFirstName);
stringBuilder.append("&adminLastName=").append(adminLastName);
stringBuilder.append("&emailAddress=").append(emailAddress);
stringBuilder.append("&uuid=").append(uuid);
stringBuilder.append("&adminUserUuid=").append(adminUserUuid);
return stringBuilder.toString();
}
}
/*
* identity-json - AccountJsonSerializer.java, Sep 22, 2015 12:41:03 PM
*
* Copyright 2015 Trimble Ltd, Inc. All rights reserved.
* Trimble proprietary/confidential. Use is subject to license terms.
*/
package com.trimble.identity.json.serializer;
import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.JsonProcessingException;
import org.codehaus.jackson.annotate.JsonIgnore;
import org.codehaus.jackson.map.JsonSerializer;
import org.codehaus.jackson.map.SerializerProvider;
import com.trimble.identity.json.Account;
import com.trimble.identity.json.annotate.ResponseJsonIgnore;
/**
* TODO Description go here.
*
* @author <a href="mailto:Rajakrishna_Reddy@Trimble.com">Rajakrishna V.
* Reddy</a>
* @version 1.0
*
*/
public class AccountJsonSerializer extends JsonSerializer<Account>
{
/** The accepted methods. */
private final List<Method> acceptedMethods = new LinkedList<Method>();
/** The rejected methods. */
private final List<Method> rejectedMethods = new LinkedList<Method>();
/**
* Instantiates a new account json serializer.
*/
public AccountJsonSerializer()
{
for (Method m : printGettersSetters(Account.class))
{
final Annotation[] annotations = m.getDeclaredAnnotations();
boolean isAnnotated = false;
for (Annotation annotation : annotations)
{
if (annotation.annotationType().equals(ResponseJsonIgnore.class)
|| annotation.annotationType().equals(JsonIgnore.class))
{
isAnnotated = true;
}
}
if (isAnnotated)
{
rejectedMethods.add(m);
}
else
{
acceptedMethods.add(m);
}
}
}
/*
* (non-Javadoc)
*
* @see org.codehaus.jackson.map.JsonSerializer#serialize(java.lang.Object,
* org.codehaus.jackson.JsonGenerator,
* org.codehaus.jackson.map.SerializerProvider)
*/
@Override
public void serialize(Account account, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException
{
jgen.writeStartObject();
for (Method method : acceptedMethods)
{
try
{
if (String.class.isAssignableFrom(method.getReturnType()))
{
jgen.writeStringField(getFieldNameFromGetMethodName(method.getName()), (String) method.invoke(account, Collections.emptyList().toArray()));
}
else if (method.getReturnType().toString().equalsIgnoreCase("int") || Integer.class.isAssignableFrom(method.getReturnType()))
{
jgen.writeNumberField(getFieldNameFromGetMethodName(method.getName()), (Integer) method.invoke(account, Collections.emptyList().toArray()));
}
else if (method.getReturnType().toString().equalsIgnoreCase("float") || Float.class.isAssignableFrom(method.getReturnType()))
{
jgen.writeNumberField(getFieldNameFromGetMethodName(method.getName()), (Float) method.invoke(account, Collections.emptyList().toArray()));
}
else if (method.getReturnType().toString().equalsIgnoreCase("boolean") || Boolean.class.isAssignableFrom(method.getReturnType()))
{
jgen.writeBooleanField(getFieldNameFromGetMethodName(method.getName()), (Boolean) method.invoke(account, Collections.emptyList().toArray()));
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
jgen.writeEndObject();
}
/**
* Prints the getters setters.
*
* @param <T>
* the generic type
* @param aClass
* the a class
* @return the list
*/
public static <T> List<Method> printGettersSetters(Class<T> aClass)
{
final Method[] methods = aClass.getDeclaredMethods();
final List<Method> getterMethods = new LinkedList<Method>();
for (Method method : methods)
{
if (isGetter(method))
{
getterMethods.add(method);
}
}
return getterMethods;
}
/**
* Checks if is getter.
*
* @param method
* the method
* @return true, if is getter
*/
public static boolean isGetter(Method method)
{
if (!(method.getName().startsWith("get") || method.getName().startsWith("is")))
return false;
if (method.getParameterTypes().length != 0)
return false;
if (void.class.equals(method.getReturnType()))
return false;
return true;
}
/**
* Checks if is setter.
*
* @param method
* the method
* @return true, if is setter
*/
public static boolean isSetter(Method method)
{
if (!method.getName().startsWith("set"))
return false;
if (method.getParameterTypes().length != 1)
return false;
return true;
}
public static String getFieldNameFromGetMethodName(String method)
{
if (method.startsWith("get"))
{
return String.valueOf(method.charAt(3)).toLowerCase() + method.substring(4);
}
else
{
return String.valueOf(method.charAt(2)).toLowerCase() + method.substring(3);
}
}
/**
* Entry point to the entire application, contains the list of args to pass
* the user defined values.
*
* @param args
* the arguments
* @throws InvocationTargetException
* @throws IllegalArgumentException
* @throws IllegalAccessException
*/
public static void main(String... args) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException
{
AccountJsonSerializer serializer = new AccountJsonSerializer();
System.out.println("AM: " + serializer.acceptedMethods);
System.out.println("RM: " + serializer.rejectedMethods);
Account account = new Account();
account.setId(21);
account.setEnterprise(true);
for (Method string : serializer.acceptedMethods)
{
if (string.getReturnType().isArray())
{
}
}
System.out.println(serializer.acceptedMethods.get(0).invoke(account, Collections.emptyList().toArray()));
System.out.println(Integer.class.isAssignableFrom(serializer.acceptedMethods.get(0).getReturnType()));
System.out.println(serializer.acceptedMethods.get(0).getReturnType().isAssignableFrom(String.class));
System.out.println(serializer.acceptedMethods.get(0).getReturnType().isAssignableFrom(Float.class));
/*
* for (Method string : serializer.acceptedMethods) {
* System.out.println("id: "
* +getFieldNameFromGetMethodName(string.getName())); } for (Method
* string : serializer.rejectedMethods) { System.out.println("id: "
* +getFieldNameFromGetMethodName(string.getName())); }
*/
}
}
package com.varra.identity.json.annotate;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.codehaus.jackson.annotate.JacksonAnnotation;
/**
* Marker annotation that indicates that the annotated method or field is to be
* ignored by introspection-based serialization functionality in the response.
* That is, it should not be consider a "getter", "setter" or "creator".
* <p>
* For example, a "getter" method that would otherwise denote a property (like,
* say, "getValue" to suggest property "value") to serialize, would be ignored
* and no such property would be output unless another annotation defines
* alternative method to use.
* <p>
* This annotation works purely on method-by-method (or field-by-field) basis;
* annotation on one method or field does not imply ignoring other methods or
* fields. Specifically, marking a "setter" candidate does not change handling
* of matching "getter" method (or vice versa).
* <p>
* Annotation is usually used just a like a marker annotation, that is, without
* explicitly defining 'value' argument (which defaults to <code>true</code>):
* but argument can be explicitly defined. This can be done to override an
* existing JsonIgnore by explictly defining one with 'false' argument.
* <p>
* Annotation is similar to {@link javax.xml.bind.annotation.XmlTransient}
*
* @author Rajakrishna V. Reddy
* @version 1.0
*/
@Target({ ElementType.METHOD, ElementType.CONSTRUCTOR, ElementType.FIELD })
@Retention(RetentionPolicy.RUNTIME)
@JacksonAnnotation
public @interface ResponseJsonIgnore
{
/**
* Optional argument that defines whether this annotation is active or not.
* The only use for value 'false' if for overriding purposes (which is not
* needed often); most likely it is needed for use with "mix-in annotations"
* (aka "annotation overrides"). For most cases, however, default value of
* "true" is just fine and should be omitted.
*
* @return true, if successful
*/
boolean value() default true;
}
No comments:
Post a Comment