Mapping encapsulated complex types with JMapper - java

I am trying to map two structures with JMapper but struggle with two encapsulated complex types and how to map them. I want to achive the following:
Source > Destination
Source.sourceString > Destination.destinationString
Source.SourceInternal > Destination.DestinationInternal
Source.SourceInternal.internalString2 > Destination.DestinationInternal.internalString
My classes look as follows:
public class Source {
private String sourceString;
private SourceInternal sourceInternal;
public String getSourceString() {
return sourceString;
}
public void setSourceString(final String sourceString) {
this.sourceString = sourceString;
}
public SourceInternal getSourceInternal() {
return sourceInternal;
}
public void setSourceInternal(final SourceInternal sourceInternal) {
this.sourceInternal = sourceInternal;
}
}
The internal source object
public class SourceInternal {
private String internalString1;
private String internalString2;
public String getInternalString1() {
return internalString1;
}
public void setInternalString1(final String internalString1) {
this.internalString1 = internalString1;
}
public String getInternalString2() {
return internalString2;
}
public void setInternalString2(final String internalString2) {
this.internalString2 = internalString2;
}
}
The destination the source should be mapped to
public class Destination {
private String destinationString;
private DestinationInternal destinationInternal;
public String getDestinationString() {
return destinationString;
}
public void setDestinationString(final String destinationString) {
this.destinationString = destinationString;
}
public DestinationInternal getDestinationInternal() {
return destinationInternal;
}
public void setDestinationInternal(final DestinationInternal destinationInternal) {
this.destinationInternal = destinationInternal;
}
}
The internal destination object.
public class DestinationInternal {
private String internalString;
public String getInternalString() {
return internalString;
}
public void setInternalString(final String internalString) {
this.internalString = internalString;
}
}
How would I achive the described mapping? Is it even possible with JMapper? Thanks.

I was looking into that a similar feature too. Here's how I managed it.
JMapperAPI jMapperAPI = new JMapperAPI()
.add(mappedClass(Destination.class)
.add(attribute("destinationString").value("sourceString"))
.add(attribute("destinationInternal").value("sourceInternal")))
.add(mappedClass(DestinationInternal.class).add(attribute("internalString").value("internalString1").targetClasses(SourceInternal.class)));
Basically the logic is to have a mapping for each nested class.

Related

Remove properties from JSON in some cases

