KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > samples > tour > beans > DatabaseFacade


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.cocoon.samples.tour.beans;
18
19 import java.util.LinkedList JavaDoc;
20 import java.util.Date JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.Iterator JavaDoc;
23
24 /** Provides access to the "database", which is in nothing more than an in-memory data structure.
25  *
26  * @author bdelacretaz@codeconsult.ch
27  */

28  
29 public class DatabaseFacade {
30     /** singleton */
31     private static final DatabaseFacade m_instance = new DatabaseFacade();
32
33     /** list of tasks (simulated database) */
34     private final LinkedList JavaDoc m_tasks = new LinkedList JavaDoc();
35
36     /** simulated data */
37     final String JavaDoc title [] = {
38         "Design DB interface",
39         "Load test data",
40         "User interface design",
41         "Usability test",
42         "Market acceptance study",
43         "Investors demo",
44         "Public press release",
45         "TV interview",
46         "Local radio interview",
47         "Location shooting for promo video",
48         "Government group demo",
49         "Test users screening",
50         "Promo video casting",
51         "Promo video editing",
52         "Audio sweetening",
53         "Sound design",
54         "Games rules evaluation"
55     };
56
57     /** simulated data */
58     final String JavaDoc name [] = {
59         "Donald Duck",
60         "Miles Davis",
61         "Leonardo DaVinci",
62         "Rodney Curtis",
63         "Foad Zee"
64     };
65
66     /** simulated data */
67     final String JavaDoc commentText [] = {
68         "Revised with management",
69         "Checked budget",
70         "Called Mr.Smith about it, he's ok with the current state",
71         "Asked Fred Flintstone for a budget extension",
72         "Looked at the planning, logistics says we won't make it"
73     };
74
75     private DatabaseFacade() {
76         loadTestData();
77     }
78
79     public static DatabaseFacade getInstance() {
80         return m_instance;
81     }
82
83     /** get our list of tasks */
84     public List JavaDoc getTasks() {
85         return m_tasks;
86     }
87
88     /** get a single TaskBean */
89     public TaskBean getTaskBeanById(int id) throws Exception JavaDoc {
90         // inefficient but ok for this demo!
91
TaskBean result = null;
92         for(Iterator JavaDoc it=m_tasks.iterator(); it.hasNext(); ) {
93             final TaskBean tb = (TaskBean)it.next();
94             if(tb.getId() == id) {
95                 result = tb;
96                 break;
97             }
98         }
99
100         if(result == null) {
101             throw new Exception JavaDoc("Not found: TaskBean having id=" + id);
102         }
103         return result;
104     }
105
106     /** for tests, simulate data */
107     private void loadTestData() {
108         for(int i=0; i < title.length; i++) {
109             m_tasks.add(generateTask(title[i],i));
110         }
111     }
112
113     /** create a TaskBean and generate a random number of comments in it */
114     private TaskBean generateTask(String JavaDoc title,int index) {
115         final TaskBean tb = new TaskBean();
116         tb.setTaskName(title);
117         tb.setAssignedTo(name[index % name.length]);
118
119         final int nComments = (int)(Math.random() * 20);
120         final long MSEC_OFFSET = (1000L * 3600L * 24L) + (1000L * 60 * 12);
121         long timestamp = new Date JavaDoc().getTime() - nComments * MSEC_OFFSET;
122         for(int i=0; i < nComments; i++) {
123             final TaskCommentBean tcb = new TaskCommentBean();
124             tcb.setDate(new Date JavaDoc(timestamp));
125             tcb.setComment(commentText[i % commentText.length]);
126             tb.addComment(tcb);
127             timestamp += MSEC_OFFSET;
128         }
129
130         return tb;
131     }
132
133     /** version info */
134     public String JavaDoc getVersion() {
135         return "$Id: DatabaseFacade.java 30932 2004-07-29 17:35:38Z vgritsenko $";
136     }
137 }
138
Popular Tags