List<PortletPreference> inserts = new LinkedList<PortletPreference>();
Iterator<DatabasePreference> preferences = getPersistenceBrokerTemplate().getIteratorByQuery(query);
while (preferences.hasNext())
{
DatabasePreference preference = preferences.next();
PortletPreference found = map.get(preference.getName());
if (found == null)
{
deletes.add(preference);
}
else
{
updates.add(preference);
}
mergeMap.put(preference.getName(), preference);
}
for (PortletPreference preference : map.values())
{
DatabasePreference dbPref = mergeMap.get(preference.getName());
if (dbPref == null)
{
inserts.add(preference);
}
}
// perform database manipulations
for (DatabasePreference dbPref : deletes)
{
getPersistenceBrokerTemplate().delete(dbPref);
}
for (PortletPreference preference : inserts)
{
DatabasePreference dbPref = new DatabasePreference();
dbPref.setDtype(DISCRIMINATOR_USER);
dbPref.setApplicationName(appName);
dbPref.setPortletName(portletName);
dbPref.setEntityId(entityId);
dbPref.setUserName(userName);
dbPref.setName(preference.getName());
dbPref.setReadOnly(preference.isReadOnly());
short index = 0;
for (String value : preference.getValues())
{
DatabasePreferenceValue dbValue = new DatabasePreferenceValue();
dbValue.setIndex(index);
dbValue.setValue(value);
dbPref.getPreferenceValues().add(dbValue);
index++;
}
getPersistenceBrokerTemplate().store(dbPref);
}
for (DatabasePreference dbPref : updates)
{
dbPref.getPreferenceValues().clear();
PortletPreference preference = map.get(dbPref.getName());
short index = 0;
for (String value : preference.getValues())
{
DatabasePreferenceValue dbValue = new DatabasePreferenceValue();
dbValue.setIndex(index);
dbValue.setValue(value);
index++;