| 1 | package de.uka.ipd.sdq.sensorframework.dao.memory; |
| 2 | |
| 3 | import java.util.ArrayList; |
| 4 | import java.util.Collection; |
| 5 | import java.util.Collections; |
| 6 | import java.util.HashMap; |
| 7 | |
| 8 | import de.uka.ipd.sdq.sensorframework.entities.Experiment; |
| 9 | import de.uka.ipd.sdq.sensorframework.entities.ExperimentRun; |
| 10 | import de.uka.ipd.sdq.sensorframework.entities.Sensor; |
| 11 | import de.uka.ipd.sdq.sensorframework.entities.dao.IExperimentDAO; |
| 12 | import de.uka.ipd.sdq.sensorframework.entities.impl.ExperimentImpl; |
| 13 | |
| 14 | /** |
| 15 | * @author Steffen Becker |
| 16 | * |
| 17 | */ |
| 18 | public class MemoryExperimentDAO implements IExperimentDAO { |
| 19 | |
| 20 | private long nextID = 0; |
| 21 | private HashMap<Long, Experiment> index = new HashMap<Long, Experiment>(); |
| 22 | |
| 23 | private MemoryDAOFactory factory; |
| 24 | |
| 25 | public MemoryExperimentDAO(MemoryDAOFactory memoryDAOFactory) { |
| 26 | this.factory = memoryDAOFactory; |
| 27 | } |
| 28 | |
| 29 | public synchronized Experiment addExperiment(String p_experimentname) { |
| 30 | ExperimentImpl result = new ExperimentImpl(factory); |
| 31 | result.setExperimentID(nextID++); |
| 32 | result.setExperimentName(p_experimentname); |
| 33 | |
| 34 | index.put(result.getExperimentID(),result); |
| 35 | |
| 36 | return result; |
| 37 | } |
| 38 | |
| 39 | public synchronized Experiment get(long id) { |
| 40 | return index.get(id); |
| 41 | } |
| 42 | |
| 43 | public synchronized Collection<Experiment> getExperiments() { |
| 44 | return Collections.unmodifiableCollection(index.values()); |
| 45 | } |
| 46 | |
| 47 | /** {@inheritDoc}*/ |
| 48 | public synchronized Collection<Experiment> findByExperimentName(String searchKey) { |
| 49 | ArrayList<Experiment> result = new ArrayList<Experiment>(); |
| 50 | for (Experiment e:this.index.values()){ |
| 51 | if (e.getExperimentName().equals(searchKey)) |
| 52 | result.add(e); |
| 53 | } |
| 54 | return Collections.unmodifiableCollection(result); |
| 55 | } |
| 56 | |
| 57 | public void store(Experiment e) { |
| 58 | } |
| 59 | |
| 60 | public synchronized void removeExperiment(Experiment experiment, boolean doCascade) { |
| 61 | if (experiment == null) { |
| 62 | return; |
| 63 | } |
| 64 | |
| 65 | if ( doCascade == true ) { |
| 66 | //remove all experiment runs |
| 67 | for (ExperimentRun expRun:experiment.getExperimentRuns()) { |
| 68 | factory.createExperimentRunDAO().removeExperimentRun(expRun, true); |
| 69 | } |
| 70 | //remove all sensors |
| 71 | for (Sensor sensor:experiment.getSensors()) { |
| 72 | factory.createSensorDAO().removeSensor(sensor, true); |
| 73 | } |
| 74 | } |
| 75 | index.remove(experiment.getExperimentID()); |
| 76 | } |
| 77 | |
| 78 | public void storeAll() { |
| 79 | // Nothing to do here |
| 80 | } |
| 81 | |
| 82 | } |