KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snipsnap > snip > Modified


1 /*
2  * This file is part of "SnipSnap Wiki/Weblog".
3  *
4  * Copyright (c) 2002 Stephan J. Schmidt, Matthias L. Jugel
5  * All Rights Reserved.
6  *
7  * Please visit http://snipsnap.org/ for updates and contact.
8  *
9  * --LICENSE NOTICE--
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  * --LICENSE NOTICE--
24  */

25
26 package org.snipsnap.snip;
27
28 import org.radeox.util.i18n.ResourceManager;
29 import org.snipsnap.container.Components;
30 import org.snipsnap.user.UserManager;
31
32 import java.sql.Timestamp JavaDoc;
33 import java.text.MessageFormat JavaDoc;
34
35 /**
36  * Object with modified information, e.g. for snips
37  * Modified can be pretty printed.
38  *
39  * @author stephan
40  * @version $Id: Modified.java 1648 2004-06-10 14:11:20Z leo $
41  */

42
43 public class Modified {
44   String JavaDoc cUser, mUser;
45   Timestamp JavaDoc cTime, mTime;
46
47   public Modified(String JavaDoc cUser, String JavaDoc mUser, Timestamp JavaDoc cTime, Timestamp JavaDoc mTime) {
48     this.cUser = cUser;
49     this.mUser = mUser;
50     this.cTime = cTime;
51     this.mTime = mTime;
52   }
53
54   public Modified() {
55   }
56
57   public String JavaDoc getcUser() {
58     return cUser;
59   }
60
61   public void setcUser(String JavaDoc cUser) {
62     this.cUser = cUser;
63   }
64
65   public String JavaDoc getmUser() {
66     return mUser;
67   }
68
69   public void setmUser(String JavaDoc mUser) {
70     this.mUser = mUser;
71   }
72
73   public Timestamp JavaDoc getcTime() {
74     return cTime;
75   }
76
77   public void setcTime(Timestamp JavaDoc cTime) {
78     this.cTime = cTime;
79   }
80
81   public Timestamp JavaDoc getmTime() {
82     return mTime;
83   }
84
85   public void setmTime(Timestamp JavaDoc mTime) {
86     this.mTime = mTime;
87   }
88
89   /**
90    * Generate a pretty print of the modified object,
91    * e.g. "Created by steph - Last edited ..."
92    *
93    * @return Pretty print of modified object
94    */

95   public String JavaDoc toString() {
96     MessageFormat JavaDoc mf = new MessageFormat JavaDoc(ResourceManager.getString("i18n.messages", "modified.info"),
97                                          ResourceManager.getLocale("i18n.messages"));
98
99     UserManager um = (UserManager) Components.getComponent(UserManager.class);
100     return mf.format(new Object JavaDoc[]{
101       um.exists(cUser) ? SnipLink.createLink(cUser) : cUser,
102       um.exists(mUser) ? SnipLink.createLink(mUser) : mUser,
103       getNiceTime(mTime)
104     });
105   }
106
107   /**
108    * Return a short version of the modification user and time.
109    * @return pretty print of the user and modified time
110    */

111   public String JavaDoc getShort() {
112     MessageFormat JavaDoc mf = new MessageFormat JavaDoc(ResourceManager.getString("i18n.messages", "modified.info.short"),
113                                          ResourceManager.getLocale("i18n.messages"));
114     UserManager um = (UserManager) Components.getComponent(UserManager.class);
115     return mf.format(new Object JavaDoc[] {
116       um.exists(cUser) ? SnipLink.createLink(cUser) : cUser,
117       getNiceTime(mTime)
118     });
119   }
120
121   // Should go to a date class
122
/**
123    * Generate a pretty print of the difference
124    * between the timestamp and now.
125    * e.g. show minutes, minutes and hours and
126    * days since now.
127    * "3 hours, 5 minutes ago.", "4 days ago."
128    *
129    * @param time Timestamp to pretty print
130    * @return Pretty string
131    */

132   public static String JavaDoc getNiceTime(Timestamp JavaDoc time) {
133     if (time == null) {
134       return "";
135     }
136     java.util.Date JavaDoc now = new java.util.Date JavaDoc();
137     return getNiceTime(now.getTime(), time.getTime());
138
139   }
140
141   public static String JavaDoc getNiceTime(long now, long time) {
142     long secs = (now - time) / 1000; // amount of seconds
143

144     //int sec = (int) secs % 60;
145
long mins = secs / 60; // amount of minutes
146
int min = (int) mins % 60; // current minute within hour
147
long hours = mins / 60; // amount of hours
148
int hour = (int) hours % 24; // hour within day
149
int days = (int) hours / 24; // amount of days
150
int day = days % 365; // current day within the year
151
int years = days / 365; // amount of years
152

153     StringBuffer JavaDoc nice = new StringBuffer JavaDoc();
154     if (secs < 60) {
155       nice.append(ResourceManager.getString("i18n.messages", "modified.time.just"));
156     } else {
157       if (hours == 0) {
158         MessageFormat JavaDoc mf = new MessageFormat JavaDoc(ResourceManager.getString("i18n.messages", "modified.time.minutes"),
159                                              ResourceManager.getLocale("i18n.messages"));
160         mf.format(new Object JavaDoc[]{new Long JavaDoc(min)}, nice, null);
161       } else if (days == 0) {
162         MessageFormat JavaDoc mf = new MessageFormat JavaDoc(ResourceManager.getString("i18n.messages", "modified.time.hours"),
163                                              ResourceManager.getLocale("i18n.messages"));
164         mf.format(new Object JavaDoc[]{new Long JavaDoc(hour), new Long JavaDoc(min)}, nice, null);
165       } else if(years == 0) {
166         MessageFormat JavaDoc mf = new MessageFormat JavaDoc(ResourceManager.getString("i18n.messages", "modified.time.days"),
167                                              ResourceManager.getLocale("i18n.messages"));
168         mf.format(new Object JavaDoc[]{new Long JavaDoc(days)}, nice, null);
169       } else {
170         MessageFormat JavaDoc mf = new MessageFormat JavaDoc(ResourceManager.getString("i18n.messages", "modified.time.years"),
171                                              ResourceManager.getLocale("i18n.messages"));
172         mf.format(new Object JavaDoc[]{new Long JavaDoc(years), new Long JavaDoc(day)}, nice, null);
173       }
174     }
175     return nice.toString();
176   }
177 }
178
Popular Tags