KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > datatypes > processors > CreationTimeGuesser


1 /*
2
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8
9 */

10 package org.mmbase.datatypes.processors;
11
12 import org.mmbase.bridge.*;
13 import org.mmbase.util.logging.*;
14 import java.util.Date JavaDoc;
15
16 /**
17  * Get-processor for 'created' field. Using day-markers to fill the field.
18  *
19  * @author Michiel Meeuwissen
20  * @version $Id: CreationTimeGuesser.java,v 1.3 2006/06/28 14:28:51 michiel Exp $
21  * @since MMBase-1.8
22  */

23
24 public class CreationTimeGuesser implements Processor {
25
26     private static final Logger log = Logging.getLoggerInstance(CreationTimeGuesser.class);
27
28     private static final long serialVersionUID = 1L;
29
30
31     public final Object JavaDoc process(Node node, Field field, Object JavaDoc value) {
32         Object JavaDoc v = node.getValueWithoutProcess(field.getName());
33         if (v == null) {
34             int age = node.getFunctionValue("age", null).toInt();
35             Date JavaDoc creationTime = new Date JavaDoc(System.currentTimeMillis() - 24L * 60 * 60 * 1000 * age);
36             // could try to save it here.
37
if (node.mayWrite() && ! field.isVirtual()) {
38                 boolean c = node.isChanged();
39                 node.setValueWithoutProcess(field.getName(), creationTime);
40                 if (! c) {
41                     node.commit();
42                 }
43             }
44             value =
45                 value == null ?
46                 creationTime :
47                 org.mmbase.util.Casting.toType(value.getClass(), node.getCloud(), creationTime); // return in similar type
48

49             log.debug("Guessing creation time : " + age + " days --> " + value);
50         }
51         return value;
52     }
53
54     public String JavaDoc toString() {
55         return "CreationTime";
56     }
57 }
58
59
60
Popular Tags