Write enum to DB as lowercase value - spring-data-mongodb

I am getting a #RequestBody where one of the fields is an enum in my model.
ie
public enum X{
VALUE("Value")
}
I am able to pass in "Value" with the request which gets mapped to the correct enum value just fine. But when I save the body to the database it saves it as "VALUE". Is there some annotation or relatively easy way to save it as "Value" in the DB?
my fallback option is to just rename the enum to Value.

Related

How to map the country names in one enum to the time zone in another seperate enum

I am having an enum named Country like this,
Enum_1:
typedef enum = {Pacific/Midway, America/Adak, Etc/GMT+10, Pacific/Marquesas, Pacific/Gambier}Country;
and an enum for storing their corresponding time zones
Enum_2:
typedef enum = {GMT-11:00, GMT-10:00, GMT-10:00, GMT-09:30, GMT-09:00}zone;
I want to give the country as input from the user. When the input is given the code has to check the Country in Enum_1 and if the country is present, then it maps to the corresponding time zone in Enum_2
I know that enum returns only integer. I don't know whether we can map one enum to other
Example input:
Pacific/Midway
Output should be:
GMT-11:00
I am really confused about this. I want to know even if it is possible to implement this method in C?

Querying database in Grails

The values in database are saved as such
::{"rating1":"2","rating2":"4","rating3":"5","rating4":"0","rating5":"0"},
Now I need to acces the individual values like 2,4,5 etc.
I made a variable "rating" of the Domain Class type and tried accesing as object using (.) operator but it wont work and gives error:
:exception::groovy.lang.MissingPropertyException: No such property: rating1 for class: java.lang.String
, I tried casting to array and list (as Array, as ArrayList, as List) etc but that wont work either.
Casting to List gives exception:exception::org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object '{"rating1":"2","rating2":"4","rating3":"5","rating4":"0","rating5":"0"}' with class 'java.lang.String' to class 'java.util.List' .
Accessing like "rating[3]" gives answer "a". Should I use "rating[11]" to get value 2 or is there any way around.
What could be the possible solution. Please help.
You're storing a string, so you only have string operations available. You need to parse that string to get at the attributes individually (JsonSlurper perhaps?).
rating[character-position] is a bad idea IMO.
Maybe transients (check the GORM docs) would be useful.
class YourDomain {
String yourField
String getRating1() { new JsonSlurper().parseText(yourField).rating1 }
}
Maybe. Totally untested, just an idea.

How to store values even after the application restarts?

When creating applications with ApplescriptObjC you can create a property like this:
property myValue : "value"
However, if I change the value of this variable, it will get set back to "value" whenever the application restarts. How can I store values so they do not get reset after the application restarts?
You'll need to store your value somewhere before your application quits. Using NSUserDefaults is the best approach:
property NSUserDefaults : ((current application)'s NSUserDefaults's standardUserDefaults)
To save your value, use the following code, where myKey can be any string, and val is the value that you want to save:
NSUserDefaults's setObject:val forKey:myKey
To retrieve your value later on, you can use:
NSUserDefaults's objectForKey:myKey
By the way, NSUserDefaults has several useful functions for getting values, depending on the type of data stored. For example, if you were storing a boolean, you could use "boolForKey:".
However, you should set a default value for your key, in case it has never been saved before. This must be done before trying to retrieve the value for your key, where defVal is the default value:
NSUserDefaults's registerDefaults:((current application)'s NSDictionary's dictionaryWithObject:defVal forKey:myKey)
If you'd like to know more about NSUserDefaults, you can read about it here: https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSUserDefaults_Class/

MarkupExtension that uses a DataBinding value

