KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > dotmarketing > portlets > indexation > factory > IndexationFactory


1 package com.dotmarketing.portlets.indexation.factory;
2
3 import java.text.SimpleDateFormat JavaDoc;
4 import java.util.ArrayList JavaDoc;
5 import java.util.Date JavaDoc;
6 import java.util.HashMap JavaDoc;
7 import java.util.List JavaDoc;
8
9 import com.dotmarketing.beans.Inode;
10 import com.dotmarketing.db.DbConnectionFactory;
11 import com.dotmarketing.db.DotConnect;
12 import com.dotmarketing.db.DotHibernate;
13 import com.dotmarketing.factories.InodeFactory;
14 import com.dotmarketing.portlets.contentlet.factories.ContentletFactory;
15 import com.dotmarketing.portlets.contentlet.model.Contentlet;
16 import com.dotmarketing.portlets.indexation.model.Indexation;
17 import com.dotmarketing.portlets.structure.model.Structure;
18 import com.dotmarketing.util.Config;
19 import com.dotmarketing.util.Logger;
20
21 public class IndexationFactory {
22
23     public static Indexation getIndexationById(long id)
24     {
25         Indexation indexation = null;
26         indexation = (Indexation) DotHibernate.load(Indexation.class, id);
27         return indexation;
28     }
29     
30     public static List JavaDoc<Indexation> getNextIndexationList(Date JavaDoc finalDate,String JavaDoc serverId)
31     {
32         ArrayList JavaDoc<Indexation> returnValue;
33         SimpleDateFormat JavaDoc sdf = new SimpleDateFormat JavaDoc();
34         String JavaDoc query = "select * from Indexation where time_entered <= ? and server_id = ?";
35         DotConnect dc = new DotConnect();
36         dc.setSQL(query);
37         dc.addParam(finalDate);
38         dc.addParam(serverId);
39         List JavaDoc<HashMap JavaDoc> list = dc.getResults();
40         returnValue = new ArrayList JavaDoc<Indexation>(list.size());
41         for(HashMap JavaDoc indexationHash : list)
42         {
43             Indexation indexation = new Indexation();
44             long id = Long.parseLong((String JavaDoc) indexationHash.get("indexation_id"));
45             long objectToIndex = Long.parseLong((String JavaDoc) indexationHash.get("object_to_index"));
46             Date JavaDoc timestamp = new Date JavaDoc();
47             try
48             {
49                 timestamp = sdf.parse((String JavaDoc)indexationHash.get("time_entered"));
50             }
51             catch(Exception JavaDoc ex)
52             {}
53             
54             if(!contains(returnValue,objectToIndex))
55             {
56                 indexation.setIndexationId(id);
57                 indexation.setObjectToIndex(objectToIndex);
58                 indexation.setTimestamp(timestamp);
59                 returnValue.add(indexation);
60             }
61         }
62         return returnValue;
63     }
64     
65     private static boolean contains(ArrayList JavaDoc<Indexation> IndexationArray,long inodeToIndex)
66     {
67         boolean returnValue = false;
68         for(Indexation indexationAux : IndexationArray)
69         {
70             long indexationAuxToIndex = indexationAux.getObjectToIndex();
71             if(indexationAuxToIndex == inodeToIndex)
72             {
73                 returnValue = true;
74                 break;
75             }
76         }
77         return returnValue;
78     }
79     
80     public static void saveIndexation(Indexation indexation)
81     {
82         DotHibernate.saveOrUpdate(indexation);
83     }
84     
85     public static void deleteIndexation(Date JavaDoc finalDate,String JavaDoc serverId)
86     {
87         DotConnect dc = new DotConnect();
88         String JavaDoc query = "delete from Indexation where time_entered <= ? and server_id = ?";
89         dc.setSQL(query);
90         dc.addParam(finalDate);
91         dc.addParam(serverId);
92         dc.getResult();
93     }
94     
95     /**
96      * This method checks for the necessary new/updated objects to be reindexed, and triggers the reindexation for this objects.
97      *
98      */

99     public static void reIndex()
100     {
101         String JavaDoc serverId = Config.getStringProperty("DIST_INDEXATION_SERVER_ID");
102         Date JavaDoc finalDate = getDBDate();
103         
104         List JavaDoc<Indexation> listIndexation = IndexationFactory.getNextIndexationList(finalDate,serverId);
105
106         for(Indexation indexation : listIndexation)
107         {
108             long objectToIndexInode = indexation.getObjectToIndex();
109             Inode inode = InodeFactory.getInode(objectToIndexInode,Inode.class);
110             if(inode instanceof Structure)
111             {
112                 ContentletFactory.reIndexAllContentletsByStructure((Structure) inode,false);
113             }
114             else if(inode instanceof Contentlet)
115             {
116                 ContentletFactory.reIndexContentlet((Contentlet) inode,false);
117             }
118             else if (objectToIndexInode == 0)
119             {
120                 ContentletFactory.reIndexAllContentlets(false);
121             }
122         }
123         //If there were entries in the INDEXATION, then I will delete those entries
124
if(listIndexation.size() > 0)
125         {
126             IndexationFactory.deleteIndexation(finalDate,serverId);
127         }
128     }
129     
130     
131     /*
132      *This method have to be change id the DB is changed
133      */

134     public static Date JavaDoc getDBDate()
135     {
136         String JavaDoc db = DbConnectionFactory.getDBType();
137         String JavaDoc query = new String JavaDoc ();
138         if (db.equals("Oracle"))
139             query = "select sysdate as today from dual";
140         else if (db.equals("MySQL"))
141             query = "select CURRENT_TIMESTAMP() as today";
142         else if (db.equals("PostgreSQL"))
143             query = "select CURRENT_TIMESTAMP as today";
144         else
145             return new Date JavaDoc ();
146         DotConnect dc = new DotConnect();
147         dc.setSQL(query);
148         List JavaDoc<HashMap JavaDoc> list = dc.getResults();
149         SimpleDateFormat JavaDoc sdf = null;
150         if (db.equals("MySQL"))
151             sdf = new SimpleDateFormat JavaDoc("yyyy-MM-dd KK:mm:ss");
152         else
153             sdf = new SimpleDateFormat JavaDoc("yyyy-MM-dd KK:mm:ss.SSS");
154         Date JavaDoc date = new Date JavaDoc();
155         try
156         {
157             String JavaDoc stringDate = (String JavaDoc) list.get(0).get("today");
158             if (db.equals("PostgreSQL"))
159                 stringDate = stringDate.substring(0,stringDate.lastIndexOf("-"));
160             date = sdf.parse(stringDate);
161         }
162         catch(Exception JavaDoc ex)
163         {
164             Logger.warn(IndexationFactory.class,ex.toString());
165         }
166         return date;
167     }
168     
169 }
170
Popular Tags