Saving Data in Wicket without PropertyModels - java

I am attempting to build a web application using Wicket and OrientDB. I am trying to avoid writing/maintaining a flat Java class that represents each Class of vertex as a POJO (like an ORM). Rather, I am sending the vertices themselves all the way to the web layer. I access the properties via:
vertex.getProperty("propertyName");
Meaning that the properties themselves are not member variables of Vertex and thus cannot be accessed through normal getters/setters. I am running into an issue with Wicket because many components depend on a "PropertyModel" style implementation where you pass in a Model that represents one of the member variables of the class you're working with, and the data is stored in that member variable.
I have this DataView that pulls the properties of a vertex out into a Label and a TextField.
public VertexViewer(final PageParameters pageParameters, OrientVertex vertex)
this.vertex = vertex;
this.properties = this.vertex.getProperties();
List<String> keyList = new ArrayList<>();
keyList.addAll(this.vertex.getPropertyKeys());
final DataView<String> propertiesView = new DataView<String>("properties", new ListDataProvider<>(keyList)) {
#Override
protected void populateItem(Item item) {
String key = item.getModelObject().toString();
item.add(new Label("property_name", key));
item.add(new TextField<String>("edit_field", new Model<String>(properties.get(key).toString())));
}
};
add(propertiesViewer);
}
And the HTML:
<wicket:extend>
<div wicket:id="properties" style="display: table-row;">
<div wicket:id="property_name" style="display: table-cell;"></div>
<input wicket:id="edit_field" type="text" style="display: table-cell;"/>
</div>
</wicket:extend>
This renders exactly how I want it to, but does anyone have any recommendations on how I can save the data that is being changed in the TextFields? I can't use a model of a member variable like I would normally do on a Form because I don't ever know exactly what/how many properties are going to be in a vertex. Am I going about this in completely the wrong way? Any help is much appreciated.

Take a look at https://github.com/OrienteerDW/wicket-orientdb.
The developer of this library also created https://issues.apache.org/jira/browse/WICKET-5623, but this improvement didn't get much support. Please feel free to vote for it if you think it is needed.

So, after much trial and error (mostly error) I found a patttern that gets around Wicket's NoSQL deficiencies and doesn't involve manually creating member variables to make Models of.
I get the Map<String, Object> from vertex.getProperties() and then create a copy of it.
private Map<String, Object> realProperties = baseVertex.getProperties();
private Map<String, Object> dummyProperties = new HashMap<>();
dummyProperties.putAll(realProperties);
This allows me to use the values in the dummyProperties as Models to store data.
List<String> keyList = new ArrayList<>();
keyList.addAll(getDummyProperties().keySet());
final DataView<String> propertiesView = new DataView<String>("properties", new ListDataProvider<>(keyList)) {
#Override
protected void populateItem(Item item) {
String key = item.getModelObject().toString();
item.add(new Label("property_name", key));
item.add(new TextField<String>("edit_field", new PropertyModel<String>(getDummyProperties(), key)));
}
};
From there, I put the DataView on a Form with an AjaxButton that compares the potential new values in the TextFields to the original values through some convoluted looping.
AjaxButton saveButton = new AjaxButton("saveButton") {
#Override
protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
super.onSubmit(target, form);
Map<String, Object> changedProperties = new HashMap<>();
// for each entry in the dummy values linked via PropertyModel to the TextField input
for (Map.Entry<String,Object> dummyEntry : getDummyProperties().entrySet()) {
// for each entry in the
for (Map.Entry<String,Object> realEntry : baseVertex.getProperties().entrySet()) {
// if the keys match
if (dummyEntry.getKey().equals(realEntry.getKey())) {
// if the value has changed
if (!dummyEntry.getValue().equals(realEntry.getValue())){
// value in textField differs from value in database
changedProperties.put(dummyEntry.getKey(),dummyEntry.getValue());
}
}
}
}
DBWorker worker = new DBWorker();
// perform the update
worker.updateVertex(recordID, changedProperties);
// pull the fresh vertex out and pass it to the page again
setResponsePage(new VertexViewer(new PageParameters(), new CustomVertex(worker.getVertexByID(recordID))));
}
};
This ends up evaluating the new values against the old, writing the new ones back to the database, pulling back the fresh vertex and calling a new ViewerPage that accepts the updated vertex as an argument.
This works as intended, is generic to the class of vertex/number of properties, and prevents me from having to maintain ORM style Classes for each vertex class.

Related

How come I can't have several hashmaps as member variable in my Custom object

