Hi experts
I am new to Grails and trying to do something without much luck. Ok here I go
I have two tables abc and def. I want to move data from abc to def based on some rules. For my project, I am expecting several such cases in future (mapping tables klm with xyz, pqrst with jklmn so on so forth), hence the need for generalization).
The classes Abc and Def are shown below
I created a domain class called Mappings (that will act as parent for all future mappings) that is shown belowCode:class Abc { String firstAbc String secondAbc static constraints = { firstAbc(nullable:true) secondAbc(nullable:true) } } class Def { String fieldA String someRandomField static constraints = { } }
Then I extended Mappings to create my specific instance called AbcDefMapping, that is shown belowCode:class Mappings { String inputTable // in this case this will be abc String inputField // can be firstAbc or secondAbc String inputValue // some value String outputTable // def in this case String outputField // either of fieldA or someRandomField String outputValue }
The rules for mapping abc with def are stored in AbcDefMapping. One such rule could beCode:class AbcDefMapping extends Mappings { }
The values for mappings in this case would bewhen abc.firstAbc is “jack”, store “jacky” in def.fieldA.
I want to add a method called transform() in Controller.groovy that would do the needful, so that in future I just create the domain classes, mapping class and scaffold the controller. I want to list all the input objects(Abc.list() in this case), access the mapping rules as explained above, create new Def domain instances and save them. For this I need to split ${classname} (=AbcDefMapping in this case) to separate out the two domain classes (Abc and Def in this case). I wrote a for loop to split ${classname} at uppercases and stored them in the array csplit[]. At this stage, I am not sure how to split the ${classname} so that it transforms into Abc.list() in my generated controller class. Here is my codeinputTable = abc
inputField = firstAbc
inputValue = “jack”
outputTable = def
outputField = fieldA
outputValue = “jacky”
Please suggest how do I do this or is there a better way to achieve what I am trying to achieve.Code:def transform(){ def csplit = [] def count = 0 for (i in ${className}){ if (i == i.toUpperCase() && count!=0){ csplit.add(count) } count++ } def f = csplit[0].length() def inputs = ${className.substring(0,3)}.list() // here instead of manually inserting 3, I need to insert f def inputTable = ${className}.substring(0,1).toLowerCase() + ${className}.substring(1,3) //replace 3 with f again def mappings = ${className}.executeQuery(" from ${className} acm where acm.inputTable = '" + inputTable + "' order by acm.inputField, acm.inputValue, acm.outputField, acm.outputValue") println mappings }


Reply With Quote
