0

I have an Android Application.

I have captured the events by creating a method which captures various parameter /variable value from the application. The code snippet which I have written contain EventClass and EventModelClass { public class UserAccessEvent implements ILog {

private Logger mLogger = null;
UserAccessEventModel obj;

public void StoreUserAccessEvent(String Timestamp, String UserId, int FailedAttempts, int SuccessfulAttempts, int Attempts,
                                 int NoOfRevocations, int PassCodeChange) {

    obj = new UserAccessEventModel(Timestamp, UserId,  SuccessfulAttempts, FailedAttempts, Attempts, NoOfRevocations, PassCodeChange);

    toJsonLog();
}

public String toJsonLog() {

    JSONObject event = new JSONObject();

    try {

        event.put("Timestamp", obj.getTimestamp());
        event.put("UserId", obj.userid);
        event.put("SuccessfulAttempts", obj.successfulattempts);
        event.put("FailedAttempts", obj.failedattempts);
        event.put("Attempts", obj.attempts);
        event.put("NoOfRevocations", obj.noofrevocations);
        event.put("PassCodeChange", obj.passcodechange);

        //to store the log data that is stored in json object
        Log.d("UserAccessEvent", event.toString());
        return event.toString();

    } catch (Exception e) {
        Log.d("UserAccessEvent", "Exception");
        throw new RuntimeException(e);

    }
}

}

public class UserAccessEventModel {

private String timestamp;
public String userid;
public int successfulattempts;
public int failedattempts;
public int attempts;
public int noofrevocations;
public int passcodechange;


public void setTimestamp(String timestamp) {
    this.timestamp = timestamp;
}

public void setUserid(String userid) {
    this.userid = userid;
}

public void setSuccessfulattempts(int successfulattempts) { this.successfulattempts = successfulattempts;   }

public void setFailedattempts(int failedattempts) {
    this.failedattempts = failedattempts;
}

public void setAttempts(int attempts) {
    this.attempts = attempts;
}

public void setNoofrevocations(int noofrevocations) {
    this.noofrevocations = noofrevocations;
}

public void setPasscodechange(int passcodechange) {
    this.passcodechange = passcodechange;
}

public String getTimestamp(){
    timestamp = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTime());
    return timestamp;

}

public UserAccessEventModel() {

}

public UserAccessEventModel(String timestamp, String userid, int successfulattempts, int failedattempts, int attempts, int noofrevocations, int passcodechange) {
    this.timestamp = timestamp;
    this.userid = userid;
    this.successfulattempts = successfulattempts;
    this.failedattempts = failedattempts;
    this.attempts = attempts;
    this.noofrevocations = noofrevocations;
    this.passcodechange = passcodechange;
}

}

I want to make changes so that I can just use JSON to log data without using model class.I want it to be more generic.....

Now I need to log the data which is in the method by just changing the JSON method key/values pair.

Is there any method where I can do this??

Please Give any Suggestions.

padma
  • 15
  • 6

1 Answers1

0

If you are using java 8 you can do as following.

public String toJsonLog(Class YourModelClass) {
                JSONObject event = new JSONObject();
                Class yourClass = YourModelClass.class;
                Field[] fields = yourClass.getFields();

                YourModelClass obj = new YourModelClass();

                for(Field f: fields){
                    f.getName();
                }
                try {
                    for(int i=0; i<fields.length(); i++){
                    String geterStr = obj + ".get" + fields[i] + "()";
                        event.put(fields[i], geterStr);
                    }

                    Log.d("YourModelClass", event.toString());
                    return event.toString();

                } catch (Exception e) {
                    Log.d("YourModelClass", "Exception");
                    throw new RuntimeException(e);

                }
            }

but in this you have to make getter method same as your field name. for ex. say your field name is userId then you have to make getter method as getuserId() instead of getUserId(). you have to ignore java naming convention to make this generic.

Note: I haven't tried this, but I took reference from this question: How to get variable name? - java and put my own logic, but I guess it should work.

Megha Maniar
  • 444
  • 5
  • 22