Use enum to select string from wicket properties file - properties

I'd like to add a label to a wicket panel where the label's model is an enum value. Based on the value of that enum, I'd like to display a different message pulled from the page's properties file.
For example, here's an enum:
public enum ApprovalType { UNAPPROVED, APPROVED, BLOCKED };
I can easily add a label to the panel that has this enum as its model:
add(new Label("approval", new Model(ApprovalType.APPROVED)));
However, this will simply output UNAPPROVED, APPROVED, or BLOCKED. I'd like to output a localized message that is selected based on the value of this enum.
So if I have this properties file, what's the best way to get these messages to output?
UNAPPROVED=Your membership is currently pending approval
APPROVED=Your membership has been approved
BLOCKED=You have been blocked from membership
I can get it working like this:
add(new Label("approval", new ResourceModel(ApprovalType.APPROVED.name());
But the problem is that ResourceModel only accepts a string (resource key). I'd like to pull the value of the resource key automatically from a model (preferrably a CompoundPropertyModel). In other words, I don't want to do this:
Member member = (Member) getDefaultModelObject();
add(new Label("approval", new ResourceModel(member.getApproval().name())));
I'd rather do something like:
add(new EnumLabel("approval"); // assuming I have a CompoundPropertyModel
Is there a component that does this that comes with Wicket? Or do I need to write my own?
Lastly, I discovered this also works:
add(new Label("approval", new StringResourceModel(
"${}",
new PropertyModel<ApprovalType>(getDefaultModel(),"approval")
)));
But this seems overly verbose, and still doesn't utilize the simplicity of using CompoundPropertyModel.
Ideas?

Not sure if this 'll work, but maybe you could try writing your own EnumModel class that takes the value of an Enum and returns a ResourceModel value..
Please say so if the above isn't clear enough, I'll try to elaborate.

Related

questions on ObjectBox data model rename

Say I want to rename property "user" to "customer". I understand that I can add #Uid of "user" at the new property name. Like:
#Uid(123985252953064306)
String customer;
So I presume during the next build and run of my app, "user" in the database is renamed to "customer". My first questions are: can I then remove #Uid(123985252953064306) from my code for further builds? I guess the answer is yes if the app is only used by myself? So to maintain compatibility for endusers of the app, I still need to keep the #Uid annotation in the code. Is it correct?
My next question is: what if later I want to rename "customer" to "client"? Should I add an additional #Uid at the new property? Like:
#Uid(123985252953064306)
#Uid(124568645726267383)
String client;
The questions you are asking are covered by the docs in Data Model Updates and with some more background in Meta Model, IDs, and UIDs.
In short, once you put an #Uid on an property (or an entity) you can rename it anyway you want as often you want. ObjectBox takes the UID to identify the property and knows it been there before. Thus you need to keep it on the property.
So from
#Uid(123985252953064306)
String customer;
you can go to
#Uid(123985252953064306)
String someFancyName;
and back to
#Uid(123985252953064306)
String customer;
with keeping the same data for the property in the database.

Howto select a ASP.NET Core action from a JSON property in the body?

I have a REST interface endpoint like
POST /items/12345/actions
I utilize a generic actions sub collection to be apply to apply changes to 12345 which are not easily mapped to the content or direct other sub collections of it.
My question is the following: Since there could be multiple different action types I identify the action by a JSON property of the content of an uploaded document.
How do I select a action by a part of the JSON body of the request. Is there something possible like...
[Route("api/v1/items")
public class ItemsController : Controller
{
[HttpPost("{id}/actions")]
[CheckJsonBody("type", "ActionA")]
public ActionResult DoActionA(int id, ActionA a)
{
// do something
}
[HttpPost("{id}/actions")]
[CheckJsonBody("type", "ActionB")]
public ActionResult DoActionB(int id, ActionB b)
{
// do something
}
}
The request would look like ...
{
"type": "ActionA",
"abc": "xyz"
}
I have digged myself up into the code till Microsoft.AspNetCore.Mvc.ActionConstraints.ActionMethodSelectorAttribute (GitHub).
However starting from there, I am a bit lost to reach a high-performance solution. Do I need to decode the body or is that something which is already done at that time the constraint is evaluated?
ps: And yes, I know I could handle them in one action and do a switch on the "type" property.
An ASP.NET team member was so friendly to direct me to an answer: In the ActionMethodSelectorAttribute you can read the body into a memory stream, read till the property for the selection filter. Then you seek the memory stream to zero and replace it in the request (for later model binding). You can cache the criteria value in HttpContext.Items to speed it up if you use the same property for multiple actions.

How to pass multiple parameters in #PUT method?

I have a requirement to implement a method like this in Apache CXF JAX-RS(in a concurrent scenario)
#PUT
#Path("/customers/123")
public void updateConcurrentCustomer(Customer existingCustomer,Customer updatedCustomer,boolean forceUpdate){
......
}
In request body, i need to call this method something like this (no root element).
<Customer>
.....existing data....
</Customer>
<Customer>
......updated data....
</Customer>
<boolean>true</boolean>
How this data binding can be achieved?
I tried creating a composite wrapper Resource class like this
#XmlRootElement
public class CustomCustomer implements java.io.Serializable
{
private Customer existingCustomer;
private Customer updatedCustomer;
private boolean forceUpdate;
.....
.....
}
It works well. But i dont want to create this wrapper class.
My concurrency scenario:
customer123 object is in state A.
user1 changes customer123 to state B.
user2 changes customer123 to state C.
user3 changes customer123 to state D. (all at same time)
only high priority user sets forceUpdate flag and finally that update will be overwritten than others.
existingCustomer - will be used to detect conflict changes. It will be in state A
According to the definition
The PUT method requests that the enclosed entity be stored under the
supplied Request-URI. If the Request-URI refers to an already existing
resource, the enclosed entity SHOULD be considered as a modified
version of the one residing on the origin server.
So first of all your customer needs a unique identifier like /customers/{id}. Otherwise the server can't know where the resource should be stored.
Then you don't need to pass the existingCustomer. Either there is none (new resource) or the server already knows him (because you addressed him with a unique URL).
The forceUpdate is also not senseful here. A PUT should modify if the resource already exists so forceUpdate is true be definition.
Sometimes you can't use the clear semantics of a PUT. For instance if the client does not know the id and you don't want that the client chooses one (he can't guarantee uniqueness). Then you can use a POST and the server will return the Location where he stored the Resource.
Also if you want to update only in special cases maybe depending on some other parameters a POST is the appropriate Method.

LINQ and Static ObservableCollection

This will probably be a basic questions for the LINQ & architecture experts however I am failing to understand a problem i've encounted when trying to update a 'Static ObservableCollection.
Me.Grid1.ItemsSource = ContactList
Me.Grid2.ItemsSource = From s In ContactList Where s.ContactTypes.Any(Function(t) t.ContactTypeName = "Christmas List")
If I add a new Contact with the ContactType "Christmas List" to the ContactList ObservableCollection, Grid1 reflects the additional Contact however Grid2 does not reflect the change unless I rebind.
Anyway to Reflect the change in Grid2 to show the new Contact with the queried ContactType
Grid2 is actually binding to an IEnumerable(Of Contact) instead of an Observable Collection. That's why the change isn't reflected in Grid2. You need to cause your Linq query to reexecute using an event or INotifyPropertyChanged.
It could be happening due to the deferred execution nature of LINQ query. The values are fetched only when, you start enumerating over the result set. That is why, you have to rebind the data source, to see the change. Try adding ToList(), method at the end of the query. For e.g.
Me.Grid2.ItemsSource = From s In ContactList Where s.ContactTypes.Any(Function(t) t.ContactTypeName = "Christmas List").ToList();
You need my ObservableComputations library. Using this library you can code like this:
Me.Grid2.ItemsSource = ContactList.Filtering(c => c.ContactTypes.ContainsComputing("Christmas List").Value);
Filtering extention method returns instance of ObservableCollection and reflects all the changes in the ContactList collection and ContactTypes collection. Writing the code above, I assumed contactContactTypes id ObservableCollection. If this is not so, then you can code:
Me.Grid2.ItemsSource = ContactList.Filtering(c => c.ContactTypes.Contains("Christmas List"));
In this case do not forget to add the implementation of the INotifyPropertyChanged interface to Contact class, so that the result ObservableCollection reflects the change of contact.ContactTypes property.

validation based on attributes in metadata class

I am performing validation based on attributes in metadata.cs file. I am using Entity framework 4.0 and using wcf ria services. I want to know what is the equivalent of Page.IsValid in asp.net in silverlight? How do i ensure that the entity is in correct state before saving it? If i do not ensure this an exception fires which looks very ugly. I found a property named entityName.ValidationErrorCount so if my entity is named User i do objUser.ValidationErrorCount is less than equal to 0 i save it. Problem with this approach is if the user doesn't enter value in any of the textbox then subsequently all the values in the entity are null. So ValidationErrorCount property returns 0 because all are null values and thus my program tries to save the entity but naturally the entity is in incorrect state so exception fires. How do i get past this problem?
I hope i am clear. If not, please let me know. Thanks in advance :)
You can validate an entity using the Validator class (from the System.ComponentModel.DataAnnotations
namespace), like so (where entity is a reference to the entity to be validated):
List<ValidationResult> validationResults = new List<ValidationResult>();
ValidationContext validationContext = new ValidationContext(entity, null, null);
bool isValid = Validator.TryValidateObject(entity, validationContext, validationResults, true);
Alternatively, are you using the DataForm control? If so, there's an even easier way to check whether the current item is valid, by calling the ValidateItem() method on the DataForm. This will return a boolean indicating whether the current item is valid (you can also check the IsItemValid property of the DataForm). It will also update the bound controls to show their validation status. If you're not using the DataForm, then it will certainly make it easier if you can. Otherwise, simply add the validation results to the entity's ValidationErrors property:
foreach (ValidationResult result in validationResults)
entity.ValidationErrors.Add(result);
Hope this helps...
Chris