1 package com.genimen.djeneric.web.util; 2 3 import java.io.FileNotFoundException ; 4 import java.io.InputStream ; 5 import java.io.Serializable ; 6 import java.util.ArrayList ; 7 import java.util.Iterator ; 8 9 import com.genimen.djeneric.repository.DjContext; 10 import com.genimen.djeneric.repository.DjCredentials; 11 import com.genimen.djeneric.repository.DjExtent; 12 import com.genimen.djeneric.repository.DjMessenger; 13 import com.genimen.djeneric.repository.DjModelView; 14 import com.genimen.djeneric.repository.DjPersistenceManager; 15 import com.genimen.djeneric.repository.DjPersistenceManagerFactory; 16 import com.genimen.djeneric.repository.DjRepositoryDescriptor; 17 import com.genimen.djeneric.repository.DjSession; 18 import com.genimen.djeneric.repository.exceptions.DjenericException; 19 import com.genimen.djeneric.repository.exceptions.LogonException; 20 import com.genimen.djeneric.repository.exceptions.ObjectNotDefinedException; 21 import com.genimen.djeneric.structure.ResourceDefinition; 22 import com.genimen.djeneric.tools.io.DjenericDocumentHandler; 23 import com.genimen.djeneric.web.renderers.tree.WebTree; 24 25 public class DjenericSessionManager implements Serializable , DjMessenger 26 { 27 private static final long serialVersionUID = 1L; 28 29 private DjPersistenceManager _mgr; 30 private DjSession _primarySession; 31 private DjModelView _view; 32 private WebTree _tree; 33 private DjenericDocumentHandler _viewHandler; 34 private ResourceDefinition[] _images; 35 private ArrayList _activeSessions = new ArrayList (); 36 private DjenericXSLTransform _transformer = null; 37 private boolean _loggedIn = false; 38 39 private String _password; 40 private String _userid; 41 DjPersistenceManagerFactory _mgrFactory; 42 43 public DjenericSessionManager(InputStream repositories) throws FileNotFoundException , DjenericException 44 { 45 _mgrFactory = new DjPersistenceManagerFactory(this, repositories); 46 } 47 48 public DjSession getPrimarySession() 49 { 50 return _primarySession; 51 } 52 53 public WebTree getTree() throws DjenericException 54 { 55 if (_tree == null) 56 { 57 _tree = new WebTree(getPrimarySession(), getView().getExtentUsages()); 58 } 59 return _tree; 60 } 61 62 public void releaseDatasources() 63 { 64 Iterator it = _activeSessions.iterator(); 65 while (it.hasNext()) 66 { 67 DjSession session = (DjSession) it.next(); 68 if (session.isValid()) session.releaseDatasources(); 69 else it.remove(); 70 } 71 } 72 73 public DjCredentials getCredentials(DjRepositoryDescriptor repository) throws LogonException 74 { 75 DjCredentials cred = new DjCredentials(); 76 cred.setUserid(getUserId()); 77 cred.setPassword(getPassword()); 78 return cred; 79 } 80 81 public DjContext selectContext(DjContext[] ctxts) throws LogonException 82 { 83 for (int i = 0; i < ctxts.length; i++) 84 { 85 if (ctxts[i].getCode().equals(getContextCode())) return ctxts[i]; 86 } 87 throw new LogonException("Context " + getContextCode() + " not found"); 88 } 89 90 public DjRepositoryDescriptor selectRepository(DjRepositoryDescriptor[] descriptors) throws DjenericException 91 { 92 for (int i = 0; i < descriptors.length; i++) 93 { 94 if (descriptors[i].getName().equals(getRepositoryName())) return descriptors[i]; 95 } 96 throw new DjenericException("Repository " + getRepositoryName() + " not found"); 97 } 98 99 public DjModelView getView() throws DjenericException 100 { 101 if (_view == null) 102 { 103 DjModelView[] views = _mgr.getCurrentUser().getViews(); 104 if (views.length == 0) throw new DjenericException("No views defined for current user"); 105 106 if (getViewCode() == null) _view = views[0]; 107 else 108 { 109 for (int i = 0; i < views.length; i++) 110 { 111 if (views[i].getCode().equals(getViewCode())) _view = views[i]; 112 } 113 } 114 115 if (_view == null) throw new DjenericException("View " + getViewCode() + " not found"); 116 117 } 118 return _view; 119 } 120 121 public void showMessage(String title, String msg) 123 { 124 System.out.println(msg); 125 } 126 127 public void showWarning(String title, String msg) 129 { 130 System.err.println(msg); 131 } 132 133 public String getPassword() 135 { 136 return _password; 137 } 138 139 public String getUserId() 140 { 141 return _userid; 142 } 143 144 public String getContextCode() 145 { 146 return "dflt"; 147 } 148 149 public String getRepositoryName() 150 { 151 return "Hypersonic"; 152 } 153 154 public String getViewCode() 155 { 156 return null; 158 } 159 160 public ResourceDefinition getResource(String fileName) throws DjenericException 161 { 162 DjenericDocumentHandler vh = getViewHandler(); 163 return vh.getResource(fileName); 164 } 165 166 private DjenericDocumentHandler getViewHandler() throws DjenericException 167 { 168 if (_viewHandler == null) 169 { 170 _viewHandler = getView().getDocumentHandler(); 171 } 172 return _viewHandler; 173 } 174 175 public ResourceDefinition[] getImages() throws DjenericException 176 { 177 if (_images == null) 178 { 179 DjenericDocumentHandler vh = getViewHandler(); 180 ResourceDefinition[] res = vh.getResources(); 181 ArrayList result = new ArrayList (); 182 for (int i = 0; i < res.length; i++) 183 { 184 if (res[i].isImageIcon()) result.add(res[i]); 185 } 186 _images = (ResourceDefinition[]) result.toArray(new ResourceDefinition[result.size()]); 187 } 188 189 return _images; 190 } 191 192 public DjSession createSession() throws DjenericException 193 { 194 DjSession session = _mgr.createSession(); 195 _activeSessions.add(session); 196 return session; 197 } 198 199 public DjExtent getExtent(String extentName) throws ObjectNotDefinedException 200 { 201 return _mgr.getExtent(extentName); 202 } 203 204 public DjenericXSLTransform getTransformer() 205 { 206 if (_transformer == null) _transformer = new DjenericXSLTransform("com/genimen/djeneric/templates/dflt"); 207 return _transformer; 208 } 209 210 public boolean isLoggedIn() 211 { 212 return _loggedIn; 213 } 214 215 public void login(String userid, String password) throws DjenericException 216 { 217 _userid = userid; 218 _password = password; 219 220 if(_mgr != null) _mgr.close(); 221 _mgr = _mgrFactory.createInstance(); 222 _mgr.determineContext(); 223 224 _mgr.setEstimatedConcurrentSessions(0); 226 _primarySession = _mgr.createSession(); 227 _activeSessions.add(_primarySession); 228 _loggedIn = true; 229 } 230 } 231 | Popular Tags |