I have a class like this:
public class SampleDto {
private String normalProperty1;
private String normalProperty2;
private String normalProperty3;
private String sensitiveProperty1;
private String sensitiveProperty2;
public String getNormalProperty1() {
return normalProperty1;
}
public void setNormalProperty1(String normalProperty1) {
this.normalProperty1 = normalProperty1;
}
public String getNormalProperty2() {
return normalProperty2;
}
public void setNormalProperty2(String normalProperty2) {
this.normalProperty2 = normalProperty2;
}
public String getNormalProperty3() {
return normalProperty3;
}
public void setNormalProperty3(String normalProperty3) {
this.normalProperty3 = normalProperty3;
}
public String getSensitiveProperty1() {
return sensitiveProperty1;
}
public void setSensitiveProperty1(String sensitiveProperty1) {
this.sensitiveProperty1 = sensitiveProperty1;
}
public String getSensitiveProperty2() {
return sensitiveProperty2;
}
public void setSensitiveProperty2(String sensitiveProperty2) {
this.sensitiveProperty2 = sensitiveProperty2;
}
}
There are parts in the application where i need to serialize it as it is because the object is in a secure environment.
But i need to store the json in a db and store it without the sensitiveProperties, I can't just ignore the properties because they are needed in the other processes.
I was thinking to use Jackson views to solve the problem but i don't know if there is something special in Jackson where I can say, every json object that has the property "sensitiveProperty1" set it to null.
I'm using Java and Jackson
I think that this site covers what you're looking for pretty well.
Essentially what you'll want to do is to add #JsonIgnoreProperties(value = { "intValue" }) at the class level or #JsonIgnore at the field level and then Jackson should take care of the rest for you.
In your case that would look something like:
public class SampleDto {
#JsonIgnore
private String normalProperty1;
private String normalProperty2;
...

Invalid property 'isDifferentLanguage' of bean class

I´m new to the Spring MVC Framework and I´m having a problem. When trying to bind the Information of a form to my controller the following error occurs:
"Invalid property 'isDifferentLanguage' of bean class [de.pwc.form.FPagesCheckForm]: Bean property 'isDifferentLanguage' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?"
My aim is to prove if some checkboxes are checked. Strangely enough this code works for the second and third checkbox of my .jsp file. But for the first checkbox "Different language in the documents?" it is not working despite I have implemented getter and setter of the property.
This is a part of my .jsp File:
Different language in the documents? <form:checkbox path="isDifferentLanguage"/><br/>
Only compareNumbers <form:checkbox path="onlyCompareNumbers"/><br/>
Markup <form:checkbox path="markup"/><br/>
This is my form:
private int startPageOld;
private int endPageOld;
private int startPageNew;
private int endPageNew;
private boolean isDifferentLanguage;
private boolean onlyCompareNumbers;
private boolean markup;
// Files
private CommonsMultipartFile[] fileDatas;
public CommonsMultipartFile[] getFileDatas() {
return fileDatas;
}
public void setFileDatas(CommonsMultipartFile[] fileDatas) {
this.fileDatas = fileDatas;
}
public int getStartPageOld() {
return startPageOld;
}
public void setStartPageOld(int startPageOld) {
this.startPageOld = startPageOld;
}
public int getStartPageNew() {
return startPageNew;
}
public void setStartPageNew(int startPageNew) {
this.startPageNew = startPageNew;
}
public int getEndPageOld() {
return endPageOld;
}
public void setEndPageOld(int endPageOld) {
this.endPageOld = endPageOld;
}
public int getEndPageNew() {
return endPageNew;
}
public void setEndPageNew(int endPageNew) {
this.endPageNew = endPageNew;
}
public boolean isOnlyCompareNumbers() {
return onlyCompareNumbers;
}
public void setOnlyCompareNumbers(boolean onlyCompareNumbers) {
this.onlyCompareNumbers = onlyCompareNumbers;
}
public boolean isMarkup() {
return markup;
}
public void setMarkup(boolean markup) {
this.markup = markup;
}
public boolean isDifferentLanguage() {
return isDifferentLanguage;
}
public void setDifferentLanguage(boolean isDifferentLanguage) {
this.isDifferentLanguage = isDifferentLanguage;
}
If you need more information just tell me ! Thanks for your help!

Android - can't store a list in the Application class instance

I'm trying to store a list in the Application class instance as a global variable in one of my Android applications. Below is my Application class code:
public class DefectsApplication extends Application{
private NormalUser normalUser;
private ArrayList<Complaint> complaintList;
public String getTestString() {
return testString;
}
public void setTestString(String testString) {
this.testString = testString;
}
private String testString;
public NormalUser getNormalUser() {
return normalUser;
}
public void setNormalUser(NormalUser normalUser) {
this.normalUser = normalUser;
}
public ArrayList<Complaint> getComplaintList() {
return complaintList;
}
public void setComplaintList(ArrayList<Complaint> m_complaints) {
this.complaintList = complaintList;
}
}
Below is my code which is trying to access the fields from the Application class instance:
DefectsApplication defectsApplication = ((DefectsApplication)getApplicationContext());
defectsApplication.setComplaintList(m_complaints);
defectsApplication.setTestString("urghhhhhhhhh");
ArrayList<Complaint> complaintList = defectsApplication.getComplaintList();
String s = defectsApplication.getTestString();
In the above code, m_complaints is a list of objects. When I try to store a String, it works. But for a list, it doesn't. Please, help me to resolve this issue.
Probably, a typo is taking place:
public void setComplaintList(ArrayList<Complaint> m_complaints) {
this.complaintList = complaintList;
}
You're setting this.complaintList to itself which is initially null. Try
public void setComplaintList(ArrayList<Complaint> m_complaints) {
this.complaintList = m_complaints;
}

implicit QueryStringBindable java.util.List[models.InputTimeSheetDataStore]

Hi i am trying to pass a list of objects of type models.InputTimeSheetDataStore from view to application.java and i am getting No QueryString binder found for type java.util.List[models.InputTimeSheetDataStore]. Try to implement an implicit QueryStringBindable for this type error
in application.java i am passing list of object to view
InputTimeSheetDataStore ITSDS= new InputTimeSheetDataStore();
ITSDS.ConsultantName=EmployeeFilter;
ITSDS.Client=ClientFilter;
ITSDS.Project=ProjectFilter;
ITSDS.Role=EmployeeRoleFilter;
ITSDS.Task=Task;
ITSDS.TimeSheetDate=TimeSheetDate;
ITSDS.Hours=TaskHours;
ITSDS.IsBilled=IsBilled;
ITSDS.Workplace=WorkPlace;
InputTimeSheetList.add(ITSDS);
return ok(TimeSheetInput.render(Consultant.PopulateConsultant(),Client.PopulateClient(),Project.PopulateProject(ClientFilter),
Consultant.PopulateConsultantRole(),Consultant.ConsultantRoleRate(EmployeeRoleFilter),InputTimeSheetList));
in view i am passing that object back to application.java
#(EmployeeList:java.util.List[String],ClientList:java.util.List[String],
ProjectList:java.util.List[String],EmployeeRoleList: java.util.List[String],Rate:String,
CurrentPage:List[InputTimeSheetDataStore])
<form id="TimeSheetEntryForm" name="TimeSheetEntryForm" action="#{routes.Application.save("name","name","name","name","name","name","name","name","name",CurrentPage)}" method="GET">
<code.....>
here is my class file
InputTimeSheetDataStore.java
package models;
public class InputTimeSheetDataStore {
public String ConsultantName;
public String Client;
public String Project;
public String Role;
public String Task;
public String TimeSheetDate;
public String Hours;
public String IsBilled;
public String Workplace;
public String getConsultantName(){
return this.ConsultantName;
}
public String getClient(){
return this.Client;
}
public String getProject(){
return this.Project;
}
public String getRole(){
return this.Role;
}
public String getTask(){
return this.Task;
}
public String getTimeSheetDate(){
return this.TimeSheetDate;
}
public String getHours(){
return this.Hours;
}
public String getIsBilled(){
return this.IsBilled;
}
public String getWorkPlace(){
return this.Workplace;
}
}
my routes is
GET /Application/save controllers.Application.save(EmployeeFilter:String,ClientFilter:String,ProjectFilter:String, EmployeeRoleFilter:String,Task:String,TaskHours:String,TimeSheetDate:String,IsBilled:String,WorkPlace:String,CurrentPage:java.util.List[models.InputTimeSheetDataStore])
can someone help me with the implicit querybinder of type InputTimeSheetDataStore
Thanks in advance
Hi this is the example implementation of QueryStringbindable:
public class InputTimeSheetDataStore implements QueryStringBindable<InputTimeSheetDataStore> {
public String consultantName, client, project;
#Override
public Optional bind(String key, Map data) {
if (data.containsKey("consultantName")) {
this. consultantName = data.get("consultantName").toString();
}
if (data.containsKey("client")) {
this.client = data.get("client").toString();
}
if (data.containsKey("project")) {
this.project = data.get("project").toString();
}
return Optional.of(this);
}
#Override
public String unbind(String key) {
return null;
}
#Override
public String javascriptUnbind() {
return null;
}
}
Extra tips for you, when writing a programming language, make sure you are following the code convention. For example: the convention in Java syntax you must write variable with lower case for the first character;
public String ConsultantName; // this is wrong
public String consultantName; //this is right
Hope it helps.