I'd like to have a firestore Document with several hashmaps as data objects on the server-side.
On the client-side (Android) I am using a POJO as a custom object to store and organize all data requested from firestore. This POJO implements Parcelable to be able to pass it through intent from an activity to another.
My POJO looks like this:
public class Session implements Parcelable {
private Map<String, Boolean> firstHashmap;
private Map<String, Boolean> secondHashmap;
private Map<String, String> thirdHashmap;
// constructors (first I notice that I can only create 1 constructor for a hashmap as a parameter, I wonder why)
// getters and setters
#Override
public int describeContents() {
return 0;
}
// Write the object's data to the passed-in Parcel
#Override
public void writeToParcel(Parcel out, int flags) {
if (firstHashmap != null) {
Bundle firstBundle = new Bundle();
for (Map.Entry<String, Boolean> entry : firstHashmap.entrySet()) {
firstBundle.putBoolean(entry.getKey(), entry.getValue());
}
out.writeBundle(firstBundle);
}
// same thing for secondHashmap
// same thing for thirdHashmap
public static final Parcelable.Creator<Session> CREATOR = new
Parcelable.Creator<Session>() {
public Session createFromParcel(Parcel in){
return new Session(in);
}
#Override
public Session[] newArray(int size) {
return new Session[size];
}
};
// Constructor that takes a Parcel and gives you an object populated with it's values
private Session(Parcel in) {
Bundle firstBundle = new Bundle();
firstBundle = in.readBundle(getClass().getClassLoader());
Map<String, Boolean> tempFirstHashmap = new HashMap<>();
for (String key: firstBundle.keySet()){
tempFirstHashmap.put(key, firstBundle.getBoolean(key));
}
firstHashmap = firstBundle;
// same thing for secondHashmap
// same thing for thirdHashmap
}
}
It worked fine while I was having the same value data type in my hashmap i.e. Boolean but then I introduced thirdHashmap with String and everything went wrong: for instance when firstHashmap is null on the firestore document and thirdHashmap is not, I'm having Strings deserialized to my firstHashmap causing RuntimeError.
To sum up I am having some issue to get data from firestore and put them in the right place. If anyone could give me a clue it would be much appreciated :)
Thank you very much!
To save you a lot of time and effort you can use a plugin to generate parceling code for you inside your POJO if you're using android studio just go to File>settings>plugins and there search for plugins and install it.
then go to your POJO and clear it from everything except your member variable(HashMap variables) after that just by right clicking your mouse on inside your class file and from your opened window click on generate you will find a new tab added to the last of the window called Parcelable click on it and choose what member variables you want to parcel.
Note: If i were in your shoes i would delete all my firestore data on server-side and on client-side when a new change made to my POJO specially when I delete or change member variable, I hope this answer help you.

Set Value to ComboBoxItem smartgwt

