Grails - a conditional mapping - grails

Currently I'm developing a webapp which uses Oracle as a database. I've already been informed that my webapp has to work also on Sybase ASE. My Oracle database uses sequences to generate IDs and I mapped my domain classes to use those sequences. As far I know Sybase ASE doesn't have sequences but identities. And here is my problem. What to do with code/configuration to work with Oracle and Sybase. Some "conditional mapping" of ID for each domain would do.
I just don't want to comment/uncomment lines in mappings every time when I'm building webapp for other database than already mapped. Have anybody any idea what to do?
There is another danger: another differences between Oracle and Sybase which have influence on mapping (I don't generate database structure using dbCreate = "create") . At this point I don't see anything. Any experiences?
EDIT:
It turned out that it's not possible make one mapping for both databases. My Oracle structure uses quotes in column and table names. And quotes are not allowed in Sybase ASE. My (not nice and pretty) solution is changing (comment/uncomment) mapping when building webapp on some particular platform.
Btw I'm huge fan of Grails and this mapping thing seems to be a drawback. If I was using "pure" Hibernate I'd have two mappings in xml files and I'd change them depending on an underlying database. But Grails doesn't give me possibility to have two mappings.

I don't know if grails enable to you to configure coditional mapping for id generation but I think that maybe you don't need use neither sequences or identities, and use other hibernate id generator strategy: http://grails.org/doc/1.3.7/ref/Database%20Mapping/id.html.
EDIT:
Reviewing the hibernate documentation I found that is possible use sequences or identites according to capacities of database, from http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/mapping.html#mapping-declaration-id:
All generators implement the interface
org.hibernate.id.IdentifierGenerator. This is a very simple interface.
Some applications can choose to provide their own specialized
implementations, however, Hibernate provides a range of built-in
implementations. The shortcut names for the built-in generators are as
follows:
...
native:
selects identity, sequence or hilo depending upon the capabilities of
the underlying database.
So you maybe will need a configuration like this:
static mapping = {
id generator:'native'
}

Related

Dynamic Spring Data Classes for Neo4J based on an Ontology

I have to create a web service that needs to interact with a Neo4J database using the Spring framework with Spring-Data-Neo4J. This requires a static data domain model, e.g. defined labels, relations, properties.
The Problem is, that my data is based on an Ontology (via the neosemantics plugin) which could be modified in the future. It would be great if the application could automatically adopt to it. This way, the data model could be extended by editing the ontology only and no additional programming knowledge would be necessary.
Does this mean I have to generate the Spring data classes dynamically (based on the Ontology) or is there a better way to achieve this with Spring-Data-Neo4J (or should I use a different framework)?
Sure, you could come up with a way to generate a set of classes from an ontology. But that is probably going to present more problems than it solves.
An automatically-generated set of classes may not correspond to an appropriate data model for your use cases. The determination of the appropriate data model still requires a human.
Also, the new classes may be incompatible with the existing client code. And you may have to migrate the existing DB over to the new data model. Fixing all that requires humans as well.
I ended up using the Java neo4j driver instead of Spring-Data-Neo4jand a generic node class implementation only having the fields id, list of labels and a map of properties. The set labels and properties can be checked against the ontology prior to creating the nodes in the database. This way I can enforce a specific set of node labels and properties by only modifying the ontology and without having to generate the specific Spring-Data-Neo4j data classes.

GORM for Rest (Grails)?

I am doing some research on Grails and writing about what the future holds for it..
Something interesting jumped out in the RoadMap (http://grails.org/Roadmap)
GORM for REST
Anyone with more experience with Grails than me know what this would entail?
I am guessing some sort of CRUD operations through Web Services instead of using Hibernate to connect to an SQL database?
there is a JSON RESTful API for GORM which gives some insight on what GORM for REST is like:
GET on /context/api/domain-class-name returns a list of domain objects (possible arguments are the same as for the DomainClass.list() method argument map)
POST on /context/api/domain-class-name creates a new instance
GET on /context/api/domain-class-name/id retrieves the given instance
PUT on /context/api/domain-class-name/id updates the given instance by ID
DELETE on /context/api/domain-class-name/id deletes the given instance
As far as to RESTy GORM that is scheduled for Grails 2.0, here is the GORM Virtual REST domain objects discussion on Grails mailing list:
I am currently evaluating the use of grails to connect to other backend systems. Would it be possible to let the domain layer talk to CRUD REST services instead of a Database? It would be a bit like a XML backend.... We have got a very big backend where it is difficult to implement business logic, but we can manage to provide restful services. My idea is to have grails as a business / web application layer on top to deploy various systems to cross platform
This feature is scheduled for development for Grails 2.0
-- Graeme Rocher
I think the intent is to apply the scaffolding pattern to a RESTful API out of the box. There has been a JIRA entry around for several years for this.
Resulting JIRA: http://jira.grails.org/browse/GRAILS-2823
I also wouldn't be surprised if they took the dynamic finder idea and applied it to URL patterns.
GET /book/findByTitle/Dune
or
GET /book/findByTitle?title=Dune&format=json
or something like that.
I don't know exactly what is on the roadmap, but I imagine that the final product will have scaffold functionality (list,view,create,update,delete) through a RESTful interface plus some URL patterns that correspond to what you can currently do with the dynamic finders that GORM provides.
Take a look at the JAX-RS plugin. I suspect that will be the kind of thing they use.
Not that I want to rain on the parade but why would I use REST for accessing database rather than directly through hibernate. It's bound to slow down the DB access.

Datawarehousing with ASP.NET MVC

On one server there are more than 20 databases with identical structure but different data. I need to collect some of the data (the same queries) from all databases and store in new database which is located on another server. I decided to use ASP.NET MVC 2 but it doesn't seem logical to use more than 20 "LINQ to SQL Classes" (.dbml) files because the structure is the same for all databases and it's repeating if I use so many of these files. Is there a simple way to use one .dbml file (for remote databases) but change only connection string?
I agree that you really wouldn't want to use MVC as that is a web framework and has nothing to do with moving data around.
You can also look into using an ETL tool to accomplish this task. I have used RhinoETL in the past successfully to accomplish something similar.
There are also multiple posts on this site discussing ETL tools. For example, check the following link - https://stackoverflow.com/questions/51198/what-etl-tool-do-you-use
According to this, you can pass in a connection string with the dataContext constructor. So theoretically, you should be able to have one dbml file, but you can instantiate multiple instances of your data context, each with a different database connection string specified. Each context should then point to their respective database and allow you to work with multiple databases.
Why do you want to use ASP.NET MVC at all? ASP.NET is for web UI, not data warehousing (except when you need to display cubes). Looks like you use SQL Server. If that is true you can utilize Integration Services (ex DTS) to do the job.

ASP.Net MVC: Where to store Application and Admin Panel settings? Web.config?

Having built myself a decent website using ASP.NET MVC, I'm about to build an "Admin Panel" for users in the "Admin" Role to control the website with. Specifically, the website that I've built is an internal site for an enterprise and it consists of a bunch of mini web-apps, each of which need to be configured and/or tweaked from time to time.
Is Web.Config the appropriate file to store these application settings?
Should it be in a separate XML file?
A DB Table (or set of tables)?
Currently, I have no DB available to me, but perhaps at a later date I will. Currently, I'm inclined to create an XML file and store values there. Is this a reasonable approach? Or am I missing something obvious on how to do this?
EDIT: Ok, here's my abstraction layer (Interface):
public interface IAppSettings
{
IQueryable<string> GetValues(string component, string setting);
void SetValues(string component, string setting, List<string> values, bool append);
}
I figure I can read/write to Web.Config, another XML, or a DB this way.
What do you think?
I would recommend you a db. Writing to files in a multi-user/multi-threaded application could be challenging and problems could arise if you don't synchronize the access to those files, not to mention transactional access if you ever needed such. There are some lightweight NoSQL databases which could be extremely useful and easy to setup.
Whatever you choose make sure to create abstractions over the data access layer so that you could easily switch later to another method like a SQL database if you will (after trying a NoSQL database you probably won't will :-)).
The question to ask here is, in general, is this a set of settings that will change with the codebase or be changed by users? If it is the former, XML is the way to go -- it can easily be version controlled with the code. If users are changing it then a database is a better option.

ASP.NET MVC models when using external web services

I'm getting started on a new MVC project where there are some peculiar rules and a little bit of strangeness, and it has me puzzled. Specifically, I have access to a database containing all of my data, but it has to be handled entirely through an external web service. Don't ask me why, I don't understand the reasons. That's just how it is.
So the CRUD will be handled via this API. I'm planning on creating a service layer that will wrap up all the calls, but I'm having trouble wrapping my head around the model... To create my model-based domain objects (customers, orders, so on..) should I:
Create them all manually
Create a dummy database and point an ORM at it
Point an ORM at the existing database but ignore the ORM's persistence in lieu of the API.
I feel like I've got all the information I need to build this out, but I'm getting caught up with the API. Any pointers or advice would be greatly appreciated.
Depending on the scale of what you're doing option 3 is dangerous as you're assuming the database model is the same as that exposed by the external service. Options 1 and 2 aren't IMHO much different from each other - in either case you'll have to decide what your objects, properties and behaviours are going to be - it just boils down to whether you're more comfortable doing it in classes or database tables.
The key thing is to make sure that the external service calls are hidden behind some sort of wrapper. Personally I'd then put a repository on top of that to handle querying the external service wrapper and return domain objects.
In general, ORMs are not known for their ability to generate clean domain model classes. ORMs are known for creating data layers, which you don't appear to need in this case.
You could probably use a code generation tool like T4 to code generate a first pass at your domain model classes, based on either the web service or the database, if that would save you time. Otherwise, you probably would just manually create the domain objects. Even if you code generate a first pass at your domain objects, it's unlikely there is a clean 1-1 mapping to your domain objects from either the database or web service, so you will likely need to spend significant time manually editing your code generated domain classes anyway.

Resources