How to Globally Access Java Bean Class?

I have one java bean class set values in parsing .My requirement to access this class
Globally but give null pointer exception when use its instance static and assign value in that.Code is as:-
private String Responce;
private String Error;
private String Url;
private SIPModle objsip;
private VMModle objvmmodle;
private ArrayList<CustompadModle> objcmodlelist = new ArrayList<CustompadModle>();
private SettingModle objsettingmodle;
private WifiModle objwifimodle;
public String getResponce() {
return Responce;
}
public void setResponce(String responce) {
Responce = responce;
}
public String getError() {
return Error;
}
public void setError(String error) {
Error = error;
}
public ArrayList<CustompadModle> getObjcmodlelist() {
return objcmodlelist;
}
public void setObjcmodlelist(CustompadModle objcmodlelist) {
this.objcmodlelist.add(objcmodlelist);
}
public String getUrl() {
return Url;
}
public void setUrl(String url) {
Url = url;
}
public SIPModle getObjsip() {
return objsip;
}
public void setObjsip(SIPModle objsip) {
this.objsip = objsip;
}
public VMModle getObjvmmodle() {
return objvmmodle;
}
public void setObjvmmodle(VMModle objvmmodle) {
this.objvmmodle = objvmmodle;
}
public SettingModle getObjsettingmodle() {
return objsettingmodle;
}
public void setObjsettingmodle(SettingModle objsettingmodle) {
this.objsettingmodle = objsettingmodle;
}
public WifiModle getObjwifimodle() {
return objwifimodle;
}
public void setObjwifimodle(WifiModle objwifimodle) {
this.objwifimodle = objwifimodle;
}
My requirement is that globle access this bean class globlly.Anyone suggest me
I am getting this object in doinbackground and assign in static object of that class as:
AsyncTask {
ProgressDialog objprogress = new ProgressDialog(
UserSettingConfiguration.this);
ApplicationRequestHandler objhandler = new ApplicationRequestHandler();
#Override
protected void onPreExecute() {
this.objprogress.setMessage("Please Wait While Loading...");
this.objprogress.isShowing();
}
#Override
protected ConfigurationSttingModle doInBackground(String... params) {
objconfigsetting = objhandler.getConfigurationSetting(params[0],
params[1], params[2], params[3]);
return objconfigsetting;
}
#Override
protected void onPostExecute(ConfigurationSttingModle result) {
if (this.objprogress.isShowing()) {
this.objprogress.dismiss();
}
}
}
And use in a class Advance as;-
private static HashMap<String, String> SUMMARIES = new HashMap<String, String>() {
private static final long serialVersionUID = 3055562364235868653L;
{
String server = UserSettingConfiguration.objconfigsetting.getObjsip().getServer();
String displayname1 = UserSettingConfiguration.objconfigsetting.getObjsip().getUser();
String user = UserSettingConfiguration.objconfigsetting.getObjsip().getUser();
String password= UserSettingConfiguration.objconfigsetting.getObjsip().getPassword();
put(FIELD_DISPLAY_NAME,displayname1);// "90901");
put(FIELD_CALLER_ID,displayname1); //"90901");
put(FIELD_SERVER,server);
put(FIELD_USERNAME,user);
put(FIELD_AUTH_ID, "207");
put(FIELD_PASSWORD,password);
put(FIELD_PROXY, null);
}
};
I think that a singleton pattern will match your requirements.
You must add this code:
private static MyJavaBeanClass = null;
public static MyJavaBeanClass getInstance(){
if(null == instance){
instance = new MyJavaBeanClass();
}
return instance;
}
You must change MyJavaBeanClass for the name of your class, and then you can invoke it anywhere like this:
MyJavaBeanClass.getInstance().setError("Testing");

Categories