1 package com.dotmarketing.portlets.indexation.factory; 2 3 import java.text.SimpleDateFormat ; 4 import java.util.ArrayList ; 5 import java.util.Date ; 6 import java.util.HashMap ; 7 import java.util.List ; 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 <Indexation> getNextIndexationList(Date finalDate,String serverId) 31 { 32 ArrayList <Indexation> returnValue; 33 SimpleDateFormat sdf = new SimpleDateFormat (); 34 String 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 <HashMap > list = dc.getResults(); 40 returnValue = new ArrayList <Indexation>(list.size()); 41 for(HashMap indexationHash : list) 42 { 43 Indexation indexation = new Indexation(); 44 long id = Long.parseLong((String ) indexationHash.get("indexation_id")); 45 long objectToIndex = Long.parseLong((String ) indexationHash.get("object_to_index")); 46 Date timestamp = new Date (); 47 try 48 { 49 timestamp = sdf.parse((String )indexationHash.get("time_entered")); 50 } 51 catch(Exception 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 <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 finalDate,String serverId) 86 { 87 DotConnect dc = new DotConnect(); 88 String 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 99 public static void reIndex() 100 { 101 String serverId = Config.getStringProperty("DIST_INDEXATION_SERVER_ID"); 102 Date finalDate = getDBDate(); 103 104 List <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(listIndexation.size() > 0) 125 { 126 IndexationFactory.deleteIndexation(finalDate,serverId); 127 } 128 } 129 130 131 134 public static Date getDBDate() 135 { 136 String db = DbConnectionFactory.getDBType(); 137 String query = new String (); 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 (); 146 DotConnect dc = new DotConnect(); 147 dc.setSQL(query); 148 List <HashMap > list = dc.getResults(); 149 SimpleDateFormat sdf = null; 150 if (db.equals("MySQL")) 151 sdf = new SimpleDateFormat ("yyyy-MM-dd KK:mm:ss"); 152 else 153 sdf = new SimpleDateFormat ("yyyy-MM-dd KK:mm:ss.SSS"); 154 Date date = new Date (); 155 try 156 { 157 String stringDate = (String ) 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 ex) 163 { 164 Logger.warn(IndexationFactory.class,ex.toString()); 165 } 166 return date; 167 } 168 169 } 170 | Popular Tags |