001: /**
002: * LibreSource
003: * Copyright (C) 2004-2008 Artenum SARL / INRIA
004: * http://www.libresource.org - contact@artenum.com
005: *
006: * This file is part of the LibreSource software,
007: * which can be used and distributed under license conditions.
008: * The license conditions are provided in the LICENSE.TXT file
009: * at the root path of the packaging that enclose this file.
010: * More information can be found at
011: * - http://dev.libresource.org/home/license
012: *
013: * Initial authors :
014: *
015: * Guillaume Bort / INRIA
016: * Francois Charoy / Universite Nancy 2
017: * Julien Forest / Artenum
018: * Claude Godart / Universite Henry Poincare
019: * Florent Jouille / INRIA
020: * Sebastien Jourdain / INRIA / Artenum
021: * Yves Lerumeur / Artenum
022: * Pascal Molli / Universite Henry Poincare
023: * Gerald Oster / INRIA
024: * Mariarosa Penzi / Artenum
025: * Gerard Sookahet / Artenum
026: * Raphael Tani / INRIA
027: *
028: * Contributors :
029: *
030: * Stephane Bagnier / Artenum
031: * Amadou Dia / Artenum-IUP Blois
032: * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
033: */package org.libresource.web.wiki.macros;
034:
035: import org.libresource.Libresource;
036:
037: import org.libresource.core.CoreConstants;
038: import org.libresource.core.TimelineDateItem;
039: import org.libresource.core.TimelineItem;
040: import org.libresource.core.interfaces.LibresourceCoreService;
041:
042: import org.libresource.kernel.KernelConstants;
043: import org.libresource.kernel.interfaces.KernelService;
044:
045: import org.libresource.membership.MembershipConstants;
046: import org.libresource.membership.interfaces.MembershipService;
047:
048: import org.libresource.web.wiki.LibresourceRenderContext;
049:
050: import org.radeox.macro.BaseMacro;
051: import org.radeox.macro.parameter.MacroParameter;
052:
053: import java.io.IOException;
054: import java.io.InputStream;
055: import java.io.Writer;
056:
057: import java.net.URI;
058:
059: import java.text.MessageFormat;
060: import java.text.SimpleDateFormat;
061:
062: import java.util.Iterator;
063: import java.util.Properties;
064:
065: public class TimelineMacro extends BaseMacro {
066: private String scheme;
067: private String host;
068: private KernelService kernelService;
069: private MembershipService membershipService;
070:
071: public TimelineMacro() throws Exception {
072: kernelService = (KernelService) Libresource
073: .getService(KernelConstants.SERVICE);
074: membershipService = (MembershipService) Libresource
075: .getService(MembershipConstants.SERVICE);
076: scheme = kernelService.getUriScheme();
077: host = kernelService.getUriHost();
078: }
079:
080: public String getName() {
081: return "timeline";
082: }
083:
084: public void execute(Writer writer, MacroParameter params)
085: throws IllegalArgumentException, IOException {
086: try {
087: if ((params.getLength() != 1) && (params.getLength() != 2)) {
088: throw new IllegalArgumentException(
089: "syntax : timelineURI|(max days to display, default is 1)");
090: }
091:
092: // tout ca n'est qu'un vieux hack
093: String uri = params.get(0);
094: String LibresourceLinkPattern = "([^\"'=]|^)("
095: + scheme
096: + "://(%[\\p{Digit}A-Fa-f][\\p{Digit}A-Fa-f]|[-_.!~*';/?:@#&=+$,\\p{Alnum}])+)";
097: URI currentUri = ((LibresourceRenderContext) params
098: .getContext()).getUri();
099: URI resolvedUri = null;
100: int max = 1;
101:
102: if (params.getLength() > 1) {
103: max = Integer.parseInt(params.get(1));
104: }
105:
106: // resolve uri
107: if (uri.matches(LibresourceLinkPattern)) {
108: resolvedUri = kernelService.normalizeURI(new URI(uri));
109: } else {
110: URI relativeLink;
111:
112: if (uri.startsWith("/")) {
113: relativeLink = new URI(scheme + "://" + host + uri);
114: } else {
115: relativeLink = new URI(uri);
116: }
117:
118: resolvedUri = kernelService.normalizeURI(currentUri
119: .resolve(relativeLink));
120: }
121:
122: TimelineDateItem[] items = ((LibresourceCoreService) Libresource
123: .getService(CoreConstants.SERVICE))
124: .getTimelineDateItems(resolvedUri, max);
125: Properties properties = new Properties();
126:
127: InputStream is = null;
128:
129: if (membershipService.getProfile().getInfos().get(
130: "libresource-web.lang") == null) {
131: is = getClass().getResourceAsStream(
132: "/resources/application_en.properties");
133: } else {
134: is = getClass().getResourceAsStream(
135: "/resources/application_"
136: + membershipService.getProfile()
137: .getInfos().get(
138: "libresource-web.lang")
139: + ".properties");
140: }
141:
142: properties.load(is);
143: writer.write("<div class=\"viewTimeline\">");
144:
145: for (int i = 0; i < items.length; i++) {
146: writer.write("<div class=\"date\">"
147: + new SimpleDateFormat("dd MMM yyyy")
148: .format(items[i].getDate()) + "</div>");
149:
150: int status = 0;
151:
152: for (Iterator it = items[i].getItems().iterator(); it
153: .hasNext();) {
154: status++;
155:
156: TimelineItem item = (TimelineItem) it.next();
157: writer.write("<div class=\"item "
158: + (((status % 2) == 0) ? "odd" : "even")
159: + "\">");
160: writer.write("<span class=\"time\">"
161: + new SimpleDateFormat("h:mm a")
162: .format(item.getDate())
163: + "</span> : ");
164:
165: MessageFormat messageFormat = new MessageFormat(
166: properties.getProperty("timeline."
167: + item.getType()));
168: Object[] mparams = new Object[] {
169: "<a href=\""
170: + item.getUri().getPath()
171: .substring(1) + "\">"
172: + item.getResourceName() + "</a>",
173: item.getUser(), item.getArgs() };
174: writer.write(messageFormat.format(mparams));
175: writer.write("</div>");
176: }
177:
178: if (i != (items.length - 1)) {
179: writer.write("<br/>");
180: }
181: }
182:
183: writer.write("</div>");
184: } catch (IOException e) {
185: throw e;
186: } catch (IllegalArgumentException e) {
187: throw e;
188: } catch (Exception e) {
189: throw new IllegalArgumentException("error in news : "
190: + e.getMessage());
191: }
192: }
193:
194: public String getDescription() {
195: return "Display the first items of a timeline.";
196: }
197:
198: public String[] getParamDescription() {
199: return new String[] { "1. the timeline URI",
200: "2. Max items (optional)" };
201: }
202: }
|