public AppData findOrCreateByPersonAndGadgetDefinitionIds(final long gadgetDefinitionId, final String personId)
{
String logMsg = "GadgetDefId#" + gadgetDefinitionId + ", open social id: " + personId;
log.info("Looking for the AppData in the database for " + logMsg);
AppData outputAppData = null;
Query query = getEntityManager().createQuery(
"from AppData a where a.person.openSocialId = "
+ ":openSocialId and a.gadgetDefinition.id = :gadgetDefinitionId").setParameter("openSocialId",
personId).setParameter("gadgetDefinitionId", gadgetDefinitionId);
List<AppData> appDataList = query.getResultList();
if (appDataList.size() > 0)
{
// found it
log.info("Found the AppData in the database for " + logMsg);
outputAppData = appDataList.get(0);
}
else
{
// didn't find it - create it
log.info("Didn't find the AppData in the database for " + logMsg + " - building it");
Query getPersonQuery = getEntityManager().createQuery("from Person p where p.openSocialId = :openSocialId")
.setParameter("openSocialId", personId);
Query getGadgetDefQuery = getEntityManager().createQuery(
"from GadgetDefinition gd where gd.id = :gadgetDefinitionId").setParameter("gadgetDefinitionId",
gadgetDefinitionId);
Person currentPerson = null;
GadgetDefinition currentGadgetDefinition = null;
List<Person> peopleList = getPersonQuery.getResultList();
if (peopleList.size() > 0)
{
List<GadgetDefinition> gadgetDefList = getGadgetDefQuery.getResultList();
if (gadgetDefList.size() > 0)
{
log.info("Persisting AppData for " + logMsg);
currentPerson = peopleList.get(0);
currentGadgetDefinition = gadgetDefList.get(0);
outputAppData = new AppData(currentPerson, currentGadgetDefinition);
getEntityManager().persist(outputAppData);
}
}
}