mirror of
				https://github.com/Alfresco/alfresco-community-repo.git
				synced 2025-10-29 15:21:53 +00:00 
			
		
		
		
	So the bug was caused by 2 problems: one in ScriptPreferenceService and one in person.sites.get.js The first problem was that ScriptPreferenceService constructs raw Mozilla NativeObjects - a very unusual practice in itself - and it does not provide a default value as required by ECMA 9.1 from the ECMA standard. I've added this code to the NativeObject (can't change the type to ScriptableHashMap<K, V> or similar as the API is published). I've also fixed the same bug (unreported, possibly never apparent) in a jbpm class. The second problem was that person.sites.get.js simply could never return preferences data if the caller provided a filter (favourites or recents) and did not also provide a page size. There was a logic error in the algorithm such that the size defaulted to 0 thus providing a 'page' of zero results as a default. I assume this is a merge error from cloud, but I don't know. I added tests for the JavaScript API's favourites/recents filter calls and logging here and there. Also deleted some dead code. git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@55474 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
		
			
				
	
	
		
			182 lines
		
	
	
		
			5.9 KiB
		
	
	
	
		
			Java
		
	
	
	
	
	
			
		
		
	
	
			182 lines
		
	
	
		
			5.9 KiB
		
	
	
	
		
			Java
		
	
	
	
	
	
| /*
 | |
|  * Copyright (C) 2005-2010 Alfresco Software Limited.
 | |
|  *
 | |
|  * This file is part of Alfresco
 | |
|  *
 | |
|  * Alfresco is free software: you can redistribute it and/or modify
 | |
|  * it under the terms of the GNU Lesser General Public License as published by
 | |
|  * the Free Software Foundation, either version 3 of the License, or
 | |
|  * (at your option) any later version.
 | |
|  *
 | |
|  * Alfresco is distributed in the hope that it will be useful,
 | |
|  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 | |
|  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | |
|  * GNU Lesser General Public License for more details.
 | |
|  *
 | |
|  * You should have received a copy of the GNU Lesser General Public License
 | |
|  * along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
 | |
|  */
 | |
| package org.alfresco.repo.preference.script;
 | |
| 
 | |
| import java.io.Serializable;
 | |
| import java.util.HashMap;
 | |
| import java.util.Map;
 | |
| 
 | |
| import org.alfresco.repo.jscript.BaseScopableProcessorExtension;
 | |
| import org.alfresco.service.ServiceRegistry;
 | |
| import org.alfresco.service.cmr.preference.PreferenceService;
 | |
| import org.alfresco.service.transaction.TransactionService;
 | |
| import org.mozilla.javascript.NativeObject;
 | |
| 
 | |
| /**
 | |
|  * @author Roy Wetherall
 | |
|  */
 | |
| public class ScriptPreferenceService extends BaseScopableProcessorExtension
 | |
