<fim-prefix>ITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. <br>
* See the License for the specific language governing permissions and <br>
* limitations under the License.
* <p>
* Copyright (c) since 2004 at Multimedia- & E-Learning Services (MELS),<br>
* University of Zurich, Switzerland.
* <hr>
* <a href="http://www.openolat.org">
* OpenOLAT - Online Learning and Training</a><br>
* This file has been modified by the OpenOLAT community. Changes are licensed
* under the Apache 2.0 license as the original file.  
* <p>
* Initial code contributed and copyrighted by<br>
* JGS goodsolutions GmbH, http://www.goodsolutions.ch
* <p>
*/
package org.olat.core.util.prefs.db;

import java.util.Iterator;
import java.util.List;

import org.apache.logging.log4j.Logger;
import org.olat.core.id.Identity;
import org.olat.core.logging.Tracing;
import org.olat.core.util.prefs.Preferences;
import org.olat.core.util.prefs.PreferencesStorage;
import org.olat.core.util.xml.XStreamHelper;
import org.olat.properties.Property;
import org.olat.properties.PropertyManager;

import com.thoughtworks.xstream.XStream;

/**
 * Description:<br>
 * <P>
 * Initial Date: 21.06.2006 <br>
 * 
 * @author Felix Jost
 */
public class DbStorage implements PreferencesStorage {
	
	private static final Logger log = Tracing.createLoggerFor(DbStorage.class);

	static final String USER_PROPERTY_KEY = "v2guipreferences";
	
	private static final XStream xstream = XStreamHelper.createXStreamInstance();
	static {
		// BUG: CWE-91 XML Injection (aka Blind XPath Injection)
		// 
		// FIXED:
	<fim-middle>	XStreamHelper.allowDefaultPackage(xstream);
		xstream.ignoreUnknownElements();
	}

	@Override
	public Preferences getPreferencesFor(Identity identity, boolean useTransientPreferences) {
		if (useTrans<fix-suffix>ientPreferences) {
			return createEmptyDbPrefs(identity,true);
		} else {			
			try {
				return getPreferencesFor(identity);
			} catch (Exception e) {
				log.error("Retry after exception", e);
				return getPreferencesFor(identity);
			}
		}
	}

	@Override
	public void updatePreferencesFor(Preferences prefs, Identity identity) {
		String props = xstream.toXML(prefs);
		Property property = getPreferencesProperty(identity);
		if (property == null) {
			property = PropertyManager.getInstance().createPropertyInstance(identity, null, null, null, DbStorage.USER_PROPERTY_KEY, null, null,
					null, props);
			// also save the properties to db, here (strentini)
			// fixes the "non-present gui preferences" for new users, or where guiproperties were manually deleted
			PropertyManager.getInstance().saveProperty(property);
		} else {
			property.setTextValue(props);
			PropertyManager.getInstance().updateProperty(property);
		}
	}

	/**
	 * search x-stream serialization in properties table, <|endoftext|>