zk MVC null check text box value data-bind annotation - zk

My page has text-boxes whose value attribute annotated as #{bean.innerbean.prop}. Here if inner bean obj is null , it throws null exception since when it does "nullobj.prop" evaluation.
I'm using MVC 'not MVVM'. How to do null check on bean.innerbean then load or save value from bean.innerbean.prop.
'bean' obj I set it as page scope.it has other props as name,tooltip etc which obviously work well to other respective annotated components....
Please let me know is the way to do "empty", == null on #{……} ..
Im using annotation(in MVC long since in my app) to bind user entered-value to be saved on the object as well loading it on AnnotateDataBinder.loadAlll() method being invoke from AnnotateDataBinderInit class doAfterCompose(). My question anyway to do null check on the annotated bean value..... EL Expression evaluated once on the page load phase thereafter it wont work

First of all, if you are using MVC you use the wrong annotation.
${} is used for el expressions, while #{} is used for data binding, witch is MVVM.
So if you use the correct expression you can do :
${empty bean.innerbean?'empty':bean.innerbean.prop}

Related

JPA entity modified by reflection not merged by entity manager

I need to check if my actual entity is different from the old one.
I use reflection because my method must be standardized.
For each column, I update the value if and only if it's not null (because i read it from a CSV and a column may be not specified).
for(Field column : fields){
column.setAccessible(true);
Object newValue = column.get(myObject);
if( newValue != null && !newValue.equals(column.get(oldObject))){
column.set(oldObject, newValue);
}
}
this.entitymanager.merge(oldObject)
If I do the changes like that, no UPDATE query is done.
If I change the value in the normal way oldobject.setValue(newValue) the query is done and the record is updated.
Why no UPDATE query is done by the entity manager if I change value via reflection?
Just find some good information about such behaviour at this :
By default when using weaving/agent EclipseLink uses attribute change
tracking to detect changes. This will not detect changes made through
reflective field access (method access is ok though).
You can change the default using the #ChangeTracking annotation to
deferred which will detect change made through reflection. i.e.
#ChangeTracking(ChangeTrackingType.DEFERRED)
You could also disable weaving, or weaving of change tracking in the
persistence.xml using, "eclipselink.weaving.changetracking"="false"
So there are couple of solutions that can try :
Use reflective method access
Change the ChangeTracking to deferred or even disable it
Disable weaving
Thank to #Ken Chan answer, I correct the method using getters and setters.
for(Field column : columns){
Method mSet = myclass.getMethod("set"+ StringUtils.capitalize(column.getName()), column.getType());
Method mGet = myclass.getMethod("get"+ StringUtils.capitalize(column.getName()));
Object newValue = mGet.invoke(articolo);
if( newValue != null && !newValue.equals(mGet.invoke(old))){
mSet.invoke(old, newValue);
}
}
I must pay attention to methods' name. If entity have the property description, there must be also getDescription() and setDescription().
Now it works

Lookup Edit binding

I have a model written using Entity Framework Code First called Project.
In my UI, there is a page called ProjectEdit as shown in the image below. As seen in the image below, Customer and BOMs are Lookup Edit.
I'm trying to load Customer and BOMs to Lookup Edit but it's not working. Below is my code.
//New
if (entity == null)
{
Entity = new Project();
}
//Edit
else
{
ProjectCodeTextEdit.DataBindings.Add("EditValue", entity, "ProjectCode");
DescriptionTextEdit.DataBindings.Add("EditValue", entity, "Description");
CustomerLookUpEdit.DataBindings.Add("EditValue", entity, "CustomerId");
BOMsLookUpEdit.DataBindings.Add("EditValue", entity, "BOMs");
}
Below is my LookUpEdit Properties.
Generally LookUpEdit object's data binding is not implemented the same way as a TextEdit object's. While in TextEdits's case you just need to assign the variable value to EditValue property (I suppose your TextEdits binding work fine, isn't it?), with LookUp Edit you should assign variables to ValueMember and a DisplayMember properties of the object. That is why we usually display data rows with LookUpEdit objects, where ValueMember is the identification field of the row and DisplayMember is the field of the row whose value you wish to be displayed.
In your case you should be more clear about what you wish to display in your lookupedits. Each Project instance has one Customer property and many BOMs, right? So CustomerLookUpEdit will show one record and BOMsLookUpEdit a list of values according to the Project object that was chosen for edit, correct? I suppose that both your Customer and BOM classes have some kind of ID property and description property of their own. In this case you should bind these values to the LookUpEdits. eg. in your initialization function code add these lines
CustomerLookUpEdit.Properties.DataSource = entity.Customer;
CustomerLookUpEdit.Properties.ValueMember = "someCustomerIDpropertyName" ;
CustomerLookUpEdit.Properties.DisplayMember = "someCustomerDescriptionpropertyName";
BOMsLookUpEdit.Properties.DataSource = entity.BOMs;
BOMsLookUpEdit.Properties.ValueMember = "someBOMIDpropertyName" ;
BOMsLookUpEdit.Properties.DisplayMember = "someBOMDescriptionpropertyName" ;
You can read more in this topic https://documentation.devexpress.com/#WindowsForms/clsDevExpressXtraEditorsLookUpEdittopic
When we are adding entities to a List, we have to take care of our DataSource if is a DBContext or a DBSet, each one has implications in the compiler, that was your case, in this case you had to especify your DataSource like a DBSet and get the Entities
Add<TEntity>(TEntity entity)
The type parameter omitted is posible because the compiler will infer it.

