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 import java.util.Set ; 57 58 61 abstract public class AbstractUserAttributeStore 62 implements UserAttributeStore 63 { 64 protected static String DELETE = "<delete>"; 65 66 private Map <String , String > _defaultMap; 67 private Map <String , String > _nameLinkMap; 68 private Map <String , String > _reverseNameLinkMap; 69 70 79 abstract protected Map <String , String > load( PortletRequest request ) 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 109 public void addDefault(String name, String value) 110 { 111 if (_defaultMap == null) 112 _defaultMap = new LinkedHashMap <String , String >(); 113 114 _defaultMap.put(name, value); 115 } 116 117 public void addDefault(NameValuePair nameValuePair) 118 { 119 addDefault(nameValuePair.getName(), nameValuePair.getValue()); 120 } 121 122 126 public void addNameLink(String name, String link) 127 { 128 if (_nameLinkMap == null) 129 _nameLinkMap = new HashMap <String , String >(); 130 131 _nameLinkMap.put(name, link); 132 } 133 134 public void addNameLink(NameLink nameLink) 135 { 136 addNameLink(nameLink.getName(), nameLink.getLink()); 137 } 138 139 public Map <String , String > getUserAttributeMap( PortletRequest request, 140 Set <String > names ) 141 throws IOException 142 { 143 Map <String , String > storeMap = load(request); 144 145 StoreUpdateMap<String , String > map = new StoreUpdateMap<String , String >(); 146 147 if (_nameLinkMap != null) { 148 synchronized (_nameLinkMap) { 149 if (_reverseNameLinkMap == null) 150 _reverseNameLinkMap 151 = KeyLinkMap.<String >getReverseKeyLinkMap(_nameLinkMap); 152 } 153 } 154 155 map.start(_nameLinkMap, _reverseNameLinkMap, 156 _defaultMap, storeMap, names, DELETE); 157 158 return map; 159 } 160 161 public void finish( Map <String , String > userAttributeMap ) 162 throws IOException 163 { 164 StoreUpdateMap<String , String > map 165 = (StoreUpdateMap<String , String >) userAttributeMap; 166 167 Map <String , String > storeMap = map.getStoreMap(); 168 Map <String , String > updateMap = map.getUpdateMap(); 169 170 map.finish(); 171 172 if (updateMap != null) 173 save(storeMap, updateMap); 174 175 unload(storeMap); 176 } 177 178 } 179 180 | Popular Tags |