KickJava   Java API By Example, From Geeks To Geeks.

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


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.snipsnap.app.Application;
29 import org.snipsnap.date.Month;
30 import org.snipsnap.semanticweb.rss.Rssify;
31 import org.snipsnap.user.Permissions;
32 import org.snipsnap.user.Roles;
33 import org.snipsnap.xmlrpc.WeblogsPing;
34
35 import java.sql.Date JavaDoc;
36 import java.util.Calendar JavaDoc;
37 import java.util.GregorianCalendar JavaDoc;
38 import java.util.List JavaDoc;
39
40 /**
41  * BlogImpl for Blog.
42  *
43  * @author stephan
44  * @version $Id: BlogImpl.java 1772 2004-11-01 08:17:00Z leo $
45  */

46
47 public class BlogImpl implements Blog {
48   private String JavaDoc startName;
49   private String JavaDoc name;
50   private Snip blog;
51   private SnipSpace space;
52
53   public BlogImpl(SnipSpace space, String JavaDoc blogName) {
54     this.space = space;
55     // May not be initialized, so set it to something sane
56
this.startName = Application.get().getConfiguration().getStartSnip();
57     if (blogName == null || "".equals(blogName)) {
58       blogName = startName;
59     }
60     this.name = blogName;
61     this.blog = space.load(name);
62   }
63
64   public String JavaDoc getName() {
65     return this.name;
66   }
67
68   public Snip post(String JavaDoc content, String JavaDoc title) {
69     return post(BlogKit.getContent(title, content));
70   }
71
72   public Snip post(String JavaDoc content) {
73     Date JavaDoc date = new Date JavaDoc(new java.util.Date JavaDoc().getTime());
74     return post(content, date);
75   }
76
77   public Snip post(String JavaDoc content, Date JavaDoc date) {
78     String JavaDoc snipName = name + "/" + SnipUtil.toName(date);
79     Snip snip = null;
80
81     // Should several posts per day be one snip or
82
// several snips?
83
int max = findMaxPost(snipName);
84     if (true) { // Application.get().getConfiguration().allow(Configuration.APP_PERM_MULTIPLEPOSTS)) {
85
// if (space.exists(snipName)) {
86
// }
87
// how many children do exist?
88
// get the highest count
89
// e.g. start/2003-05-06
90
snip = snip = space.create(snipName + "/" + (max + 1), content);
91     } else {
92       // there was a post with a least /1 then add to that post
93
if (max != 0) {
94         snipName = snipName + "/" + max;
95       }
96       if (space.exists(snipName)) {
97         snip = space.load(snipName);
98         snip.setContent(content + "\n\n" + snip.getContent());
99       } else {
100         snip = space.create(snipName, content);
101       }
102     }
103
104     //snip.setParent(weblog);
105
snip.addPermission(Permissions.EDIT_SNIP, Roles.OWNER);
106     space.systemStore(snip);
107
108     // Ping weblogs.com that we changed our site
109
WeblogsPing.ping(blog);
110     return snip;
111   }
112
113   private int findMaxPost(String JavaDoc snipName) {
114     Snip[] existing = space.match(snipName + "/");
115     int max = 0;
116     //System.out.println("found="+existing.length+" name="+snipName);
117
for (int i = 0; i < existing.length; i++) {
118       Snip post = existing[i];
119       String JavaDoc name = post.getName();
120       int index = name.lastIndexOf('/');
121       //System.out.println("name="+name);
122
if (index != -1) {
123         try {
124           //System.out.println("parsing="+name.substring(index+1));
125
max = Math.max(Integer.parseInt(name.substring(index + 1)), max);
126           //System.out.println("max="+max);
127
} catch (NumberFormatException JavaDoc e) {
128           //
129
}
130       }
131     }
132     return max;
133   }
134
135   public List JavaDoc getFlatPosts() {
136     return Rssify.rssify(getPosts(10));
137   }
138
139   public List JavaDoc getPosts(int count) {
140     Calendar JavaDoc endC = new GregorianCalendar JavaDoc();
141     endC.setTime(new java.util.Date JavaDoc());
142     endC.add(Calendar.DAY_OF_MONTH, 1);
143     Calendar JavaDoc startC = new GregorianCalendar JavaDoc();
144     startC.setTimeInMillis(blog.getCTime().getTime());
145
146     List JavaDoc posts = space.getByDate(blog.getName(), Month.toKey(startC), Month.toKey(endC));
147     return posts.subList(0, Math.min(posts.size(), count));
148   }
149
150   public Snip getSnip() {
151     return blog;
152   }
153 }
154
Popular Tags