001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.cocoon.samples.tour.beans;
019:
020: import java.util.LinkedList;
021: import java.util.Date;
022: import java.util.List;
023: import java.util.Iterator;
024:
025: /** Provides access to the "database", which is in nothing more than an in-memory data structure.
026: *
027: * @author bdelacretaz@codeconsult.ch
028: */
029:
030: public class DatabaseFacade {
031: /** singleton */
032: private static final DatabaseFacade m_instance = new DatabaseFacade();
033:
034: /** list of tasks (simulated database) */
035: private final LinkedList m_tasks = new LinkedList();
036:
037: /** simulated data */
038: final String title[] = { "Design DB interface", "Load test data",
039: "User interface design", "Usability test",
040: "Market acceptance study", "Investors demo",
041: "Public press release", "TV interview",
042: "Local radio interview",
043: "Location shooting for promo video",
044: "Government group demo", "Test users screening",
045: "Promo video casting", "Promo video editing",
046: "Audio sweetening", "Sound design",
047: "Games rules evaluation" };
048:
049: /** simulated data */
050: final String name[] = { "Donald Duck", "Miles Davis",
051: "Leonardo DaVinci", "Rodney Curtis", "Foad Zee" };
052:
053: /** simulated data */
054: final String commentText[] = { "Revised with management",
055: "Checked budget",
056: "Called Mr.Smith about it, he's ok with the current state",
057: "Asked Fred Flintstone for a budget extension",
058: "Looked at the planning, logistics says we won't make it" };
059:
060: private DatabaseFacade() {
061: loadTestData();
062: }
063:
064: public static DatabaseFacade getInstance() {
065: return m_instance;
066: }
067:
068: /** get our list of tasks */
069: public List getTasks() {
070: return m_tasks;
071: }
072:
073: /** get a single TaskBean */
074: public TaskBean getTaskBeanById(int id) throws Exception {
075: // inefficient but ok for this demo!
076: TaskBean result = null;
077: for (Iterator it = m_tasks.iterator(); it.hasNext();) {
078: final TaskBean tb = (TaskBean) it.next();
079: if (tb.getId() == id) {
080: result = tb;
081: break;
082: }
083: }
084:
085: if (result == null) {
086: throw new Exception("Not found: TaskBean having id=" + id);
087: }
088: return result;
089: }
090:
091: /** for tests, simulate data */
092: private void loadTestData() {
093: for (int i = 0; i < title.length; i++) {
094: m_tasks.add(generateTask(title[i], i));
095: }
096: }
097:
098: /** create a TaskBean and generate a random number of comments in it */
099: private TaskBean generateTask(String title, int index) {
100: final TaskBean tb = new TaskBean();
101: tb.setTaskName(title);
102: tb.setAssignedTo(name[index % name.length]);
103:
104: final int nComments = (int) (Math.random() * 20);
105: final long MSEC_OFFSET = (1000L * 3600L * 24L)
106: + (1000L * 60 * 12);
107: long timestamp = new Date().getTime() - nComments * MSEC_OFFSET;
108: for (int i = 0; i < nComments; i++) {
109: final TaskCommentBean tcb = new TaskCommentBean();
110: tcb.setDate(new Date(timestamp));
111: tcb.setComment(commentText[i % commentText.length]);
112: tb.addComment(tcb);
113: timestamp += MSEC_OFFSET;
114: }
115:
116: return tb;
117: }
118:
119: /** version info */
120: public String getVersion() {
121: return "$Id: DatabaseFacade.java 433543 2006-08-22 06:22:54Z crossley $";
122: }
123: }
|