Check for list of String in DataAnnotation

I need to check whether the Property contains one of the or all following strings
"C-I", "C-II", "C-III", "C-IV", "C-V"
if not it Errormessage must be
"Invalid Property. Must be blank or C-I, C-II, C-III, C-IV, or C-V.",
i don know which "DataAnnotation Attribute" to use and How? if possible please provide sample.
You could use the Regular Expression data annotation. However, I would recommend implementing IValidatableObject on your data class. You can then write your custom logic within the Validate method. This way, if/when those valid options change, you would just be modifying a collection, rather then trying to figure out a new valid regex statement.
It can be done using anyone of the follwing Attributes
**
1.EnumDataTypeAttribute
2.CustomValidationAttribute
3. Creating New Custom Attribute.
**

FindBy property in TYPO3 Extbase MVC is not working

I'm unable to run the FindBy magic function property in Extbase MVC
$title=array(0 =>'Books Day');
$each_event=$this->eventRepository->findByTitle($title);
$each_event is returning an object of type TYPO3\CMS\Extbase\Persistence\Generic\QueryResult .
How do I make this work?
I also tried passing in a string to findByTitle and findByOne. Both don;t work! :(
I'm using TYPO3 6.1 and extension builder.
The last part of those magic functions always needs to be a field in the database. So "title" must be in your model. You might have a field "one" for your object, but I guess you meant findOneByTitle?
The object type QueryResult is correct. You can turn it into an array for debugging purpose for example:
$foo = $query->execute()->toArray();
By the way: check wether your eventRepository is null or not and you could try this to see if it works at all:
$result = $this->myRepository->findAll();
Try
$each_event=$this->eventRepository->findByTitle($title)->toArray();
Reference to the QueryResult.
As said in the documentation, it returns a QueryResultInterface|array.
As a consequence you have to loop over the result like this:
foreach($each_event as $single_event) {
$single_event->getProperty();
}
If you are sure that it returns only one single value you could also access it by the index 0:
$each_event[0]->getProperty();

C# to Javascript in MVC

In my MVC application i created the view Index.cshtml and there i declared the C# variable..
Eg: string selectedvalue="";
now how to use it in the javascript function code which i wrote with in this Index.cshtml, so that i can append some text to this string in javascript function?
string selectedvalue="";
// Coding Part
function onchangeFT(e)
{
'#selectedvalue'= e.value; //e.value come form combobox and it is should be assigned to "selectedvalue"
alert('#selectedvalue');
}
It is giving me error...
any idea to solve this issue?
I think you are confused about what you are doing in this case.
You have declared the C# variable #selectedValue using MVC razor syntax. You can then use this variable to inject it's value into the content that is output to the client. However, you cannot assign back to this variable from client side javscript code as it does not exist. Your error will be in relation to the fact that you are actually attempting to assign an object value back to a literal string declaration which is invalid.
In your example
'#selectedValue'=e.value;
would translate to
'' = e.value;
This is because MVC razor will automatically translate the c# param into it's value and output it literally. In order to assign a variable to the e.value you should create a javascript value as part of your script block which your script will then identify as usable target variable.
For instance:
var selectedValue = '';
selectedValue = e.value;