Spring formatters for nulls values - java

I have a select control in my view and i want to "bind" or "format" the item "Please select an item" to a null value in my backing form object.
Is that possible? because i know that making some traditional formatting doesn't work properly.
Instead i saw an example of making changes in class GenericConversionService and using that class instead of the original. (unfortunately i can't find it anymore)
This is the exception when i return a null value from my formatter class:
org.springframework.core.convert.ConversionFailedException: Unable to convert value "-1" from type 'java.lang.String' to type 'com.tesisutn.restsoft.dominio.articulo.Marca'; nested exception is java.lang.NullPointerException
Thanks a lot in advance!

It looks like your form is sending a -1 to indicate that "Please select an item" is selected.
First thing you can try is to send noting. Then the binding (when the form is send to the server) will work, the according command object field will be null.
<option value="">Please select an item</option>
If this is no option for you (for example because you use dojo), then you need to implement your own property editor or conversation (Spring 3.0)

Related

Morphia update document with undefined number of fields

I'm working on a small project using morphia for MongoDB. I want to know what is the best way to update a document without knowing first hand what field to update. Say for example, I have a form, after having saved to database I might want to come back and change some fields but I haven't decided yet what to change.
My current solution is doing an update for all fields whether it is changed or not and of course it throws null exception. Morphia complains on null value update.
My code looks like this:
Query<Project> q = datastore.find(Project.class, "_id", projectToUpdate.getId());
UpdateOperations<Project> update
= datastore.createUpdateOperations(Project.class)
.set("name", updatedProject.getName())
.set("deadline", updatedProject.getDeadline())
.set("priority", updatedProject.getPriority())
.set("completion", updatedProject.getCompletion())
.set("description", updatedProject.getDescription())
.set("projectManager", updatedProject.getPM())
.set("collaborators", updatedProject.getAllCollaborators())
.set("teams", updatedProject.getAllTeams())
.set("userStories", updatedProject.getUserStories())
.set("log", updatedProject.getLog());
datastore.findAndModify(q, update);
Exception
Exception in thread "main" org.mongodb.morphia.query.QueryException: Value cannot be null.
at org.mongodb.morphia.query.UpdateOpsImpl.set(UpdateOpsImpl.java:220)
at controllers.QueryProjects.updateProject(QueryProjects.java:78)
at controllers.DBConnection.TestMongo(DBConnection.java:152)
at penelope.Main.main(Main.java:12)
I was thinking about using delegate/event handler to update each field individually but I'm afraid that might degrade the performance.
You should just use datastore.save(). See an example at http://mongodb.github.io/morphia/1.3/getting-started/quick-tour/

SharedPreferences getString NULL parameter

Will I receive an error (Exception) on some devices if I set the second parameter of SharedPreferences.getString NULL?
SharedPreferences settings = ...
String data = settings.getString(_.PREFIX , null);
Will it cause an exception or an error on at least one device? Or I have to wrap this part of code in try-catch block?
If you are asking if you will get an exception if you set the second parameter to null, the answer is no (at least not unless you reference the result without first checking it is not null). The second parameter in the getString() method is the default value (i.e. the value that will be returned if there is nothing found for your prefix. So, it is perfectly acceptable to set null as your default value, as long as you realize (and account for) the fact that the value returned by your getString() could be null.
String data = settings.getString(_.PREFIX , null/Null here is default value/);
null - u can receive when your SraredPreferences have not this item(For example if u call/get this string before setting to this field any info or user clear cash of application from settings of device). I think it's can be normal situation, and u can remove "null" with some default value if you hope to got it(some emum field).
If u don't suppose get null validate data before using.
I thin'k your app must be ready get both variant, because user can change normal workflow.

Change a property dynamically from the payload on mule esb

I'm developing a bookstore in mule esb. When I check the quantity from a book order is available with the database, I want to set a property from payload. The payload has several properties from the book (isbn, quantity, prize, avalability), so the last one in this case I want to set to true (is attribute boolean type).
Is there any way to do that with a connector?
not really sure what you're trying to do but...
To change the payload of a message there several ways the easies one being just using a MEL expression.
Say your payload is a map(for you say you toke it from the DB) then you could just do:
<expression-transformer expression="#[payload['avalability']='your value']"
Now you say you wanted that value to be true then the code should look like:
<expression-transformer expression="#[payload['avalability']=true]
MEL will put a boolean true for you there.
Finally to update the DB you should:
<db:update config-ref="Database" bulkMode="true" doc:name="insert contacts to Database">
<db:parameterized-query>
UPDATE books
SET 'avalability' = #[payload['avalability']]
WHERE 'isbn'= #[payload['isbn']]
</db:parameterized-query>
</db:update>
If you want more example about working with DB please check:
https://www.mulesoft.com/library#!/?types=template&filters=Database
You can set the propertyName dynamically using:
#[message.outboundProperties.propertyName]=any value

How to use ADF CreateInsert when having a sequence generated ID trigger in my database?

I'm having an exception all the time when trying to add a new entity :
All fields are filled in, only ID is missing.
I'm adding an image :
For solving this problem it is necessary to change the type of the entity. Open the Entity Object, go to attributes tab, select ID field and in property inspector tab change Type NUMBER to DBSequence.
In the case that you have an ID of type BigDecimal, it's necessary to double click the attribute to make appear "Edit Attribute" Popup, then in browse for the Type label, and search oracle.jpo.doman.DBSequence, then do OK.

DisplayTag error

I am using the DisplayTag with pagination to display a List objects. The Transactions has a property called 'company' / getCompany() which is the Company object. The Company object contains a String called 'name' / getName().
My code looks like this:
<display:table name="${transactions}" id="transaction" pagesize="2" defaultsort="1">
<display:column property="id" title="ID" href="showTransactionDetails.html" paramId="id" />
<display:column property="company.name" title="Company Name" sortable="true" >
<display:column property="status" title="Status" sortable="true">
</display:table>
Here is the strange part.... Everything works great when the first page is displayed and there are a total of 11 pages with each page containing 2 records.
I can click on a page number and see the page advance. But for some strange reason, when I click on page (2-4) I get an exception:
org.apache.jasper.JasperException:
javax.servlet.ServletException:
javax.servlet.jsp.JspException:
Exception: [.LookupUtil] Error looking
up property "company.name" in object
type
"com.replacements.entity.Transaction".
Cause: null
(It's also important to note that all of the Transaction records contain a value for the company.name since its a required field in my DB)
Is it possible that the company is null. That is, you have a transaction with no company in the database.
I solved it by changing the company property in Hibernate mapping to "lazy=false"
I'm still not sure why the pagination worked from some pages and not all. But this fixed it.
Thank you all for your ideas.
As #Vincent says, likely company is null. You may have a value in your database, but maybe there is an issue where your Transaction class isn't properly reading the db value and setting its company member. Have you tried setting a breakpoint and looking at the Transaction instance?
My first guess is that there is an empty company list. I would suggest you print dump to output your transaction results before they get to the display part.
If that’s not the problem I’ve seen display problems caused by special characters. One of the company names might contain a control character or some other non-displayable character.
Try changing the name="${transactions}" in the display:table tag to name="transactions".
Assuming you have the transactions collection in the session or request or whatever.
The exception message literally tells that the Transaction is null. Thus, there's apparently a null item in the transaction list behind ${transactions}. Look like a fault in the loading/populating logic of the transaction list. Maybe the last item is null? Or maybe the list is request scoped and dependent from some request parameters which are missing in the subsequent request so that loading/populating the list failed?
For the interested, if Company was null as some suggests, EL would not have error'ed that way. It would have mentioned object type Company instead.
A requestURI tag...like so.... requestURI="
Make sure you have setters and getters methods for all attributes in your class and names matching attributes names.

Categories

Resources