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 com.nokia.helium.metadata.CustomMetaDataProvider; |
20 import java.io.File; |
|
21 import java.util.Date; |
|
22 import java.util.Vector; |
|
23 |
|
24 import javax.persistence.EntityManagerFactory; |
|
25 |
|
26 import org.apache.tools.ant.BuildException; |
|
27 import org.apache.tools.ant.Project; |
|
28 import org.apache.tools.ant.Task; |
|
29 |
|
30 import com.nokia.helium.metadata.FactoryManager; |
21 import com.nokia.helium.metadata.MetaDataInput; |
31 import com.nokia.helium.metadata.MetaDataInput; |
22 import com.nokia.helium.jpa.entity.metadata.Metadata; |
32 import com.nokia.helium.metadata.MetadataException; |
23 import org.apache.tools.ant.BuildException; |
|
24 import org.apache.tools.ant.Task; |
|
25 import java.util.Vector; |
|
26 import java.util.Iterator; |
|
27 import org.apache.log4j.Logger; |
|
28 import com.nokia.helium.metadata.db.*; |
|
29 import java.util.Date; |
|
30 |
33 |
31 /** |
34 /** |
32 * This task provide a way to record the data in the Database. |
35 * This task provide a way to record the data in the Database. |
33 * |
36 * |
34 * <pre> |
37 * <pre> |
53 * |
56 * |
54 * @ant.task name="metadatarecord" category="Metadata" |
57 * @ant.task name="metadatarecord" category="Metadata" |
55 */ |
58 */ |
56 public class MetaDataRecordTask extends Task { |
59 public class MetaDataRecordTask extends Task { |
57 |
60 |
58 private static Logger log = Logger.getLogger(MetaDataRecordTask.class); |
61 private File database; |
59 |
|
60 private String database; |
|
61 |
62 |
62 private boolean failOnError = true; |
63 private boolean failOnError = true; |
63 |
64 |
64 private Vector<MetaDataInput> metadataList = new Vector<MetaDataInput>(); |
65 private Vector<MetaDataInput> metaDataInputs = new Vector<MetaDataInput>(); |
65 |
66 |
66 /** |
67 /** |
67 * Helper function to set the database parameter |
68 * Helper function to set the database parameter |
68 * |
69 * |
69 * @ant.required |
70 * @ant.required |
70 */ |
71 */ |
71 public void setDatabase(String dbFile) { |
72 public void setDatabase(File database) { |
72 database = dbFile; |
73 this.database = database; |
73 } |
74 } |
74 |
75 |
75 public void setFailOnError(String failNotify) { |
76 /** |
76 if (failNotify.equals("false")) { |
77 * Defines if the task should fail on error. |
77 failOnError = false; |
78 * @param failOnError |
78 } |
79 * @ant.not-required Default is true. |
|
80 */ |
|
81 public void setFailOnError(boolean failOnError) { |
|
82 this.failOnError = failOnError; |
79 } |
83 } |
|
84 |
80 /** |
85 /** |
81 * Helper function to get the database |
86 * Helper function to get the database |
82 * |
87 * |
83 */ |
88 */ |
84 public String getDatabase() { |
89 public File getDatabase() { |
85 return database; |
90 return database; |
86 } |
91 } |
87 |
92 |
88 /** |
93 /** |
89 * Helper function to return the metadatalist |
94 * Helper function to return the metadatalist |
90 * @return build metadata object |
95 * @return build metadata object |
91 * |
96 * |
92 */ |
97 */ |
93 public Vector<MetaDataInput> getMetaDataList() throws Exception { |
98 public Vector<MetaDataInput> getMetaDataList() throws MetadataException { |
94 if (metadataList.isEmpty()) { |
99 if (metaDataInputs.isEmpty()) { |
95 throw new Exception("metadata list is empty"); |
100 throw new MetadataException("metadata list is empty"); |
96 } |
101 } |
97 return metadataList; |
102 return metaDataInputs; |
98 } |
103 } |
99 |
104 |
100 /** |
105 /** |
101 * Helper function to add the metadatalist |
106 * Helper function to add the metadatalist |
102 * @param build metadata list to add |
107 * @param build metadata list to add |
103 * |
108 * |
104 */ |
109 */ |
105 public void add(MetaDataInput interf) { |
110 public void add(MetaDataInput metaDataInput) { |
106 metadataList.add(interf); |
111 metaDataInputs.add(metaDataInput); |
107 } |
112 } |
108 |
113 |
109 |
114 /** |
|
115 * {@inheritDoc} |
|
116 */ |
110 @Override |
117 @Override |
111 public void execute() { |
118 public void execute() { |
112 ORMMetadataDB ormDB = null; |
119 if (database == null) { |
|
120 throw new BuildException("'database' attribute is not defined."); |
|
121 } |
|
122 EntityManagerFactory factory = null; |
113 try { |
123 try { |
114 log.debug("Getting Contents to write to db: " + database); |
124 factory = FactoryManager.getFactoryManager().getEntityManagerFactory(database); |
115 log.debug("Initializing DB: " + database); |
|
116 log.debug("initializing ORM db"); |
|
117 ormDB = new ORMMetadataDB(database); |
|
118 log.debug("Parsing the input and writing to DB"); |
|
119 Date before = new Date(); |
125 Date before = new Date(); |
120 log("Time before recording to db: " + before); |
126 log("Time before recording to db: " + before); |
121 for (MetaDataInput metadataInput : metadataList) { |
127 for (MetaDataInput metadataInput : metaDataInputs) { |
122 boolean removed = false; |
128 metadataInput.extract(this, factory); |
123 String logPath = null; |
|
124 String currentLogPath = null; |
|
125 Iterator<Metadata.LogEntry> inputIterator = metadataInput.iterator(); |
|
126 while (inputIterator.hasNext()) { |
|
127 //Todo: better way of log handling, with metadatainput |
|
128 // metadata initialization for each logfile within |
|
129 //metadatainput itself would be better. this is temporary. |
|
130 Metadata.LogEntry logEntry = inputIterator.next(); |
|
131 logPath = logEntry.getLogPath(); |
|
132 if (currentLogPath == null) { |
|
133 currentLogPath = logPath; |
|
134 removed = false; |
|
135 } else if (!currentLogPath.equals(logPath)) { |
|
136 finalizeForLogPath(currentLogPath, metadataInput, ormDB); |
|
137 currentLogPath = logPath; |
|
138 removed = false; |
|
139 } |
|
140 if (!removed ) { |
|
141 log.debug("processing for log: " + logPath); |
|
142 ormDB.removeEntries(logPath); |
|
143 removed = true; |
|
144 } |
|
145 //initializes the metadata if none exists |
|
146 ormDB.addLogEntry(logEntry); |
|
147 } |
|
148 finalizeForLogPath(currentLogPath, metadataInput, ormDB); |
|
149 } |
129 } |
150 Date after = new Date(); |
130 Date after = new Date(); |
151 log("Time after recording to db: " + after); |
131 log("Time after recording to db: " + after); |
152 log("Elapsed time: " + (after.getTime() - before.getTime()) + " ms"); |
132 log("Elapsed time: " + (after.getTime() - before.getTime()) + " ms"); |
153 log.debug("Successfully writen to DB"); |
133 } catch (MetadataException ex) { |
154 } catch (BuildException ex1) { |
134 log(ex.getMessage(), Project.MSG_ERR); |
155 log.debug("BuildException during writing to db: ", ex1); |
|
156 if (failOnError) { |
135 if (failOnError) { |
157 throw ex1; |
136 throw new BuildException(ex.getMessage(), ex); |
158 } |
137 } |
159 } finally { |
138 } finally { |
160 if (ormDB != null) { |
139 if (factory != null) { |
161 ormDB.finalizeDB(); |
140 factory.close(); |
162 } |
141 } |
163 } |
142 factory = null; |
164 } |
|
165 |
|
166 private void finalizeForLogPath(String currentLogPath, |
|
167 MetaDataInput metadataInput, ORMMetadataDB ormDB) { |
|
168 if (currentLogPath != null) { |
|
169 if (metadataInput instanceof CustomMetaDataProvider) { |
|
170 CustomMetaDataProvider provider = |
|
171 (CustomMetaDataProvider)metadataInput; |
|
172 provider.provide(ormDB, currentLogPath); |
|
173 } |
|
174 ormDB.finalizeMetadata(currentLogPath); |
|
175 } |
143 } |
176 } |
144 } |
177 } |
145 } |