2009/05/20 - Apache Shale has been retired.

For more information, please explore the Attic.

View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to you under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.shale.usecases.model.minimal;
19  
20  import java.util.ArrayList;
21  import java.util.Date;
22  import java.util.Iterator;
23  import java.util.List;
24  import org.apache.shale.usecases.model.User;
25  import org.apache.shale.usecases.model.UsersDAO;
26  
27  /***
28   * <p>Minimal implementation of {@link UsersDAO} that provides no persistence
29   * at all.  A single user (username="user", password="pass") is created at
30   * initialization time.</p>
31   *
32   * $Id: MinimalUsersDAO.java 464373 2006-10-16 04:21:54Z rahul $
33   */
34  public class MinimalUsersDAO implements UsersDAO {
35  
36      
37      // ------------------------------------------------------------ Constructors
38  
39  
40      /***
41       * <p>Construct an instance with a single valid user.</p>
42       */
43      public MinimalUsersDAO() {
44  
45          User user = new MinimalUser();
46          user.setCategories(new int[0]);
47          user.setConfirmed(true);
48          user.setCreated(new Date());
49          user.setEmailAddress("user@user.com");
50          user.setFullName("Initial User");
51          user.setId(1);
52          user.setPassword("pass");
53          user.setUpdated(user.getCreated());
54          user.setUsername("user");
55          users.add(user);
56  
57      }
58  
59  
60      // ------------------------------------------------------ Instance Variables
61  
62  
63      /***
64       * <p><code>List</code> of valid {@link User} instances.</p>
65       */
66      private List users = new ArrayList();
67  
68  
69      // -------------------------------------------------------- UsersDAO Methods
70  
71  
72      // Specified by UsersDAO
73      public User createUser() {
74  
75          User user = new MinimalUser();
76          return user;
77  
78      }
79  
80  
81      // Specified by UsersDAO
82      public User findUser(int id) {
83  
84          synchronized (users) {
85              for (int i = 0; i < users.size(); i++) {
86                  User user = (User) users.get(i);
87                  if (id == user.getId()) {
88                      return user;
89                  }
90              }
91              return null;
92          }
93  
94      }
95  
96  
97      // Specified by UsersDAO
98      public User findUser(String username) {
99  
100         synchronized (users) {
101             for (int i = 0; i < users.size(); i++) {
102                 User user = (User) users.get(i);
103                 if (username.equals(user.getUsername())) {
104                     return user;
105                 }
106             }
107             return null;
108         }
109 
110     }
111 
112 
113     // Specified by UsersDAO
114     public void insertUser(User user) {
115 
116         synchronized (users) {
117             user.setCreated(new Date());
118             user.setId(calculateId());
119             user.setUpdated(user.getCreated());
120             User insert = new MinimalUser();
121             insert.setCategories(user.getCategories());
122             insert.setConfirmed(user.isConfirmed());
123             insert.setCreated(user.getCreated());
124             insert.setEmailAddress(user.getEmailAddress());
125             insert.setFullName(user.getFullName());
126             insert.setId(user.getId());
127             insert.setPassword(user.getPassword());
128             insert.setUpdated(user.getUpdated());
129             insert.setUsername(user.getUsername());
130             users.add(insert);
131         }
132 
133     }
134 
135 
136     // Specified by UsersDAO
137     public void updateUser(User user) {
138 
139         synchronized (users) {
140             User update = findUser(user.getId());
141             if (update == null) {
142                 throw new IllegalArgumentException("" + user.getId());
143             }
144             user.setUpdated(new Date());
145             update.setCategories(user.getCategories());
146             update.setEmailAddress(user.getEmailAddress());
147             update.setFullName(user.getFullName());
148             update.setPassword(user.getPassword());
149             update.setUpdated(user.getUpdated());
150             update.setUsername(user.getUsername());
151         }
152         
153 
154     }
155 
156 
157     // --------------------------------------------------------- Private Methods
158 
159 
160     /***
161      * <p>Calculate and return the next available user identifier.
162      * <strong>WARNING</strong> - It is assumed that the caller has
163      * already locked the <code>users</code> instance variable, to
164      * avoid race conditions.</p>
165      */
166     private int calculateId() {
167 
168         int id = 0;
169         Iterator items = users.iterator();
170         while (items.hasNext()) {
171             User item = (User) items.next();
172             if (item.getId() > id) {
173                 id = item.getId();
174             }
175         }
176         return id + 1;
177 
178     }
179 
180 
181 }