Copy from one datasource to another in grails 2 - grails

I'm trying to use a domain that's configured for two datasources to copy data from one datasource to another. The documentation implies that this is straight-forward but I can only get it to save to the default datasource.
class LocalTransaction {
static mapping = {
datasources(['DEFAULT','migration'])
}
}
First I tried finding the transaction from the default datasource via LocalTransaction.findAllBy..(), then attempt to save changes via tr.migration.save(flush:true,failOnError:true) but the changes are saved to DEFAULT.
I think tried to create a new instance via LocalTransaction.migration.get(lt.id), copy the data over and then save, but that isn't saving to the migration datasource either.
Thanks,
Bill

Grails 2.0 has added support for multiple datasources to its core (this was formerly provided by a plugin). Please read the following section in the Grails documentation http://grails.org/doc/2.0.0.RC1/guide/conf.html#multipleDatasources
To save data in a specific datasource, you have to use its name before calling the save() closure, for example:
localTransactionObj.migration.save()
Hope that helps!

Related

Grails 2.5 set datasource on service class not working

I have multiple data sources in my application and I want to be able to switch the datasource on a service class from the default one to a different one (so that it matches my Grails domain). Per the doc it states to use:
static datasource = 'dsName'
property on the service class, which I have done. However, the mainTransactionManager is still using my default data source.
I can see in the ChainedTransactionManager that it calls:
MultiTransactionStatus mts = new MultiTransactionStatus(transactionManagers.get(0));
The 0th index inside of transactionManagers contains my default data source... so how do I ensure that it doesn't use the transaction manager that contains the default data source?
Where/when does Grails read the
static datasource = '...'
property?
Basically I just want to lineup my datasource across my services and my domains.
Where/when does Grails read the static datasource = '...'
That property is read at https://github.com/grails/grails-core/blob/bd7cc10e17d34f20cedce979724f0e3bacd4cdb4/grails-plugin-services/src/main/groovy/org/codehaus/groovy/grails/plugins/services/ServicesGrailsPlugin.groovy#L74
That is invoking the getDatasource method at https://github.com/grails/grails-core/blob/bd7cc10e17d34f20cedce979724f0e3bacd4cdb4/grails-core/src/main/groovy/org/codehaus/groovy/grails/commons/DefaultGrailsServiceClass.java#L40.

Fix uneccessary copy of NSManagedObject