In drop down I get like this. find image attached. Actually in coulmn of "Name" field both 'Name' and 'Description' are displaying as comma(,) separated.
final ComboBoxItem comboBoxItem = new ComboBoxItem("attributeTypeId","Attr. Type");
ListGridField nameField = new ListGridField("name", "Name");
ListGridField descField = new ListGridField("description","Description");
descField.setShowHover(true);
comboBoxItem.setPickListFields(nameField, descField);
comboBoxItem.setPickListWidth(200);
comboBoxItem.setFilterLocally(true);
comboBoxItem.setColSpan(2);
comboBoxItem.setAddUnknownValues(false);
comboBoxItem.setValueField(FieldNames.ID_FIELD);
comboBoxItem.setDisplayField(FieldNames.NAME_FIELD);
comboBoxItem.setAutoFetchData(true);
OptionListDataSource attrTypeds = OptionListDataSource.getInstance(FieldNames.ATTRIBUTE_TYPE_FIELD);
attrTypeds.fetchData(null, new DSCallback() {
#Override
public void execute(final DSResponse response, final Object rawData, final DSRequest request) {
Record[] recList = response.getData();
LinkedHashMap<String, String[]> dataLinkMap = new inkedHashMap<String,String[]>(); //LinkedHashMap<String,
dataLinkMap.put("0", new String[]{"Select",""});
for (Record record : recList) {
String attrId = record.getAttribute(FieldNames.ID_FIELD);
String attrName = record.getAttribute(FieldNames.NAME_FIELD);
String attrDesc = record.getAttribute(FieldNames.DESCRIPTION_FIELD);
dataLinkMap.put(attrId, new String[]{attrName,attrDesc});
}
comboBoxItem.setValueMap(dataLinkMap);
}
});
Screen Shot
Here is some sample code to achieve what I understand you want to achieve:
public class TestCases implements EntryPoint {
public void onModuleLoad() {
DataSource logDS = DataSource.get("yourDSName");
final DynamicForm form = new DynamicForm();
form.setWidth(550);
form.setNumCols(2);
ListGridField nameField = new ListGridField(FieldNames.NAME_FIELD);
ListGridField descriptionField = new ListGridField(FieldNames.NAME_DESCRIPTION);
LinkedHashMap<String,String> hashMap = new LinkedHashMap<String,String>();
hashMap.put("-1", "Select");
ComboBoxItem myItem = new ComboBoxItem();
myItem.setTitle("ComboBox");
myItem.setOptionDataSource(logDS);
myItem.setDisplayField("category");
myItem.setValueField(FieldNames.ID_FIELD);
myItem.setSpecialValues(hashMap);
myItem.setPickListWidth(300);
myItem.setPickListFields(nameField, descriptionField);
form.setItems(myItem);
form.draw();
}
}
Notice:
In order to display various fields, you need to use setPickListFields with the reference to those fields.
You don't need to call fetch() on the DataSource itself. This is done automatically for you when you use DataBound components like ComoBoxItem.
You can add additional empty values using setSpecialValues() without modifying your DSResponse data (which is why you don't need to use fetch() directly).
EDIT
The problem you are having is that the ValueMap, which is just a Map (in other words, just a group of key/value pairs), that you are providing to the ComboBoxItem is not the same as the Record[] object provided directly by the DataSource, which in essence is just a List made of several Maps, each representing a field name and its value. This way, besides the value field, you can provide several fields to the ComboBoxItem for display purposes, like Name and Description, in your particular case.
From looking at the API, it looks to me that you can't provide a Record[] manually to the ComboBoxItem, so either you get the data via DMI (which for me is the easiest) or other method that allows you to modify and return the required response from the server automatically to the ComboBoxItem by using the data binding capabilities, or you stick to showing just the "values" (which is what you are getting right now, but off course you could format the data better).
What I mean with formatting is that if you choose to go with your original approach of using setValueMap(), you need to provide a Map where each entry in the Map is just a value on the ComboBoxItem and its respective display "text", which can be any String combining the values of several other fields, and formatted as desired using String concatenation (for instance, you could make it
nameField + ": " + descriptionField
But this is as good as it gets with this approach.
Now, via a DMI you would need to define the server class that would provide the properly-formatted data in you datasource descriptor (ds.xml file):
<operationBindings>
<operationBinding operationType="fetch" serverMethod="fetchComboBoxData">
<serverObject lookupStyle="new" className="com.myApp.ComboBoxDMI"/>
</operationBinding>
</operationBindings>
And then create the class and method to provide what you need:
public class ComboBoxDMI {
public DSResponse fetchComboBoxData(DSRequest dsRequest) throws Exception {
DSResponse response = dsRequest.execute();
if (response.statusIsSuccess()) {
#SuppressWarnings("unchecked")
List<Map<String, Object>> recList = response.getRecords();
List<Map<String, Object>> comboBoxList = new ArrayList<Map<String,Object>>();
// Add here the new record... for each field in your DataSource, you need to set a Map
// with the key being the field name and the value being the field value. So you need
// 1 Map entry per field. All your Map entries form 1 record, and that's what you add
// to your List of Maps
return constructDSResponse(comboBoxList);
}
return response;
}
private DSResponse constructDSResponse(List<Map<String, Object>> comboBoxList) {
DSResponse response = new DSResponse();
int totalRows = comboBoxList.size();
response.setStartRow(totalRows > 0 ? 1 : 0);
response.setEndRow(totalRows);
response.setTotalRows(totalRows);
response.setData(comboBoxList);
return response;
}
}
Finally, you can follow the original approach I suggest in my original answer, but now you don't need to use the setSpecialValues API, which your version doesn't support.

Accessing arbitrary number of fields in a Wicket form validator

I need to validate something about several Wicket input fields of type TextField<BigDecimal> (namely that the sum of percentages is 100). There are one to many such input fields; thing is, I don't know in advance how many.
(simplified example)
private class PercentageValidator extends AbstractFormValidator {
#Override
public FormComponent<?>[] getDependentFormComponents() {
// ...
}
#Override
public void validate(Form<?> form) {
List<TextField<BigDecimal>> fields = // TODO
// the actual validation where the value of every field is needed
}
}
Java code for the ListView:
ListView<?> listView = new ListView<PropertyShare>("shares", shares) {
#Override
protected void populateItem(ListItem<PropertyShare> item) {
// ...
item.add(new TextField<BigDecimal>("share", ... model ...));
}
};
HTML:
<tr wicket:id="shares">
<td> ... </td>
<td>
<input wicket:id="share" type="text" size="4"> %
</td>
</tr>
I tried keeping every TextField in a collection on the Page, but this approach fails as the populateItem() method of the enclosing ListView gets called not only the the Page is first created, so duplicate fields get added to the collection. (I couldn't figure out an easy way to keep it duplicate-free.)
The fact that ListView is used also seems to somewhat complicate finding the fields from the form object in the validate() method. I suppose I need to get the ListView with form.get("shares") and iterate through its children?
What's the "right way" to access any number of fields enclosed by a repeater such as ListView?
An alternative approach would be to subclass TextField and then use a Visitor to pick out all the descendant components of your subclass.
This way you can avoid unchecked casting and you don't have to rely on the ids, which isn't a very robust approach.
Edit: in practice, it would look something like this:
The subclass:
private static class ShareField extends TextField<BigDecimal> {
// ...
}
Helper method that finds all ShareFields from the form:
private List<ShareField> findShareFields(Form form) {
final List<ShareField> fields = Lists.newArrayList();
form.visitChildren(ShareField.class, new IVisitor<ShareField>() {
#Override
public Object component(ShareField component) {
fields.add(component);
return CONTINUE_TRAVERSAL;
}
});
return fields;
}
Right, while writing the question, it dawned on me that simply looping through the children of form.get("shares") and getting the field with id "share" would probably work.
It indeed does. Here's a helper method that finds the "share" fields:
#SuppressWarnings("unchecked")
private List<TextField<BigDecimal>> findFields(Form form) {
List<TextField<BigDecimal>> fields = Lists.newArrayList();
MarkupContainer container = (MarkupContainer) form.get("shares");
for (Iterator<? extends Component> it = container.iterator(); it.hasNext();) {
MarkupContainer c = (MarkupContainer) it.next();
fields.add((TextField<BigDecimal>) c.get("share"));
}
return fields;
}
However, there are three somewhat ugly casts in the above method, and one of those (Component -> TextField<BigDecimal>) produces an "unchecked cast" warning.
If you can clean up this solution, or know of better approaches, feel free to comment or post other answers!
As far I see you did not set the reuse items property on the list view; from the java doc:
If true re-rendering the list view is more efficient if the windows doesn't get changed at all or if it gets scrolled (compared to paging). But if you modify the listView model object, than you must manually call listView.removeAll() in order to rebuild the ListItems. If you nest a ListView in a Form, ALLWAYS set this property to true, as otherwise validation will not work properly.
However you also can iterate over the children of the listview with a Visitor. Wicket always keeps track of the components you added of the view.

PropertyModel Expression's Exception : org.apache.wicket.WicketRuntimeException: No get method defined for class:

I used PropertyModel as the part of my DropDownChoice as following:
List<String> choices = Arrays.asList(new String[] { "Library", "School Office", "Science Dept" });
String selected = "Library";
DropDownChoice<String> serviceDDC =
new DropDownChoice<String>("service", new PropertyModel(this, "choices.0"), choices);
Somehow I've got this exception thown:
caused by: org.apache.wicket.WicketRuntimeException: No get method defined for class: class com.samoo.tool.pages.CreatePrintingJob expression: choices
at org.apache.wicket.util.lang.PropertyResolver.getGetAndSetter(PropertyResolver.java:481)
at org.apache.wicket.util.lang.PropertyResolver.getObjectAndGetSetter(PropertyResolver.java:332)
at org.apache.wicket.util.lang.PropertyResolver.getObjectAndGetSetter(PropertyResolver.java:242)
at org.apache.wicket.util.lang.PropertyResolver.getValue(PropertyResolver.java:95)
at org.apache.wicket.model.AbstractPropertyModel.getObject(AbstractPropertyModel.java:130)
at org.apache.wicket.Component.getDefaultModelObject(Component.java:1724)
....
I know that there's something wrong with the expression. I've been trying different parameter inputs but it still doesn't work. Could anyone help?
Since you're using PropertyModel(this, "choices.0"), Wicket is trying to find a property named choices via reflection through a method getChoices() of the class declaring the PropertyModel. This method doesn't seem to exist in com.samoo.tool.pages.CreatePrintingJob, as the exception is stating.
Also, if that 0 is an index, you should be accessing it with the [index] expression, as this JIRA issue suggests: PropertyModel does not support index only property ("[0]")
However, it seems you want to initialize the DropDownChoice to the first element of choices. But What Wicket will do if you set the DropDownChoice's Model to PropertyModel(this, "choices.[0"]) will be mapping the selection of this DropDownChoice in the following way:
At form rendering time to present the (pre)selected choice, it will use the first element in the choices list.
At form submission time to store the user selected value, it will store the selection in the first position of the choices list.
Summarising, the backing object representing the DropDownChoice's selection would be the first element in the choices list.
So, you'll probably want to use a whole different Model, independent from the choices list, for the backing object representing the DDC's selection.
List<String> choices = Arrays.asList(new String[] { "Library", "School Office",
"Science Dept" });
String selected = "Library";
IModel dropdownModel = new Model<String>(choices[0]);
DropDownChoice<String> serviceDDC =
new DropDownChoice<String>("service", dropdownModel, choices);
You might find the following links useful:
Using the DropDownChoice component
Working with Wicket Models
you are declaring choices inside the method, in order to get the PropertyModel to work you need to declare it on a class level not on a method level. As #Xavi López pointed out the espression is not corret you nedd to use choices.[0]
It is good idea to use IModel instead of PropertyMOdel.PropertyModel has big problems in refactoring. In my cases I did it and the problems solved properly.Also I have override the toString() of my Topic object.
topicDropDown = new DropDownChoice<Topic>("topicOptions", new IModel<Topic>() {
#Override
public Topic getObject() {
return top;
}
#Override
public void setObject(Topic t) {
top = t;
}
#Override
public void detach() {
}
}, new LoadableDetachableModel<List<Topic>>() {
#Override
protected List<Topic> load() {
List<Topic> topics = top.getAllTopics();
return topics;
}
});

In Wicket, why isn't my results refreshing after form submission?

I'm struggling with a very basic Wicket issue. I'm trying to query a backend database, but can't get the results to display. Below is the code I'm using. currentQuery and currentResult is correctly updated after submission, but my SearchResults class is never rerendered with the new data in currentResults. I suppose that the results class just doesn't notice that the model has in fact been updated. I've been experimenting with modelChanged, but can't get it to work. I'm a bit new to Wicket, so I'm probably doing something fundamental completely wrong. Any help is much appreciated!
public class SearchPage extends WebPage {
Query currentQuery = new Query();
Result currentResult = new Result();
public SearchPage() {
add(new SearchForm("searchForm", new CompoundPropertyModel<Query>(currentQuery)));
add(new SearchResults("searchResults", new PropertyModel<List<Hit>>(currentResult, "hits")));
}
public void doSearch(Query Query) {
currentResult = getResults(query);
}
public class SearchForm extends Form<Query> {
public SearchForm(String id, CompoundPropertyModel<Query> model) {
super(id, model);
add(new TextField<String>("query"));
}
protected void onSubmit() {
super.onSubmit();
doSearch(currentQuery);
}
}
public class SearchResults extends WebMarkupContainer {
public SearchResults(String id, PropertyModel<List<Hit>> model) {
super(id, model);
add(new ListView<Hit>("hit", model) {
protected void populateItem(ListItem<Hit> item) {
item.add(new Label("column", item.getModelObject().getColumnValue("column")));
}
});
}
}
}
PropertyModel uses reflection to look up the named property on a given target object instance. When you constructed the PropertyModel, you passed it a specific instance of Result, i.e. the new Result() from SearchPage's constructor. The PropertyModel will continue to hold a reference to that same Result instance from render to render of this page, serializing the Result at the end and then deserializing the Result at the start of each new request cycle (page view). The fact that you later change the page's currentResult variable to reference a different Result instance does not affect which Result instance the PropertyModel uses to look up its model value. Your PropertyModel does not care what currentResult later refers to.
There are two possible solutions that I can think of off the top of my head.
Have the PropertyModel read hits from the actual current value of the Page's currentResult variable:
new PropertyModel<List<Hit>>(SearchPage.this, "currentResult.hits")
Use a LoadableDetachableModel to load hits once per request cycle/page view:
new LoadableDetachableModel<List<Hit>>()
{
protected Object load()
{
return getResults(currentQuery);
}
}
Note that a LoadableDetachableModel has to be detached at the end of the request cycle or it will never again call getObject() to recalculate the List<Hit>. That said, since your code shows you'd be using it as the default model of the SearchResults component, the SearchResults component would detach the model for you at the end of the request cycle automatically.
I got it working. This seems to be the offending row:
add(new SearchResults("searchResults", new PropertyModel<List<Hit>>(currentResult, "hits")));
The type of the PropertyModel, i.e. List<Hit>, must have been making the model static. So the only data SearchResults ever saw was the initial object, which was empty.
I changed the line to the below, and updated SearchResult accordingly.
add(new SearchResults("searchResults", new Model<Result>(currentResult, "hits")));
If anyone can explain this further, or feel that I'm incorrect, please comment! In any case, I'm marking my own answer as correct as this solved the problem.

Categories