Skip to content

Instantly share code, notes, and snippets.

@axeda
Created September 23, 2013 14:05
Show Gist options
  • Save axeda/6670879 to your computer and use it in GitHub Desktop.
Save axeda/6670879 to your computer and use it in GitHub Desktop.
Illustrates Simple Map/List Persistence to Extended Map/List
import static com.axeda.sdk.v2.dsl.Bridges.*
import com.axeda.services.v2.ExtendedMap
import com.axeda.services.v2.ExtendedMapCriteria
import com.axeda.services.v2.ExtendedList
import com.axeda.services.v2.ExtendedListCriteria
import com.axeda.services.v2.FindExtendedMapResult
import com.axeda.services.v2.FindExtendedListResult
import com.axeda.services.v2.NamedValue
try {
def list = ["FirstValue", "SecondValue"]
def tuples = [
TestKey: "TestValue",
key1: "value1"
]
def extendedMap = persistMap("TestMap", tuples)
logger.info(extendedMap.dump())
def extendedList = persistList("TestList", list)
logger.info(extendedList.dump())
}
catch(Exception e){
logger.info(e.localizedMessage)
}
def persistMap(String mapName, Map tuples){
ExtendedMap extendedMap = extendedMapBridge.findOne(new ExtendedMapCriteria(name: mapName))
def newmap = tuples.collect{ k,v ->
def node = extendedMap?.map?.find{ it.name == k } ?: new NamedValue(name: k, value: v)
node.value = v
node
}
if (extendedMap){
extendedMap.map = newmap
extendedMapBridge.update(extendedMap)
}
else {
def result = extendedMapBridge.create(new ExtendedMap(name: mapName, map: newmap))
if (result.successful){
extendedMap = extendedMapBridge.findById(result.succeeded.first().id)
}
}
extendedMap
}
def persistList(String listName, List list){
ExtendedList extendedList = extendedListBridge.findOne(new ExtendedListCriteria(name: listName))
if (extendedList){
extendedList.list = list
def result = extendedListBridge.update(extendedList)
}
else {
def result = extendedListBridge.create(new ExtendedList(name: listName, list: list))
if (result.successful){
extendedList = extendedListBridge.findById(result.succeeded.first().id)
}
}
extendedList
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment