View Javadoc

1   /*
2    * $Id: MemoryProcessContextDAO.java,v 1.1 2004/12/15 14:18:09 patforna Exp $
3    *
4    * Copyright (c) 2004 Patric Fornasier, Pawel Kowalski
5    * Berne University of Applied Sciences
6    * School of Engineering and Information Technology
7    * All rights reserved.
8    */
9   package bexee.dao;
10  
11  import java.util.HashMap;
12  import java.util.Map;
13  
14  import org.apache.commons.id.serial.AlphanumericGenerator;
15  
16  import bexee.core.ProcessContext;
17  
18  /***
19   * This class uses memory rather than persistent storage to store and load
20   * <code>ProcessContext</code> objects.
21   * 
22   * @version $Revision: 1.1 $, $Date: 2004/12/15 14:18:09 $
23   * @author Patric Fornasier
24   * @author Pawel Kowalski
25   */
26  public class MemoryProcessContextDAO implements ProcessContextDAO {
27  
28      protected static Map map;
29  
30      private static AlphanumericGenerator generator;
31  
32      /***
33       * Creates a new <code>MemoryProcessContextDAO</code> that uses a static
34       * <code>Map</code> to manage the stored <code>ProcessContext</code>
35       * objects.
36       */
37      public MemoryProcessContextDAO() {
38          if (map == null) {
39              map = new HashMap();
40          }
41          if (generator == null) {
42              generator = generator = new AlphanumericGenerator(false, 10);
43          }
44      }
45  
46      public void delete(String id) {
47          map.remove(id);
48      }
49  
50      public ProcessContext find(String id) {
51          return (ProcessContext) map.get(id);
52      }
53  
54      public String insert(ProcessContext ctx) {
55  
56          // generate id and set it on process context
57          String id = generator.nextStringIdentifier();
58          ctx.setId(id);
59  
60          // store process context and return id
61          map.put(id, ctx);
62          return id;
63      }
64  
65      public void update(ProcessContext ctx) throws DAOException {
66  
67          // check if we have that object already
68          String id = ctx.getId();
69          ProcessContext oldCtx = find(id);
70          if (oldCtx == null) {
71              throw new DAOException("No object with id " + id + "exists");
72          }
73  
74          // replace with new version
75          map.put(id, ctx);
76      }
77  }