I'm trying to create a WPF MarkupExtension class that provides translated text from my text translation class. The translation stuff works great, but requires a static method call with a text key to return the translated text. Like this:
ImportLabel.Text = Translator.Translate("import files");
// will be "Dateien importieren" in de or "Import files" in en
Its speciality is that it accepts a counting value to provide better wordings.
ImportLabel.Text = Translator.Translate("import n files", FileCount);
// will be "Import 7 files" or "Import 1 file"
Another example: If something takes yet 4 minutes, it's a different word than if it only takes one minute. If a text key "minutes" is defined as "Minuten" for any number and as "Minute" for a count of 1, the following method call will return the right word to use:
Translator.Translate("minutes", numberOfMinutes)
// will be "minute" if it's 1, and "minutes" for anything else
Now in a WPF application, there's a lot of XAML code and that contains lots of literal texts. To be able to translate them without getting nuts, I need a markup extension which I can pass my text key and that will return the translated text at runtime. This part is fairly easy. Create a class inheriting from MarkupExtension, add a constructor that accepts the text key as argument, store it in a private field, and let its ProvideValue method return a translation text for the stored key.
My real problem is this: How can I make my markup extension accept a counting value in such a way that it's data-bound and the translation text will update accordingly when the count value changes?
It should be used like this:
<TextBlock Text="{t:Translate 'import files', {Binding FileCount}}"/>
Whenever the binding value of FileCount changes, the TextBlock must receive a new text value to reflect the change and still provide a good wording.
I've found a similar-looking solution over there: http://blogs.microsoft.co.il/blogs/tomershamam/archive/2007/10/30/wpf-localization-on-the-fly-language-selection.aspx But as hard as I try to follow it, I can't understand what it does or why it even works. Everything seems to happen inside of WPF, the provided code only pushes it in the right direction but it's unclear how. I can't get my adaption of it to do anything useful.
I'm not sure whether it could be useful to let the translation language change at runtime. I think I'd need another level of bindings for that. To keep complexity low, I would not seek to do that until the basic version works.
At the moment there's no code I could show you. It's simply in a terrible state and the only thing it does is throwing exceptions, or not translating anything. Any simple examples are very welcome (if such thing exists in this case).
Nevermind, I finally found out how the referenced code works and could come up with a solution. Here's just a short explanation for the record.
<TextBlock Text="{t:Translate 'import files', {Binding FileCount}}"/>
This requires a class TranslateExtension, inherited from MarkupExtension, with a constructor accepting two parameters, one String and one Binding. Store both values in the instance. The classes' ProvideValue method then uses the binding it gets, adds a custom converter instance to it and returns the result from binding.ProvideValue, which is a BindingExpression instance IIRC.
public class TranslateExtension : MarkupExtension
{
public TranslateExtension(string key, Binding countBinding)
{
// Save arguments to properties
}
public override object ProvideValue(IServiceProvider serviceProvider)
{
countBinding.Converter = new TranslateConverter(key);
return countBinding.ProvideValue(serviceProvider);
}
}
The converter, say of class TranslateConverter, has a constructor that accepts one parameter, a String. This is my key argument from the TranslateExtension above. It remembers it for later.
Whenever the Count value changes (it comes through the binding), WPF will request its value anew. It seems to walk from the source of the binding, through the converter, to the surface where it's displayed. By using a converter, we don't have to worry about the binding at all, because the converter gets the binding's current value as a method argument and is expected to return something else. Counting value (int) in, translated text (string) out. This is my code.
So it's the converter's task to adapt the number to a formulated text. It uses the stored text key for that. So what happens is basically a kinda backwards data flow. Instead of the text key being the main information and the count value being added to it, we need to treat the count value as the primary information and just use the text key as a side parameter to make it whole. This isn't exactly straightforward, but the binding needs to be the primary trigger. Since the key won't change, it can be stored for good in the instance of the converter. And every occurence of a translated text gets its own copy of the converter, each with an individual key programmed in.
This is what the converter could look like:
class TranslateConverter : IValueConverter
{
private string key;
public TranslateConverter(string key)
{
this.key = key;
}
public object Convert(object value, ...)
{
return Translator.Translate(key, (int) value);
}
}
That's the magic. Add the error handling and more features to get the solution.

Pass array as parameter to JAX-RS resource

I have many parameters to pass to the server using JAX-RS.
Is there a way to pass or AarryList with the URL?
You have a few options here.
Option 1: A query parameter with multiple values
You can supply multiple simple values for a single query parameter. For example, your query string might look like:
PUT /path/to/my/resource?param1=value1&param1=value2&param1=value3
Here the request parameter param1 has three values, and the container will give you access to all three values as an array (See Query string structure).
Option 2: Supply complex data in the PUT body
If you need to submit complex data in a PUT request, this is typically done by supplying that content in the request body. Of course, this payload can be xml (and bound via JAXB).
Remember the point of the URI is to identify a resource (RFC 3986, 3.4), and if this array of values is data that is needed to identify a resource then the URI is a good place for this. If on the other hand this array of data forms part of the new representation that is being submitted in this PUT request, then it belongs in the request body.
Having said that, unless you really do just need an array of simple values, I'd recommend choosing the Option 2. I can't think of a good reason to use URL-encoded XML in the URL, but I'd be interested to hear more about exactly what this data is.
We can get the Query parameters and corresponding values as a Map,
#GET
#Produces(MediaType.APPLICATION_JSON)
public void test(#Context UriInfo ui) {
MultivaluedMap<String, String> map = ui.getQueryParameters();
String name = map.getFirst("name");
String age = map.getFirst("age");
System.out.println(name);
System.out.println(age);
}

Resources