1 48 49 package com.caucho.portal.generic; 50 51 import javax.portlet.PortletRequest; 52 import java.io.IOException ; 53 import java.util.HashMap ; 54 import java.util.LinkedHashMap ; 55 import java.util.Map ; 56 57 60 abstract public class AbstractPreferencesStore 61 implements PreferencesStore 62 { 63 protected static String [] DELETE = new String [] { "<delete>" }; 64 65 private LinkedHashMap <String , String []> _defaultMap; 66 private HashMap <String , String > _nameLinkMap; 67 private Map <String , String > _reverseNameLinkMap; 68 69 78 abstract protected Map <String , String []> load( PortletRequest request, 79 String namespace ) 80 throws IOException ; 81 82 94 abstract protected void save( Map <String , String []> storeMap, 95 Map <String , String []> updateMap ) 96 throws IOException ; 97 98 106 abstract protected void unload(Map <String , String []> map); 107 108 public void addDefault(String name, String value) 109 { 110 if (_defaultMap == null) 111 _defaultMap = new LinkedHashMap <String , String []>(); 112 113 String [] values = _defaultMap.get(name); 114 115 if (values == null) 116 values = new String [] { value }; 117 else { 118 String [] newValues = new String [values.length + 1]; 119 120 int i = 0; 121 for (; i < values.length; i++) { 122 newValues[i] = values[i]; 123 } 124 125 newValues[i] = value; 126 } 127 128 _defaultMap.put(name, values); 129 } 130 131 public void addDefault(NameValuePair nameValuePair) 132 { 133 addDefault(nameValuePair.getName(), nameValuePair.getValue()); 134 } 135 136 140 public void addNameLink(String name, String link) 141 { 142 if (_nameLinkMap == null) 143 _nameLinkMap = new HashMap <String , String >(); 144 145 _nameLinkMap.put(name, link); 146 } 147 148 public void addNameLink(NameLink nameLink) 149 { 150 addNameLink(nameLink.getName(), nameLink.getLink()); 151 } 152 153 public Map <String , String []> getPreferencesMap( PortletRequest request, 154 String namespace ) 155 throws IOException 156 { 157 Map <String , String []> storeMap = load(request, namespace); 158 159 StoreUpdateMap<String , String []> map 160 = new StoreUpdateMap<String , String []>(); 161 162 if (_nameLinkMap != null) { 163 synchronized (_nameLinkMap) { 164 if (_reverseNameLinkMap == null) 165 _reverseNameLinkMap 166 = KeyLinkMap.<String >getReverseKeyLinkMap(_nameLinkMap); 167 } 168 } 169 170 map.start(_nameLinkMap, _reverseNameLinkMap, 171 _defaultMap, storeMap, null, DELETE); 172 173 return map; 174 } 175 176 public void finish( Map <String , String []> preferencesMap ) 177 throws IOException 178 { 179 StoreUpdateMap<String , String []> map 180 = (StoreUpdateMap<String , String []>) preferencesMap; 181 182 Map <String , String []> storeMap = map.getStoreMap(); 183 Map <String , String []> updateMap = map.getUpdateMap(); 184 185 map.finish(); 186 187 if (updateMap != null) 188 save(storeMap, updateMap); 189 190 unload(storeMap); 191 } 192 } 193 194 | Popular Tags |