| {
 | |
|     @SuppressWarnings("unused")
 | |
|     private ServiceRegistry services;
 | |
|     
 | |
|     /** Preference Service */
 | |
|     private PreferenceService preferenceService;
 | |
|     
 | |
|     private TransactionService transactionService;
 | |
|     
 | |
|     public void setTransactionService(TransactionService transactionService)
 | |
|     {
 | |
|         this.transactionService = transactionService;
 | |
|     }
 | |
|     
 | |
|     public void setServiceRegistry(ServiceRegistry services)
 | |
|     {
 | |
|         this.services = services;
 | |
|     }
 | |
|     
 | |
|     public void setPreferenceService(PreferenceService preferenceService)
 | |
|     {
 | |
|         this.preferenceService = preferenceService;
 | |
|     }
 | |
|     
 | |
|     public boolean getAllowWrite()
 | |
|     {
 | |
|         return transactionService.getAllowWrite();
 | |
|     }
 | |
|     
 | |
|     public NativeObject getPreferences(String userName)
 | |
|     {
 | |
|         return getPreferences(userName, null);
 | |
|     }
 | |
|     
 | |
|     public NativeObject getPreferences(String userName, String preferenceFilter)
 | |
|     {
 | |
|         // It's a tad unusual to return a NativeObject like this - at least within Alfresco.
 | |
|         // But we can't change it to e.g. a ScriptableHashMap as the API is published.
 | |
|         Map<String, Serializable> prefs = this.preferenceService.getPreferences(userName, preferenceFilter);        
 | |
|         NativeObject result = new NativeObjectDV();
 | |
|         
 | |
|         for (Map.Entry<String, Serializable> entry : prefs.entrySet())
 | |
|         {
 | |
|             String[] keys = entry.getKey().replace(".", "+").split("\\+");            
 | |
|             setPrefValue(keys, entry.getValue(), result);
 | |
|         }        
 | |
|         
 | |
|         return result;
 | |
|     }
 | |
|     
 | |
|     /**
 | |
|      * This extension of NativeObject adds a default value. See ALF-20023 for some background.
 | |
|      */
 | |
|     private static class NativeObjectDV extends NativeObject
 | |
|     {
 | |
|         private static final long serialVersionUID = 1L;
 | |
|         
 | |
|         @Override public Object getDefaultValue(@SuppressWarnings("rawtypes") Class typeHint) { return toString(); }
 | |
|     }
 | |
|     
 | |
|     private void setPrefValue(String[] keys, Serializable value, NativeObject object)
 | |
|     {
 | |
|         NativeObject currentObject = object;
 | |
|         int index = 0;
 | |
|         for (String key : keys)
 | |
|         {
 | |
|             if (index == keys.length-1)
 | |
|             {
 | |
|                 currentObject.put(key, currentObject, value);
 | |
|             }
 | |
|             else
 | |
|             {
 | |
|                 NativeObject newObject = null;
 | |
|                 Object temp = currentObject.get(key, currentObject);
 | |
|                 if (temp == null || temp instanceof NativeObject == false)
 | |
|                 {
 | |
|                     newObject = new NativeObjectDV();
 | |
|                     currentObject.put(key, currentObject, newObject);
 | |
|                 }
 | |
|                 else
 | |
|                 {
 | |
|                     newObject = (NativeObject)temp;
 | |
|                 }
 | |
|                 currentObject = newObject;
 | |
|             }
 | |
|          
 | |
|             index ++;
 | |
|         }
 | |
|     }
 | |
|     
 | |
|     public void setPreferences(String userName, NativeObject preferences)
 | |
|     {
 | |
|         Map<String, Serializable> values = new HashMap<String, Serializable>(10);
 | |
|         getPrefValues(preferences, null, values);
 | |
|         
 | |
|         this.preferenceService.setPreferences(userName, values);
 | |
|     }
 | |
|     
 | |
|     private void getPrefValues(NativeObject currentObject, String currentKey, Map<String, Serializable> values)
 | |
|     {
 | |
|         Object[] ids = currentObject.getIds();
 | |
|         for (Object id : ids)
 | |
|         {
 | |
|             String key = getAppendedKey(currentKey, id.toString());
 | |
|             Object value = currentObject.get(id.toString(), currentObject);
 | |
|             if (value instanceof NativeObject)
 | |
|             {
 | |
|                 getPrefValues((NativeObject)value, key, values);
 | |
|             }
 | |
|             else
 | |
|             {
 | |
|                 values.put(key, (Serializable)value);
 | |
|             }
 | |
|         }
 | |
|     }
 | |
|     
 | |
|     public void clearPreferences(String userName)
 | |
|     {
 | |
|         this.preferenceService.clearPreferences(userName, null);
 | |
|     }
 | |
|     
 | |
|     
 | |
|     /**
 | |
|      * Clear the preference values
 | |
|      * 
 | |
|      * @param userName
 | |
|      * @param preferenceFilter
 | |
|      */
 | |
|     public void clearPreferences(String userName, String preferenceFilter)
 | |
|     {
 | |
|         this.preferenceService.clearPreferences(userName, preferenceFilter);
 | |
|     }
 | |
|     
 | |
|     private String getAppendedKey(String currentKey, String key)
 | |
|     {
 | |
|         StringBuffer buffer = new StringBuffer(64);
 | |
|         if (currentKey != null && currentKey.length() != 0)
 | |
|         {
 | |
|             buffer.append(currentKey).append(".").append(key);
 | |
|         }
 | |
|         else
 | |
|         {    
 | |
|             buffer.append(key);
 | |
|         }
 | |
|         return buffer.toString();
 | |
|     }
 | |
| }
 |