1 package com.sslexplorer.sample; 2 3 import java.util.ArrayList ; 4 import java.util.Calendar ; 5 import java.util.HashMap ; 6 import java.util.Iterator ; 7 import java.util.List ; 8 import java.util.Map ; 9 10 import com.sslexplorer.core.CoreServlet; 11 import com.sslexplorer.core.Database; 12 import com.sslexplorer.policyframework.Resource; 13 14 21 public class SampleDatabase implements Database { 22 23 private Map data; 24 private int counter; 25 26 29 public SampleDatabase() { 30 this.data = new HashMap (); 31 this.counter = 0; 32 } 33 34 37 public List getSamples() { 38 return new ArrayList (this.data.values()); 39 } 40 41 45 public Sample getSample(int id) { 46 if (this.data.containsKey(String.valueOf(id))) { 47 return (Sample) this.data.get(String.valueOf(id)); 48 } else { 49 return null; 50 } 51 } 52 53 61 public Sample addSample(String name, String description, int parentResourcePermission) throws Exception { 62 Calendar now = Calendar.getInstance(); 63 Sample sample = new DefaultSample(counter, name, description, parentResourcePermission, now, now); 64 if (this.data.containsKey(String.valueOf(counter))) { 65 throw new Exception ("Sample id already exists."); 66 } else { 67 this.data.put(String.valueOf(sample.getResourceId()), sample); 68 counter++; 69 return sample; 70 } 71 } 72 73 77 public Resource getSample(String name) { 78 for (Iterator iter = this.data.values().iterator(); iter.hasNext();) { 79 Sample element = (Sample) iter.next(); 80 if (element.getResourceName().equals(name)) { 81 return element; 82 } 83 return null; 84 } 85 86 return null; 87 } 88 89 94 public Sample removeSample(int resourceId) throws Exception { 95 Sample s = getSample(resourceId); 96 if (s != null) { 97 this.data.remove(String.valueOf(resourceId)); 98 } else { 99 throw new Exception ("No Sample with id " + resourceId); 100 } 101 return s; 102 } 103 104 108 public void updateSample(Sample sample) throws Exception { 109 Calendar now = Calendar.getInstance(); 110 sample.setDateAmended(now); 111 if (this.data.containsKey(String.valueOf(sample.getResourceId()))) { 112 this.data.put(String.valueOf(sample.getResourceId()), sample); 113 } else { 114 throw new Exception ("The resource id does not exist."); 115 } 116 } 117 118 121 public void cleanup() throws Exception { 122 123 } 124 125 128 public void open(CoreServlet controllingServlet) throws Exception { 129 130 } 131 132 135 public void close() throws Exception { 136 } 137 } 138 | Popular Tags |