15 * |
15 * |
16 */ |
16 */ |
17 |
17 |
18 package com.nokia.helium.metadata.ant.taskdefs; |
18 package com.nokia.helium.metadata.ant.taskdefs; |
19 |
19 |
20 import org.apache.tools.ant.types.FileSet; |
20 import java.io.File; |
|
21 import java.util.Date; |
|
22 import java.util.Iterator; |
|
23 import java.util.List; |
|
24 import java.util.Vector; |
21 |
25 |
22 import org.apache.tools.ant.DirectoryScanner; |
26 import javax.persistence.EntityManager; |
|
27 import javax.persistence.EntityManagerFactory; |
|
28 |
|
29 import org.apache.tools.ant.BuildException; |
|
30 import org.apache.tools.ant.Project; |
23 import org.apache.tools.ant.Task; |
31 import org.apache.tools.ant.Task; |
24 import java.util.Vector; |
32 import org.apache.tools.ant.types.Resource; |
25 import java.util.ArrayList; |
33 import org.apache.tools.ant.types.ResourceCollection; |
26 import java.util.List; |
34 |
27 import org.apache.log4j.Logger; |
35 import com.nokia.helium.metadata.FactoryManager; |
28 import com.nokia.helium.metadata.db.*; |
36 import com.nokia.helium.metadata.MetadataException; |
|
37 import com.nokia.helium.metadata.model.metadata.LogFile; |
|
38 import com.nokia.helium.metadata.model.metadata.LogFileDAO; |
29 |
39 |
30 /** |
40 /** |
31 * This task provide a way to delete the data from db for a log file set. |
41 * This task provide a way to delete the data from db for a log file set. |
32 * |
42 * |
33 * <pre> |
43 * <pre> |
39 * |
49 * |
40 * @ant.task name="metadatadelete" category="Metadata" |
50 * @ant.task name="metadatadelete" category="Metadata" |
41 */ |
51 */ |
42 public class MetaDataDeleteTask extends Task { |
52 public class MetaDataDeleteTask extends Task { |
43 |
53 |
44 private static Logger log = Logger.getLogger(MetaDataDeleteTask.class); |
54 private File database; |
45 |
|
46 private String database; |
|
47 |
55 |
48 private boolean failOnError = true; |
56 private boolean failOnError = true; |
49 |
57 |
50 private Vector<FileSet> fileSetList = new Vector<FileSet>(); |
58 private List<ResourceCollection> resourceCollections = new Vector<ResourceCollection>(); |
51 |
59 |
52 /** |
60 /** |
53 * Helper function to set the database parameter |
61 * Helper function to set the database parameter |
54 * |
62 * |
55 * @ant.required |
63 * @ant.required |
56 */ |
64 */ |
57 public void setDatabase(String dbFile) { |
65 public void setDatabase(File database) { |
58 database = dbFile; |
66 this.database = database; |
59 } |
|
60 |
|
61 public void setFailOnError(String failNotify) { |
|
62 if (failNotify.equals("false")) { |
|
63 failOnError = false; |
|
64 } |
|
65 } |
67 } |
66 |
68 |
67 /** |
69 /** |
68 * Updates the list of filelist from the input fileset. |
70 * Defines if the task should fail on error. |
69 * @param fileSetList input fileset list |
71 * @param failNotify |
70 * @return the matched files including the base dir. |
72 * @ant.not-required Default is true. |
71 */ |
73 */ |
72 private List<String> getFileListFromFileSet() { |
74 public void setFailOnError(boolean failOnError) { |
73 List<String> fileList = new ArrayList<String>(); |
75 this.failOnError = failOnError; |
74 for (FileSet fs : fileSetList) { |
|
75 DirectoryScanner ds = fs.getDirectoryScanner(getProject()); |
|
76 String[] includedFiles = ds.getIncludedFiles(); |
|
77 for ( String file : includedFiles ) { |
|
78 fileList.add(file); |
|
79 log.debug("includedfiles: " + file); |
|
80 } |
|
81 } |
|
82 log.debug("fileList.size" + fileList.size()); |
|
83 return fileList; |
|
84 } |
76 } |
85 |
77 |
86 /** |
78 /** |
87 * Adds the fileset (list of input log files to be processed). |
79 * Adds any ResourceCollection types from Ant (list of input log files to be processed). |
88 * @param fileSet fileset to be added |
80 * @param resourceCollection the ResourceCollection to be added |
89 * |
81 * |
90 */ |
82 */ |
91 public void add(FileSet fileSet) { |
83 public void add(ResourceCollection resourceCollection) { |
92 fileSetList.add(fileSet); |
84 resourceCollections.add(resourceCollection); |
93 } |
85 } |
94 |
86 |
95 /** |
87 /** |
96 * Helper function to get the database |
88 * Helper function to get the database |
97 * |
89 * |
98 */ |
90 */ |
99 public String getDatabase() { |
91 protected File getDatabase() { |
100 return database; |
92 return database; |
101 } |
93 } |
102 |
94 |
103 |
95 /** |
|
96 * {@inheritDoc} |
|
97 */ |
|
98 @SuppressWarnings("unchecked") |
104 @Override |
99 @Override |
105 public void execute() { |
100 public void execute() { |
106 /* |
101 if (database == null) { |
107 MetaDataDb metadataDb = null; |
102 throw new BuildException("'database' attribute is not defined."); |
|
103 } |
|
104 EntityManagerFactory factory = null; |
|
105 EntityManager entityManager = null; |
108 try { |
106 try { |
109 log.debug("Initializing DB: " + database + "to delete"); |
107 factory = FactoryManager.getFactoryManager().getEntityManagerFactory(database); |
110 log("time before removing entries from db" + new Date()); |
108 entityManager = factory.createEntityManager(); |
111 metadataDb = new MetaDataDb(database); |
109 Date before = new Date(); |
112 metadataDb.removeEntries(getFileListFromFileSet()); |
110 log("Time before recording to db: " + before); |
113 log("time after removing entries from db" + new Date()); |
111 for (ResourceCollection resourceCollection : resourceCollections) { |
114 } catch (BuildException ex1) { |
112 Iterator<Resource> ri = (Iterator<Resource>)resourceCollection.iterator(); |
|
113 while (ri.hasNext()) { |
|
114 File file = new File(ri.next().toString()); |
|
115 LogFileDAO logFileDAO = new LogFileDAO(); |
|
116 logFileDAO.setEntityManager(entityManager); |
|
117 LogFile logFile = logFileDAO.findByLogName(file); |
|
118 if (logFile != null) { |
|
119 log("Removing log from database: " + file.getAbsolutePath()); |
|
120 entityManager.getTransaction().begin(); |
|
121 logFileDAO.remove(logFile); |
|
122 entityManager.getTransaction().commit(); |
|
123 } |
|
124 } |
|
125 } |
|
126 Date after = new Date(); |
|
127 log("Time after recording to db: " + after); |
|
128 log("Elapsed time: " + (after.getTime() - before.getTime()) + " ms"); |
|
129 } catch (MetadataException ex) { |
|
130 log(ex.getMessage(), Project.MSG_ERR); |
115 if (failOnError) { |
131 if (failOnError) { |
116 throw ex1; |
132 throw new BuildException(ex.getMessage(), ex); |
117 } |
|
118 } catch (Exception ex) { |
|
119 if (failOnError) { |
|
120 throw new BuildException("Failed during writing data to db"); |
|
121 } |
133 } |
122 } finally { |
134 } finally { |
123 log.debug("finalizing DB: " + database); |
135 if (entityManager != null) { |
124 if (metadataDb != null) { |
136 entityManager.close(); |
125 metadataDb.finalizeDB(); |
137 entityManager = null; |
|
138 } |
|
139 if (factory != null) { |
|
140 factory.close(); |
|
141 factory = null; |
126 } |
142 } |
127 } |
143 } |
128 */ |
|
129 } |
144 } |
130 } |
145 } |