I'm sorry the title may mislead you, since I'm not so good at English. Let me describe my problem as below (You may skip to the TL;DR version at the bottom of this question).
In Coredata, I design a Product entity. In app, I download products from a server. It return JSON string, I defragment it then save to CoreData.
After sometimes has passed, I search a product from that server again, having some interaction with server. Now, I call the online product XProduct. This product may not exist in CoreData, and I also don't want to save it to CoreData since it may not belong to this system (it come from other warehouse, not my current warehouse).
Assume this XProduct has the same properties as Product, but not belong to CoreData, the developer from before has designed another Object, the XProduct, and copy everything (the code) from Product. Wow. The another difference between these two is, XProduct has some method to interact with server, like: - (void)updateStock:(NSInteger)qty;
Now, I want to upgrade the Product properties, I'll have to update the XProduct also. And I have to use these two separately, like:
id product = anArrayContainsProducts[indexPath.row];
if ([product isKindOfClass:[XProduct class]] {
// Some stuff with the xproduct
}
else {
// Probably the same display to the cell.
}
TL;DR
Basically, I want to create a scenario like this:
Get data from server.
Check existed in CoreData.
2 == true => add to array (also may update some data from server).
2 == false => create object (contains same structure as NSManagedObject from JSON dictionary => add to array.
The object created in step 4 will never exist in CoreData.
Questions
How can I create an NSManagedObject without having it add to NSMangedObjectContext and make sure the app would run fine?
If 1 is not encouragement, please suggest me a better approach to this. I really don't like to duplicate so many codes like that.
Update
I was thinking about inheritance (XProduct : Product) but it still make XProduct the subclass of NSManagedObject, so I don't think that is a good approach.
There are a couple of possibilities that might work.
One is just to create the managed objects but not insert them into a context. When you create a managed object, the context argument is allowed to be nil. For example, calling insertNewObjectForEntityForName(_:inManagedObjectContext:) with no context. That gives you an instance of the managed object that's not going to be saved. They have the same lifetime as any other object.
Another is to use a second Core Data stack for these objects, with an in-memory persistent store. If you use NSInMemoryStoreType when adding the persistent store (instead of NSSQLiteStoreType), you get a complete, working Core Data stack. Except that when you save changes, they only get saved in memory. It's not really persistent, since it disappears when the app exits, but aside from that it's exactly the same as any other Core Data stack.
I'd probably use the second approach, especially if these objects have any relationships, but either should work.

Why DAC class is not saved in Acumatica?

Let's say I have following code:
DacClass cl = new DacClass();
//init fields of DacClass()
this.Persist();
but when I run this code in any graph, I'm getting different errors. Why?
You can't create DAC item in db in current graph. As mentioned in T200 manual you should create instance of graph and in created instance to call method persist. Another option is to use method PXDataBase.Insert. The first option is preferable for case if you need insertion with graph logic. The second option is preferable for cases if you need perfomance.

How to drop the neo4j embedded database with java?

The class GraphDatabaseService seems not provide any method to drop/clear the database. It there any other means to drop/clear the current embedded database with Java?
Just perform a GraphDatabaseService.shutdown() and after it has returned, remove the database files (using code like this).
You could also use getAllNodes() to iterate over all nodes, delete their relationships and the nodes themselves. Maybe avoid deleting the reference node.
If your use case is testing, then you could use the ImpermanentGraphDatabase, which will delete the database after shutdown.
To use ImpermanentGraphDatabase add the neo4j-kernel tests jar/dependency to your project. Look for the file with a name ending with "tests.jar" on maven central.
I think the easiest way is to delete a directory with neo4j database. I do it in my junit tests after running all tests. Here is a function I use where file is the neo4j directory:
public static void deleteFileOrDirectory( final File file ) {
if ( file.exists() ) {
if ( file.isDirectory() ) {
for ( File child : file.listFiles() ) {
deleteFileOrDirectory( child );
}
}
file.delete();
}
}
I think I found it on neo4j wiki. I have found in this discussion another solution. You can use Blueprint API, which provide method clear.
Like nawroth said, for testing you should use the ImpermanentGraphDatabase. It pretty much auto-fixes all your problems.
If you're not testing, there are two ways really. I generally have two methods available to me. One is the clearDB method, in which I recursively delete the DB path. I use the FileUtils library for this, and it's pretty much a single line of code :
FileUtils.deleteRecursively(new File(DB_PATH));
The other one is to remove every node in the database EXCEPT THE REFERENCE NODE, using the removeAllNodes method. There is a simple query for this, which you execute like this:
engine.execute("START n = node(*), ref = node(0) WHERE n<>ref DELETE n");
Important to note is that you have to call the clearDB method BEFORE you create a new EmbeddedGraphDatabase object. The removeAllNodes method is called AFTER you've created this object.
There is a helper class
Neo4jHelper.cleanDb(db);
(it comes from org.springframework.data.neo4j.support.node.Neo4jHelper and the db you reference is a GraphDatabaseService)
You also have the ability to dump it:
Neo4jHelper.dumpDb();

Not Saving Domain Object with Grails Searchable Plugin

Is it possible to use the Searchable Plugin to create an index of objects and never actually save the objects to the database?
I think so. If you never save the objects, then I think you can just call:
domainInstance.index()
But I've never tried it, so I'm not sure if it just indexes the one instance, or any instance of that class.
See here:
https://svn.codehaus.org/grails-plugins/grails-searchable/trunk/src/groovy/org/codehaus/groovy/grails/plugins/searchable/compass/domain/DynamicDomainMethodUtils.groovy
If you just want to save the object but just want to index manually, then set the following in your conf/Searchable.groovy config file:
mirrorChanges = false
bulkIndexOnStartup = false
See here: https://svn.codehaus.org/grails-plugins/grails-searchable/trunk/src/conf/Searchable.groovy

Resources