View Javadoc

1   /*
2    *  Copyright (C) 2003-2005 SINTEF
3    *  Author:  fredrik dot vraalsen at sintef dot no
4    *  Webpage: http://coras.sourceforge.net/
5    *
6    *  This program is free software; you can redistribute it and/or
7    *  modify it under the terms of the GNU Lesser General Public License
8    *  as published by the Free Software Foundation; either version 2.1
9    *  of the License, or (at your option) any later version.
10   *
11   *  This program is distributed in the hope that it will be useful,
12   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   *  Lesser General Public License for more details.
15   *
16   *  You should have received a copy of the GNU Lesser General Public
17   *  License along with this program; if not, write to the Free
18   *  Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19   *  02111-1307 USA
20   */
21  package no.sintef.assetrepository.admin;
22  
23  import java.awt.BorderLayout;
24  import java.awt.GridLayout;
25  import java.awt.event.ItemEvent;
26  import java.net.URL;
27  import java.rmi.RemoteException;
28  import java.util.ArrayList;
29  import java.util.Collection;
30  import java.util.Iterator;
31  import java.util.Map;
32  
33  import javax.security.auth.login.LoginContext;
34  import javax.security.auth.login.LoginException;
35  import javax.swing.DefaultComboBoxModel;
36  import javax.swing.JComboBox;
37  import javax.swing.JFrame;
38  import javax.swing.JList;
39  import javax.swing.JOptionPane;
40  import javax.swing.JPanel;
41  import javax.swing.JScrollPane;
42  import javax.swing.JTextArea;
43  
44  import no.sintef.assetrepository.VersionHistory;
45  import no.sintef.assetrepository.interfaces.AssetServices;
46  import no.sintef.assetrepository.interfaces.AssetServicesUtil;
47  import no.sintef.assetrepository.interfaces.AssetTypeValue;
48  import no.sintef.assetrepository.interfaces.AssetVersionPK;
49  import no.sintef.assetrepository.interfaces.AssetVersionValue;
50  import no.sintef.assetrepository.interfaces.VersionedAssetPK;
51  import no.sintef.assetrepository.interfaces.VersionedAssetValue;
52  /***
53   * @author fvr
54   *
55   * TODO To change the template for this generated type comment go to
56   * Window - Preferences - Java - Code Style - Code Templates
57   */
58  public class AdminUI extends JFrame {
59  
60  	private AssetServices services = null;
61  	
62  	private javax.swing.JPanel jContentPane = null;
63  
64  	private JList assetList = null;
65  	private JList assetVersionList = null;
66  	private JTextArea assetVersionData = null;
67  	private JTextArea assetData = null;
68  	private JList childList = null;
69  	private JScrollPane jScrollPane = null;
70  	private JScrollPane jScrollPane1 = null;
71  	private JScrollPane jScrollPane2 = null;
72  	private JTextArea childArea = null;
73  	private JPanel jPanel = null;
74  	private JPanel jPanel1 = null;
75  	private JComboBox assetTypeBox = null;
76  	/***
77  	 * This method initializes jList	
78  	 * 	
79  	 * @return javax.swing.JList	
80  	 */    
81  	private JList getAssetList() {
82  		if (assetList == null) {
83  			assetList = new JList();
84  			assetList.addListSelectionListener(new javax.swing.event.ListSelectionListener() { 
85  				public void valueChanged(javax.swing.event.ListSelectionEvent e) {  
86  					assetSelected(getAssetList().getSelectedValue());
87  				}
88  			});
89  		}
90  		return assetList;
91  	}
92  
93  	/***
94  	 * This method initializes jList1	
95  	 * 	
96  	 * @return javax.swing.JList	
97  	 */    
98  	private JList getAssetVersionList() {
99  		if (assetVersionList == null) {
100 			assetVersionList = new JList();
101 			assetVersionList.addListSelectionListener(new javax.swing.event.ListSelectionListener() { 
102 				public void valueChanged(javax.swing.event.ListSelectionEvent e) {   
103 					assetVersionSelected((AssetVersionPK) getAssetVersionList().getSelectedValue());
104 				}
105 			});
106 		}
107 		return assetVersionList;
108 	}
109 	/***
110 	 * This method initializes jTextArea	
111 	 * 	
112 	 * @return javax.swing.JTextArea	
113 	 */    
114 	private JTextArea getAssetVersionData() {
115 		if (assetVersionData == null) {
116 			assetVersionData = new JTextArea();
117 		}
118 		return assetVersionData;
119 	}
120 	/***
121 	 * This method initializes jTextArea	
122 	 * 	
123 	 * @return javax.swing.JTextArea	
124 	 */    
125 	private JTextArea getAssetData() {
126 		if (assetData == null) {
127 			assetData = new JTextArea();
128 		}
129 		return assetData;
130 	}
131 	
132 	/***
133 	 * This method initializes jList	
134 	 * 	
135 	 * @return javax.swing.JList	
136 	 */    
137 	private JList getChildList() {
138 		if (childList == null) {
139 			childList = new JList();
140 			childList.addMouseListener(new java.awt.event.MouseAdapter() { 
141 				public void mouseClicked(java.awt.event.MouseEvent e) {
142 					VersionedAssetPK assetPk = (VersionedAssetPK) getChildList().getSelectedValue();
143 					if (e.getClickCount() >= 2) {
144 						assetSelected(assetPk);
145 					} else {
146 						childSelected(assetPk);
147 					}
148 				}
149 			});
150 		}
151 		return childList;
152 	}
153 	/***
154 	 * This method initializes jScrollPane	
155 	 * 	
156 	 * @return javax.swing.JScrollPane	
157 	 */    
158 	private JScrollPane getJScrollPane() {
159 		if (jScrollPane == null) {
160 			jScrollPane = new JScrollPane();
161 			jScrollPane.setViewportView(getAssetList());
162 		}
163 		return jScrollPane;
164 	}
165 	/***
166 	 * This method initializes jScrollPane1	
167 	 * 	
168 	 * @return javax.swing.JScrollPane	
169 	 */    
170 	private JScrollPane getJScrollPane1() {
171 		if (jScrollPane1 == null) {
172 			jScrollPane1 = new JScrollPane();
173 			jScrollPane1.setViewportView(getAssetVersionList());
174 		}
175 		return jScrollPane1;
176 	}
177 	/***
178 	 * This method initializes jScrollPane2	
179 	 * 	
180 	 * @return javax.swing.JScrollPane	
181 	 */    
182 	private JScrollPane getJScrollPane2() {
183 		if (jScrollPane2 == null) {
184 			jScrollPane2 = new JScrollPane();
185 			jScrollPane2.setViewportView(getChildList());
186 		}
187 		return jScrollPane2;
188 	}
189 	/***
190 	 * This method initializes jTextArea	
191 	 * 	
192 	 * @return javax.swing.JTextArea	
193 	 */    
194 	private JTextArea getChildArea() {
195 		if (childArea == null) {
196 			childArea = new JTextArea();
197 		}
198 		return childArea;
199 	}
200 	/***
201 	 * This method initializes jPanel	
202 	 * 	
203 	 * @return javax.swing.JPanel	
204 	 */    
205 	private JPanel getJPanel() {
206 		if (jPanel == null) {
207 			GridLayout gridLayout11 = new GridLayout();
208 			jPanel = new JPanel();
209 			jPanel.setLayout(gridLayout11);
210 			gridLayout11.setRows(2);
211 			gridLayout11.setColumns(3);
212 			jPanel.add(getJScrollPane(), null);
213 			jPanel.add(getJScrollPane1(), null);
214 			jPanel.add(getJScrollPane2(), null);
215 			jPanel.add(getAssetData(), null);
216 			jPanel.add(getAssetVersionData(), null);
217 			jPanel.add(getChildArea(), null);
218 		}
219 		return jPanel;
220 	}
221 	/***
222 	 * This method initializes jPanel1	
223 	 * 	
224 	 * @return javax.swing.JPanel	
225 	 */    
226 	private JPanel getJPanel1() {
227 		if (jPanel1 == null) {
228 			jPanel1 = new JPanel();
229 			jPanel1.add(getAssetTypeBox(), null);
230 		}
231 		return jPanel1;
232 	}
233 	/***
234 	 * This method initializes jComboBox	
235 	 * 	
236 	 * @return javax.swing.JComboBox	
237 	 */    
238 	private JComboBox getAssetTypeBox() {
239 		if (assetTypeBox == null) {
240 			assetTypeBox = new JComboBox();
241 			assetTypeBox.addItemListener(new java.awt.event.ItemListener() { 
242 				public void itemStateChanged(java.awt.event.ItemEvent e) {   
243 					if (e.getStateChange() == ItemEvent.SELECTED) {
244 						assetTypeSelected((AssetTypeValue) getAssetTypeBox().getSelectedItem());
245 					}
246 				}
247 			});
248 		}
249 		return assetTypeBox;
250 	}
251             public static void main(String[] args) {
252 		URL authURL = AdminUIold.class.getClassLoader().getResource("auth.conf");
253 		String authLocation = (authURL != null ? authURL.toString() : "auth.conf");
254 		System.setProperty("java.security.auth.login.config", authLocation);
255 
256 		AdminUI ui = new AdminUI();
257 		ui.show();
258         ui.login();
259         ui.loadAssets();
260         // ui.loadAssetTypes();
261 	}
262     
263 	private void login() {
264 		LoginDialog loginDialog = new LoginDialog(this);
265 		LoginContext lc = null;
266 		try {
267 			lc = new LoginContext("asset-repository", loginDialog);
268 			loginDialog.setLoginContext(lc);
269 			loginDialog.setLocationRelativeTo(this);
270 			loginDialog.show();
271 		} catch (LoginException e) {
272 			e.printStackTrace();
273 		}
274 		if (!loginDialog.loginSucceeded()) {
275 			JOptionPane.showMessageDialog(this,
276 					"Login failed, program exiting.", 
277 					"Login failed", 
278 					JOptionPane.ERROR_MESSAGE);
279 			System.exit(0);
280 		}
281 		lc = null;
282         try {
283 			services = AssetServicesUtil.getHome().create();
284 		} catch (Exception e) {
285 			// TODO Auto-generated catch block
286 			e.printStackTrace();
287 		}
288 	}
289 
290 	private void loadAssets() {
291 		try {
292 			Collection assets = services.getAllVersionedAssets();
293 			for (Iterator i = assets.iterator(); i.hasNext();) {
294 				VersionedAssetValue asset = (VersionedAssetValue) i.next();
295 				System.out.print("Checking asset " + asset.getPrimaryKey() + "...");
296 				boolean error = false;
297 				Collection versionPks = asset.getVersionHistory().getVersions();
298 				ArrayList missingVersions = new ArrayList();
299 				for (Iterator j = versionPks.iterator(); j.hasNext();) {
300 					AssetVersionPK versionPk = (AssetVersionPK) j.next();
301 					AssetVersionValue version = null;
302 					try {
303 						version = services.getAssetVersion(versionPk);
304 					} catch (Exception e) {
305 						System.err.println(e.getMessage());
306 					}
307 					if (version == null) {
308 						if (!error) {
309 							System.err.println();
310 						}
311 						error = true;
312 						System.err.println("Missing version " + versionPk);
313 						System.err.flush();
314 						missingVersions.add(versionPk);
315 					}
316 				}
317 				if (!error) {
318 					System.out.println(" OK");
319 				} else {
320 					System.out.println("Attempting to fix asset...");
321 					VersionHistory history = asset.getVersionHistory();
322 					for (Iterator k = missingVersions.iterator(); k.hasNext();) {
323 						AssetVersionPK versionPk = (AssetVersionPK) k.next();
324 						System.out.println("Removing version reference " + versionPk + "...");
325 						history.removeVersion(versionPk);
326 					}
327 					if (!history.getVersions().contains(asset.getBaseVersion())) {
328 						AssetVersionPK[] pks = (AssetVersionPK[]) history.getVersions().toArray(new AssetVersionPK[0]);
329 						AssetVersionPK newBaseVersion = pks[pks.length - 1];
330 						System.out.println("Setting new base version " + newBaseVersion + "...");
331 						asset.setBaseVersion(newBaseVersion);
332 					}
333 					// TODO also check root version
334 					System.out.print("Attempting to update database...");
335 					try {
336 						services.modifyVersionedAsset(asset);
337 						System.out.println(" OK");
338 						System.out.flush();
339 					} catch (Exception e) {
340 						System.err.println(" failed: " + e.getMessage());
341 						System.err.flush();
342 					}
343 				}
344 			}
345 		} catch (RemoteException e) {
346 			e.printStackTrace();
347 		}
348 	}
349 	
350 	private void loadAssetTypes() {
351 		try {
352 			Collection assetTypes = services.getAllAssetTypes();
353 			getAssetTypeBox().setModel(new DefaultComboBoxModel(assetTypes.toArray()));
354 			getAssetTypeBox().setSelectedIndex(0);
355 		} catch (RemoteException e) {
356 			// TODO Auto-generated catch block
357 			e.printStackTrace();
358 		}
359 	}
360 	
361 	private void assetTypeSelected(AssetTypeValue assetType) {
362 		try {
363 			Collection assets = services.getAssetsByType(assetType.getName());
364 			getAssetList().setListData(assets.toArray());
365 			getAssetList().setSelectedIndex(0);
366 		} catch (Exception e) {
367 			e.printStackTrace();
368 		}
369 	}
370 	
371 	private void assetSelected(Object o) {
372 		if (o == null || o instanceof VersionedAssetPK) {
373 			assetSelected((VersionedAssetPK)o);
374 		} else if (o instanceof VersionedAssetValue) {
375 			assetSelected(((VersionedAssetValue)o).getPrimaryKey());
376 		} 
377 	}
378 	
379 	private void assetSelected(VersionedAssetPK assetPk) {
380 		try {
381 			VersionedAssetValue asset = assetPk != null ? services.getVersionedAsset(assetPk) : null;
382 			String s = "";
383 			if (asset != null) {
384 				AssetTypeValue type = services.getAssetType(asset.getAssetType());
385 				s += 
386 					"Id: " + asset.getId() + "\n" 
387 					+ "Name: " + asset.getName() + "\n"
388 					+ "Type: " + type.getName() + "\n"
389 					+ "Description: " + asset.getDescription();
390 				Map map = asset.getProperties();
391 				for (Iterator i = map.keySet().iterator(); i.hasNext();) {
392 					String key = (String) i.next();
393 					String value = (String) map.get(key);
394 					s += "\n" + key + " = " + value;
395 				}
396 			}
397 			getAssetData().setText(s);
398 			
399 			VersionHistory versionHistory = asset != null ? asset.getVersionHistory() : null;
400 			Collection versions = versionHistory != null ? versionHistory.getVersions() : null;
401 			getAssetVersionList().setListData(versions != null ? versions.toArray() : new Object[0]);
402 			if (versions != null && versions.size() > 0) {
403 				getAssetVersionList().setSelectedIndex(0);
404 			} else {
405 				assetVersionSelected(null);
406 			}
407 		} catch (RemoteException e) {
408 			getAssetData().setText(e.getMessage());
409 		}
410 	}
411 	
412 	private void assetVersionSelected(AssetVersionPK assetVersionPk) {
413 		try {
414 			AssetVersionValue assetVersion = assetVersionPk != null ? services.getAssetVersion(assetVersionPk) : null;
415 			String s = "";
416 			if (assetVersion != null) {
417 				AssetTypeValue type = services.getAssetType(assetVersion.getAssetType());
418 				s += 
419 					"Id: " + assetVersion.getId() + "\n" 
420 					+ "Name: " + assetVersion.getName() + "\n"
421 					+ "Type: " + type.getName() + "\n"
422 					+ "Description: " + assetVersion.getDescription();
423 				Map map = assetVersion.getProperties();
424 				for (Iterator i = map.keySet().iterator(); i.hasNext();) {
425 					String key = (String) i.next();
426 					String value = (String) map.get(key);
427 					s += "\n" + key + " = " + value;
428 				}
429 			}
430 			getAssetVersionData().setText(s);
431 
432 			Collection children = assetVersion != null ? assetVersion.getChilds() : null;
433 			getChildList().setListData(children != null ? children.toArray() : new Object[0]);
434 			if (children != null && children.size() > 0) {
435 				getChildList().setSelectedIndex(0);
436 			} else {
437 				childSelected(null);
438 			}
439 		} catch (RemoteException e) {
440 			getAssetVersionData().setText(e.getMessage());
441 		}
442 	}
443 	
444 	private void childSelected(VersionedAssetPK childPk) {
445 		try {
446 			VersionedAssetValue child = childPk != null ? services.getVersionedAsset(childPk) : null;
447 			String s = "";
448 			if (child != null) {
449 				AssetTypeValue type = services.getAssetType(child.getAssetType());
450 				s +=
451 					"Id: " + child.getId() + "\n" 
452 					+ "Name: " + child.getName() + "\n"
453 					+ "Type: " + type.getName() + "\n"
454 					+ "Description: " + child.getDescription();
455 				Map map = child.getProperties();
456 				for (Iterator i = map.keySet().iterator(); i.hasNext();) {
457 					String key = (String) i.next();
458 					String value = (String) map.get(key);
459 					s += "\n" + key + " = " + value;
460 				}
461 			}
462 			childArea.setText(s);
463 		} catch (RemoteException e) {
464 			getChildArea().setText(e.getMessage());
465 		}
466 	}
467 	
468 	/***
469 	 * This is the default constructor
470 	 */
471 	public AdminUI() {
472 		super();
473 		initialize();
474 	}
475 	/***
476 	 * This method initializes this
477 	 * 
478 	 * @return void
479 	 */
480 	private void initialize() {
481 		this.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
482 		this.setBounds(0, 0, 600, 400);
483 		this.setContentPane(getJContentPane());
484 		this.setTitle("JFrame");
485 	}
486 	/***
487 	 * This method initializes jContentPane
488 	 * 
489 	 * @return javax.swing.JPanel
490 	 */
491 	private javax.swing.JPanel getJContentPane() {
492 		if(jContentPane == null) {
493 			jContentPane = new javax.swing.JPanel();
494 			jContentPane.setLayout(new BorderLayout());
495 			jContentPane.add(getJPanel1(), java.awt.BorderLayout.NORTH);
496 			jContentPane.add(getJPanel(), java.awt.BorderLayout.CENTER);
497 		}
498 		return jContentPane;
499 	}
500 }