I'm not much familiar to Java Currency type, and how it being used in Grails. Though, I'm yet to use it, I saw a tag <g:currencySelect> to use in the views. So, how do I represent it in the domain class.
class Money {
BigDecimal value
Currency currency
....
}
or is there a better sol, which compares diff money objects, format according to the locale ( ',' in EU for separator etc)
thanks in advance.
Babu.
You should use BigDecimal. Groovy and Grails has excellent native support for it as a datatype on GORM domain classes as well. For reasoning behind using it, see here
What is the best data type to use for money in java app?
You might want to take a look a the Currencies plugin. It provides a Money class for holding monetary amounts of differing currencies. They can be embedded into domain classes like so:
class CustomerTransaction {
Date date = new Date()
Money amount
static embedded = ['money']
}
Use the JScience library. It's just a shame it's not in a Maven repo yet, and doesn't have a Groovy wrapper to make it Groovier (TM).
If you want to have also live exchage rates updates then following plugin could help https://grails.org/ExchangeRates+Plugin all rates are pulled from Yahoo Finance.
since the original answer seems to be outdated, you might want to take a look at the money-plugin which is fresh and currently maintained.
Related
I'd like to allow users to define simple business logic such as:
if (x and y) then ...
if (z or w) then ...
Let me put it concretely:
I'm developing a HR module that answers if applicants fulfill some requirements, to be defined by users.
Such requirements can be defined around logical operators:
(Must be 18 years old) or (under 18 years and must have parents permission)
Is putting this kind of logic inside the database ok? I think it is, but I'm afraid of spending time on this and find that its a poor approach.
It's ok. It is flexible approach, although time consuming in development.
Furthermore, you don't have to create your own DSL, it's already done, e.g. json-logic-ruby allows to keep complex rules in json.
As so often, the answer is "it depends" ;) Since it seems that the logic in this case is user-defined data, putting it into the database is absolutely reasonable.
But if you are looking to model the structure/AST of this input into separate business objects with their and and or control flow reflected in the database records, I would have to say that it's very likely overkill and will - apart from the initial implementation overhead - make future refactoring very hard.
A simple text field that will be evaluated at runtime is the easiest way to go as its contents can be very easily extracted and reasoned about.
Not knowing your definitive requirements, I'd suggest you take a look at Drools, a rules engine for Java, which has in its ecosystem also a rule storage backend and guided editor. Incidentally the example in your question looks a lot like it might benefit from a rules engine but unfortunately I don't have any practical experience with any of the related Ruby libraries.
Otherwise this article on the thougtbot blog - Writing a Domain Specific Language in Ruby - might be helpful in this context, too.
I definitely think it's okay. Because the user is defining the business logic or rules, I'd recommended splitting the business logic form field into parts(rule: if/unless, operand1: user.age, operand2: permissions.parental operator1: and, operator2: greater_than...) and then storing each business logic object as a row in a serialized JSON column. This should make them easier to validate and less error prone as compared to a single text field where the user to enter in whatever they like.
I would suggest creating a simple table to store the logic if it is predictable.
For example:
Table: business_logics
Attributes:
opt_1: decimal
opt_2: decimal
logic_opt: integer (enum: and|or)
then_statement: string
So this is extendable when you have more logic_opt in someday, btw you can get the advantage in validation & refactoring later on! Allow users to input the free texts is so risky in your case!
I am using ASP.NET Web Api 2 with Json.NET 6.0.1.
According to ISO 8601, dates should be interchanged in a certain way. I am using the IsoDateTimeConverter() in order to achieve this:
config.Formatters.JsonFormatter.SerializerSettings.Converters.Add(new IsoDateTimeConverter());
But how should "time of day" be returned in a JSON response model?
I cannot find anything for this in the ISO specification.
Should time perhaps be returned as a:
TimeSpan? (with expectation of the user to not use this as a duration representation)
DateTime? (with expectation of the user to drop off the date part)
A custom Time class
There is no standard structure in JSON for containing dates or times (see JSON.org). The de-facto stardard for dates-time values is using a string in ISO 8601 format, as you mentioned. But since there is no official standard it really comes down to what works best for you and consumers of your API.
Using a DateTime object is a reasonable choice because the support already exists in Json.Net and other serializers for converting these to and from ISO 8601 strings. So this would be the easiest to implement. However, users of your API would have to know to disregard the date portion, as you said. You could set the date to 0001-01-01 to emphasize its irrelevance. This isn't so different from the more common situation where you need only a date in your API and the time doesn't matter. Most people just set the time to midnight in this case and let it go. But, I would agree that this approach does seem to have a little bit of a "code smell" to it, given that part of the value is just noise.
Perhaps a cleaner idea is to format your DateTime value as ISO 8601, but then chop off the date portion before returning it. So users of the API would get a string that looks like 14:35:28.906Z. You could write a simple JsonConverter to handle this for you during serialization. This would sort of give you the best of both worlds -- a cleaner API, but you still can work with the familiar DateTime struct internally.
A custom Time class could also work here, but might be overkill, depending. If you do need to go there, you might want to look into a third-party library such as Noda Time, which has classes already built for these kinds of things, and also has pre-built converters for Json.Net.
I would definitely not choose TimeSpan for this purpose. Wrong tool for the job.
I am trying to build an iOS application. In one of the screens the user can type something in a search bar and I have to take same action for different spellings of the same word.
For eg: User can type "elephant" or "alephant" or "elefant". I have to take same action for all these three words.
Is there any library that identifies these words as similar ones ? I cannot use spellchecker as I need this in languages other than english also ..
I did some research and I found that there are some phonetic algorithms like Text::soundex for achieving this on server side. Wondering if any libraries there for iOS ?
Thanks in advance !!
A better alternative to Soundex would be Double Metaphone or, even better, Metaphone 3. You don't say what language you are using, but both of these algorithms are available in C++, C#, and Java
There's no soundex available in for example NSString, but if that's what you want, it's fairly easy to implement. Here's a—albeit horribly formatted—soundex NSString category from CocoaDev.
You could also use the Levenstein Distance algorithm to catch simple spelling errors. Also easy to implement (read the Wikipedia article for the details), but here's a NSString category for that.
Before you use these algorithms, normalize the input. There's the amazing CFStringTransform class in Core Foundation (see this great article about it on NSHipster—especially the last part about normalization) that automatically can transform different language inputs into normalized forms.
new to F#
i need to store a bunch of lists of objects according to a float number where the collection of lists are sorted according to the float number. I know in C# i would use
SortedDictionary<float, List<obj>>
as the implementation is a red black tree, allowing for log(n) insert and search. But whats the best way to attack the situation in F#. I attempted to use SortedDitionary but i can't refer to SortedDictionary[int] to find the value so it renders it as useless essentially (i could be doing it wrong).
thanks for the help
The syntax is
sorteddictionary.[int]
then it works as you would expect
The first thig to do is read Okasaki's book Purely Functional Data Structures
It has ML implementations that may help you
You can use sorteddictionary.[int] as John Palmer already said but it may be worth pointing out that the F# standard library includes a purely functional sorted dictionary collection called Map.
I'm developing something for international use. Wondering if anyone can shed any light on whether the CultureInfo class has support for finding currency exponents for particular countries, or whether I need to feed this data in at the database level.
I can't see any property that represents this at the minute, so if anyone knows definitively if it exists, before I look for it / buy it from ISO.
Currency Exponent is the minor units of the currency.
http://en.wikipedia.org/wiki/ISO_4217 - e.g. UK is "2"
Take a look at this blog post on getting CultureInfo for a region. Basically, Window and .NET know about the user's region but not their currency. A region implies a currency, but a country can have more than currency. For example, a person in Cambodia would more than likely want to enter and use USD than Riel. If possible, when capturing any currency amount in a multi-currency system you should capture the currency ISO code.
If you just want to make a quick guess, you can create a CultureInfo object and use it's NumberDecimalDigits property. The also creates a problem when countries switch currencies. For example, if Belarus joins the EU, then it's currency would change from BYR to EUR. It's currency symbol and exponent will be out of date.
I looked at this question and provided a solution which may or may not meet your needs here: http://www.codeproject.com/KB/recipes/MoneyTypeForCLR.aspx#CurrencyType
The short of it: I implemented the ISO spec as a custom type using the spec itself to generate the values. Obviously this would need to be regularly updated in production...