buildframework/helium/sf/java/jpa/src/com/nokia/helium/jpa/entity/metadata/Metadata.java
changeset 628 7c4a911dc066
parent 588 c7c26511138f
child 629 541af5ee3ed9
equal deleted inserted replaced
588:c7c26511138f 628:7c4a911dc066
     1 /*
       
     2  * Copyright (c) 2007-2008 Nokia Corporation and/or its subsidiary(-ies).
       
     3  * All rights reserved.
       
     4  * This component and the accompanying materials are made available
       
     5  * under the terms of the License "Eclipse Public License v1.0"
       
     6  * which accompanies this distribution, and is available
       
     7  * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8  *
       
     9  * Initial Contributors:
       
    10  * Nokia Corporation - initial contribution.
       
    11  *
       
    12  * Contributors:
       
    13  *
       
    14  * Description:  
       
    15  *
       
    16  */
       
    17 
       
    18 package com.nokia.helium.jpa.entity.metadata;
       
    19 
       
    20 import java.util.ArrayList;
       
    21 import javax.persistence.FlushModeType;
       
    22 import org.apache.log4j.Logger;
       
    23 import java.util.List;
       
    24 import javax.persistence.MapKeyColumn;
       
    25 import javax.persistence.Column;
       
    26 import javax.persistence.Basic;
       
    27 import javax.persistence.FetchType;
       
    28 import javax.persistence.Entity;
       
    29 import javax.persistence.GeneratedValue;
       
    30 import javax.persistence.GenerationType;
       
    31 import javax.persistence.Id;
       
    32 import javax.persistence.OneToMany;
       
    33 import javax.persistence.EntityManager;
       
    34 import java.util.Hashtable;
       
    35 import javax.persistence.Query;
       
    36 import javax.persistence.CascadeType;
       
    37 import com.nokia.helium.jpa.ORMCommitCount;
       
    38 import com.nokia.helium.jpa.ORMUtil;
       
    39 import com.nokia.helium.jpa.ORMEntityManager;
       
    40 
       
    41 /**
       
    42  * Interface Class to store all the metadata information.
       
    43  */
       
    44 
       
    45 @Entity
       
    46 public class Metadata {
       
    47 
       
    48     private static Logger log = Logger.getLogger(Metadata.class);
       
    49     
       
    50     private static String[] priorityNames = {"FATAL", "ERROR", "WARNING", "INFO",
       
    51         "REAMARK", "CRITICAL", "DEFAULT"};
       
    52 
       
    53     private transient ORMEntityManager manager;
       
    54 
       
    55     private transient String logPath;
       
    56 
       
    57     @Basic
       
    58     @Column(unique = true, nullable = false)
       
    59     private String name;
       
    60 
       
    61 
       
    62     @Id
       
    63     @GeneratedValue(strategy = GenerationType.SEQUENCE)
       
    64     private int id;
       
    65 
       
    66     @OneToMany(mappedBy = "metadata",
       
    67             fetch = FetchType.LAZY, cascade = CascadeType.ALL)
       
    68     private Hashtable<String, LogFile> logFiles;
       
    69 
       
    70     @OneToMany(mappedBy = "metadata",
       
    71             fetch = FetchType.LAZY, cascade = CascadeType.ALL)
       
    72     @MapKeyColumn(name = "PRIORITY", insertable = false, updatable = false)
       
    73     private Hashtable<String, Priority> priorities;
       
    74 
       
    75 
       
    76     private Hashtable<String, Component> components;
       
    77  
       
    78     private List<MetadataEntry> entries;
       
    79 
       
    80     /**
       
    81      * Default Constructor.
       
    82      */
       
    83     public Metadata() {
       
    84     }
       
    85 
       
    86     /** Constructor.
       
    87      * @param nm - name to be looked for in the persistence.xml file
       
    88      * for database information.
       
    89      */
       
    90     public Metadata(String nm) {
       
    91         name = nm;
       
    92     }
       
    93 
       
    94     /** Constructor.
       
    95      *  @param mgr - entity manager using which the data to be
       
    96      *  written to database.
       
    97      *  @param path - log path for which data to be written to.
       
    98      */
       
    99     public Metadata(ORMEntityManager mgr, String path) {
       
   100         manager = mgr;
       
   101         if (logPath == null) {
       
   102             logPath = path.replace('\'', '/');
       
   103             initializeLogPath();
       
   104         }
       
   105     }
       
   106 
       
   107     /**
       
   108      * Helper function to set the name of the JPA entity manager to
       
   109      * be used.
       
   110      * @param nm - name to be looked for in the persistence.xml file
       
   111      * for database information.
       
   112      */
       
   113     public void setName(String nm) {
       
   114         name = nm;
       
   115     }
       
   116 
       
   117     /**
       
   118      * Helper function to get the name of the JPA entity manager to
       
   119      * being used.
       
   120      * @return nm - name to be looked for in the persistence.xml file
       
   121      * for database information.
       
   122      */
       
   123     public String getName() {
       
   124         return name;
       
   125     }
       
   126 
       
   127 
       
   128     /**
       
   129      * Helper function to return the list of Logfiles stored
       
   130      * in the database.
       
   131      * @return Hash table of logpath with logfile object
       
   132      * stored in the database.
       
   133      */
       
   134     public Hashtable<String, LogFile> getLogFiles() {
       
   135         return logFiles;
       
   136     }
       
   137 
       
   138     /**
       
   139      * Helper function to persist the components,
       
   140      * in the database.
       
   141      * @param componentsList of component name with component object.
       
   142      * stored in the database.
       
   143      */
       
   144     public void setComponents(Hashtable<String, Component> componentsList) {
       
   145         components = componentsList;
       
   146     }
       
   147 
       
   148     /**
       
   149      * Helper function to persist logfile objects in the database.
       
   150      * @param logFilesList logpath with logfile object.
       
   151      */
       
   152     public void setLogFiles(Hashtable<String, LogFile> logFilesList) {
       
   153         logFiles = logFilesList;
       
   154     }
       
   155 
       
   156     public String getLogPath() {
       
   157         return logPath;
       
   158     }
       
   159     /**
       
   160      * Helper function to persist metadata entry lists in the db.
       
   161      * @param entriesList List of entries to persist.
       
   162      */
       
   163     public void setEntries(List<MetadataEntry> entriesList) {
       
   164         entries = entriesList;
       
   165     }
       
   166 
       
   167     /**
       
   168      * Helper function to return the list of persisted entries currently
       
   169      * in memory.
       
   170      * @return list of metadata entries currently in memory.
       
   171      */
       
   172     public List<MetadataEntry> getEntries() {
       
   173         return entries;
       
   174     }
       
   175 
       
   176     /**
       
   177      * Helper function to set the logpath of the current entry.
       
   178      * @param path logpath associated with this entry.
       
   179      */
       
   180     public void setLogPath(String path) {
       
   181         logPath = path;
       
   182     }
       
   183 
       
   184     /**
       
   185      * Helper function to set the priority list.
       
   186      * @param prtList.
       
   187      */
       
   188     public void setPriorities(Hashtable<String, Priority> prtList) {
       
   189         priorities = prtList;
       
   190     }
       
   191 
       
   192     /**
       
   193      * Helper function to initialize the components currently in
       
   194      * the database. This is cached in memory so that before persisting
       
   195      * the cached info is checked for performance optmization.
       
   196      * @param path - path associated with the component.
       
   197      * @param logFile - logfile object associated with the component.
       
   198      */
       
   199     @SuppressWarnings("unchecked")
       
   200     public void initComponents(String path, LogFile logFile) {
       
   201         logPath = path;
       
   202         components = new Hashtable<String, Component>();
       
   203         if (logFile != null) {
       
   204             //System.out.println("logfile id: " + logFile.getId());
       
   205             List<Component> componentList =
       
   206                 (List<Component>) manager.getEntityManager().createQuery(
       
   207                     "SELECT c FROM Component c WHERE c.logPathID = :pathId")
       
   208             .setParameter("pathId", logFile.getId()).getResultList();
       
   209             for (Component comp : componentList) {
       
   210                 components.put(comp.getComponent(), comp);
       
   211             }
       
   212         }
       
   213     }
       
   214 
       
   215     /**
       
   216      * Initialize the priority table, caches in memory for
       
   217      * performance.
       
   218      */
       
   219     @SuppressWarnings("unchecked")
       
   220     public void initPriorities() {
       
   221         priorities = new Hashtable<String, Priority>();
       
   222         List<Priority> priorityList =
       
   223             (List<Priority>) manager.getEntityManager().createQuery(
       
   224                 "SELECT p FROM Priority p")
       
   225             .getResultList();
       
   226         for (Priority priority : priorityList) {
       
   227             //System.out.println("priority value: " + priority.getPriority());
       
   228             priorities.put(priority.getPriority(), priority);
       
   229         }
       
   230     }
       
   231 
       
   232     /**
       
   233      * Load the required data to be cached from database.
       
   234      * @param path - db path.
       
   235      */
       
   236     @SuppressWarnings("unchecked")
       
   237     private void loadFromDB(String path) {
       
   238         LogFile logFile = null;
       
   239         logFiles = new Hashtable<String, LogFile>();
       
   240         Query query = manager.getEntityManager().createQuery("SELECT l FROM LogFile l");
       
   241         query.setFlushMode(FlushModeType.COMMIT);
       
   242         List<LogFile> logFilesList =
       
   243             (List<LogFile>) query.getResultList();
       
   244         for (LogFile file : logFilesList) {
       
   245             log.debug("getting logfile from db: " + file.getPath());
       
   246             logFiles.put(file.getPath(), file);
       
   247         }
       
   248         //manager.getEntityManager().clear();
       
   249         logFile = logFiles.get(path);
       
   250         if (logFile == null) {
       
   251             populateDB(path);
       
   252         } else {
       
   253             initComponents(path, logFile);
       
   254             List<MetadataEntry> entriesList = new ArrayList<MetadataEntry>();
       
   255             setEntries(entriesList);
       
   256         }
       
   257         initPriorities();
       
   258     }
       
   259 
       
   260     /**
       
   261      * Internal function to persist an object into the database.
       
   262      * @param obj - object to be stored in the data.
       
   263      */
       
   264     private void persist(Object obj) {
       
   265         Object mutexObject = ORMUtil.getMutexObject();
       
   266         synchronized (mutexObject) {
       
   267             synchronized (manager) {
       
   268                 EntityManager em = manager.getEntityManager();
       
   269                 ORMCommitCount countObject = manager.getCommitCountObject();
       
   270                 //log.debug("object: " + em);
       
   271                 em.persist(obj);
       
   272                 countObject.decreaseCount();
       
   273                 if (countObject.isCommitRequired()) {
       
   274                     countObject.reset();
       
   275                     em.getTransaction().commit();
       
   276                     em.clear();
       
   277                     em.getTransaction().begin();
       
   278                 }
       
   279             }
       
   280         }
       
   281     }
       
   282 
       
   283     /**
       
   284      * Internal function to populate the priorities into the cache.
       
   285      */
       
   286     private void populatePriorities() {
       
   287         Hashtable<String, Priority> priorityList = new Hashtable<String, Priority>(); 
       
   288         setPriorities(priorityList);
       
   289         for (String priorityName : priorityNames) {
       
   290             Priority priority = new Priority();
       
   291             priority.setPriority(priorityName);
       
   292             priorityList.put(priorityName, priority);
       
   293             log.debug("populating priorities: " + priorityName);
       
   294             persist(priority);
       
   295         }
       
   296     }
       
   297 
       
   298     /**
       
   299      * Internal function to load data from database.
       
   300      * @param path - db path
       
   301      */
       
   302     private void populateDB(String path) {
       
   303         setLogPath(path);
       
   304         Hashtable<String, LogFile> logFileList =
       
   305             new Hashtable<String, LogFile>();
       
   306         //entityManager.merge(logFileList);
       
   307         setLogFiles(logFileList);
       
   308         log.debug("populatedb: " + path);
       
   309         checkAndAddLogPath();
       
   310         Hashtable<String, Component> componentsList =
       
   311             new Hashtable<String, Component>();
       
   312         //entityManager.merge(componentsList);
       
   313         setComponents(componentsList);
       
   314         List<MetadataEntry> entriesList = new ArrayList<MetadataEntry>();
       
   315         setEntries(entriesList);
       
   316     }
       
   317 
       
   318     /**
       
   319      * Internal function to cache the logpath for performance.
       
   320      */
       
   321     private void initializeLogPath() {
       
   322         EntityManager entityManager = manager.getEntityManager();
       
   323         Query query = entityManager.createQuery("select m from LogFile m");
       
   324         query.setFlushMode(FlushModeType.COMMIT);
       
   325         
       
   326         name = "metadata";
       
   327         if (query.getResultList().size() == 0) {
       
   328             log.debug("query result: size" + query.getResultList().size());
       
   329             populatePriorities();
       
   330             populateDB(logPath);
       
   331         } else {
       
   332             log.debug("loading from db: " + logPath);
       
   333             loadFromDB(logPath);
       
   334         }
       
   335         //em.clear();
       
   336     }
       
   337 
       
   338     /**
       
   339      * Adds the log entry to the database.
       
   340      * @param entry - logentry which is to be written to db.
       
   341      */
       
   342     public void addEntry(LogEntry entry) {
       
   343 
       
   344         String comp = entry.getComponent();
       
   345         Component component = null;
       
   346         if (comp != null) {
       
   347             component = checkAndAddComponent(comp);
       
   348             float elapsedTime = entry.getElapsedTime();
       
   349             String priority = entry.getPriorityText();
       
   350             int lineNo = entry.getLineNumber();
       
   351             String logText = entry.getText();
       
   352             log.debug("elapsedTime: " + elapsedTime);
       
   353             log.debug("comp: " + comp);
       
   354             if (elapsedTime != -1) {
       
   355                 addElapsedTime(component, elapsedTime);
       
   356             } else if (!priority.equals("default")) {
       
   357                 addMetadata(priority, component, lineNo, logText);
       
   358             } else {
       
   359                 WhatEntry whatEntry = entry.getWhatEntry();
       
   360                 if (whatEntry != null) {
       
   361                     List<WhatLogMember> members = whatEntry.getMembers(); 
       
   362                     if (members != null) {
       
   363                         for (WhatLogMember member : members) {
       
   364                             if (member != null) {
       
   365                                 addWhatLogMember(component, member.getMember(),
       
   366                                         member.getMemberExists());
       
   367                             }
       
   368                         }
       
   369                     }
       
   370                 }
       
   371             }
       
   372         }
       
   373     }
       
   374 
       
   375     
       
   376     /**
       
   377      * Internal function to add what log member to the database.
       
   378      * @param component for which to record the elapsed time
       
   379      * @param whatLogMember - an entry from whatlog output
       
   380      * @param memberExists - is the whatlog entry exists in the file system.
       
   381      */
       
   382     private void addWhatLogMember(Component component,
       
   383             String whatLogMember, boolean memberExists) {
       
   384         //Todo: handle duplicate whatlog member entry
       
   385         WhatLogEntry entry = new WhatLogEntry();
       
   386         entry.setMember(whatLogMember);
       
   387         entry.setMissing(!memberExists);
       
   388         entry.setComponent(component);
       
   389         persist(entry);
       
   390     }
       
   391 
       
   392     /**
       
   393      * Internal function to add log path only if it is not already
       
   394      * there in the cached entry from database.
       
   395      * @return LogFile object related to the logpath.
       
   396      */
       
   397     private LogFile checkAndAddLogPath() {
       
   398         LogFile logFile = logFiles.get(logPath);
       
   399         log.debug("checkandaddlogpath:logpath" + logPath);
       
   400         log.debug("checkandaddlogpath:logFile" + logFile);
       
   401         if (logFile == null) {
       
   402             logFile = new LogFile();
       
   403             logFile.setPath(logPath);
       
   404             persist(logFile);
       
   405             logFiles.put(logPath, logFile);
       
   406         }
       
   407         return logFile;
       
   408     }
       
   409 
       
   410     /**
       
   411      * Record the execution time in second for a particular log.
       
   412      * @param time
       
   413      */
       
   414     public void addExecutionTime(int time) {
       
   415         LogFile logFile = logFiles.get(this.getLogPath());
       
   416         if (logFile != null) {
       
   417             ExecutionTime et = new ExecutionTime();
       
   418             et.setTime(time);
       
   419             et.setLogFile(logFile);
       
   420             persist(et);
       
   421         }
       
   422     }
       
   423     
       
   424     /**
       
   425      * Internal function to add elapsed tim to the database.
       
   426      * @param component for which to record the elapsed time
       
   427      * @param elapsedTime - time to be recorded.
       
   428      */
       
   429     private void addElapsedTime(Component component, double elapsedTime) {
       
   430         ComponentTime componentTime = new ComponentTime();
       
   431         componentTime.setComponent(component);
       
   432         componentTime.setDuration(elapsedTime);
       
   433         persist(componentTime);
       
   434     }
       
   435 
       
   436     /**
       
   437      * Internal function to add component if not exist
       
   438      * in the cached entries from database.
       
   439      * @param comp - component Name to be added.
       
   440      * @return either new component / existing component from cache.
       
   441      */
       
   442     private Component checkAndAddComponent(String comp) {
       
   443         //System.out.println("checkAndAddComponent: comp: "+ comp);
       
   444         Component component = components.get(comp);
       
   445         if (component == null) {
       
   446             component = new Component();
       
   447             log.debug("checkandaddcomponent: logpath" + logPath);
       
   448             log.debug("checkandaddcomponent: logpath" + logFiles);
       
   449             component.setLogFile(logFiles.get(logPath));
       
   450             component.setComponent(comp);
       
   451             persist(component);
       
   452             components.put(comp, component);
       
   453         }
       
   454         return component;
       
   455     }
       
   456 
       
   457     /**
       
   458      * Function provides the Priority object for the string
       
   459      * priority.
       
   460      * @param prty - priority string
       
   461      * @return Priority object for the prioirty string.
       
   462      */
       
   463     private Priority getPriority(String prty) {
       
   464         Priority retValue = priorities.get(prty.toUpperCase());
       
   465         if (retValue == null) {
       
   466             retValue = priorities.get(priorityNames[0]);
       
   467         }
       
   468         log.debug("priority:getPriority: " + prty);
       
   469         return retValue;
       
   470     }
       
   471 
       
   472     /**
       
   473      * Internal function to add the entry to the database.
       
   474      * priority.
       
   475      * @param priority - Priority of the entry
       
   476      * @param component - component info for the entry
       
   477      * @param lineNo - line number at which the severity is captured.
       
   478      * @param logText - text about the severity info to be recorded.
       
   479      */
       
   480     private void addMetadata(String priority, Component component,
       
   481             int lineNo, String logText) {
       
   482         MetadataEntry entry = new MetadataEntry();
       
   483         log.debug("logfile: " + component.getLogFile().getPath());
       
   484         entry.setLogFile(component.getLogFile());
       
   485         entry.setComponent(component);
       
   486         entry.setLineNumber(lineNo);
       
   487         entry.setPriority(getPriority(priority));
       
   488         log.debug("error text message: " + logText);
       
   489         entry.setText(logText);
       
   490         persist(entry);
       
   491     }
       
   492 
       
   493     /**
       
   494      * Removes the entries for a particular log.
       
   495      * priority.
       
   496      */
       
   497     public final void removeEntries() {
       
   498         LogFile file = (LogFile)executeSingleQuery("select l from LogFile l where l.path like '%" + logPath + "'");
       
   499         if ( file != null ) {
       
   500             log.debug("removing entries for : " + file.getPath());
       
   501             int pathId = file.getId();
       
   502             removeEntries("DELETE FROM MetadataEntry AS m where m.COMPONENT_ID in (select COMPONENT_ID from Component where LOGPATH_ID= " + pathId + ")");
       
   503             removeEntries("DELETE FROM ComponentTime AS ctime where ctime.COMPONENT_ID in (select COMPONENT_ID from Component where LOGPATH_ID= " + pathId + ")");
       
   504             removeEntries("DELETE FROM WhatLogEntry AS wentry where wentry.COMPONENT_ID in (select COMPONENT_ID from Component where LOGPATH_ID= " + pathId + ")");
       
   505             removeEntries("DELETE FROM Component AS c where c.LOGPATH_ID = " + pathId);
       
   506             removeEntries("DELETE FROM ExecutionTime AS e where e.LOGPATH_ID = " + pathId);
       
   507             removeEntries("DELETE FROM LogFile AS l where l.LOGPATH_ID = " + pathId);
       
   508             removeEntries("DELETE FROM LogFile l where l.LOGPATH_ID = " + pathId);
       
   509         }
       
   510     }
       
   511 
       
   512     /**
       
   513      * Internal function execute query which results in single record.
       
   514      * @param queryString - query string for whcih the result to be obtained.
       
   515      * @return object - record from the executed query.
       
   516      */
       
   517     private Object executeSingleQuery (String queryString) {
       
   518         Object mutexObject = ORMUtil.getMutexObject();
       
   519         Object obj = null;
       
   520         synchronized (mutexObject) {
       
   521             EntityManager em = manager.getEntityManager();
       
   522             Query query = em.createQuery(queryString);
       
   523             query.setHint("eclipselink.persistence-context.reference-mode", "WEAK");
       
   524             query.setHint("eclipselink.maintain-cache", "false");
       
   525             query.setHint("eclipselink.read-only", "true");
       
   526             query.setFlushMode(FlushModeType.COMMIT);
       
   527             try {
       
   528                 obj = query.getSingleResult();
       
   529             } catch (javax.persistence.NoResultException nex) {
       
   530                 log.debug("no results for query: " + queryString, nex);
       
   531             } catch (javax.persistence.NonUniqueResultException nux) {
       
   532                 log.debug("more than one result returned by query: " + queryString, nux);
       
   533             }
       
   534         }
       
   535         return obj;
       
   536     }
       
   537 
       
   538     /**
       
   539      * Internal function to remove the entries from db.
       
   540      * @param queryString - query string for whcih the result to be obtained.
       
   541      */
       
   542     private void removeEntries(String queryString) {
       
   543         Object mutexObject = ORMUtil.getMutexObject();
       
   544         synchronized (mutexObject) {
       
   545             EntityManager em = manager.getEntityManager();
       
   546             Query query = em.createNativeQuery(queryString);
       
   547             query.setHint("eclipselink.persistence-context.reference-mode", "WEAK");
       
   548             query.setHint("eclipselink.maintain-cache", "false");
       
   549             query.setFlushMode(FlushModeType.COMMIT);
       
   550             try {
       
   551                 int deletedRecords = query.executeUpdate();
       
   552                 log.debug("total records deleted " + deletedRecords 
       
   553                         + "for query:" + queryString);
       
   554             } catch (javax.persistence.NoResultException nex) {
       
   555                 log.debug("no results:", nex);
       
   556             } catch (javax.persistence.NonUniqueResultException nux) {
       
   557                 log.debug("more than one result returned:", nux);
       
   558             }
       
   559         }
       
   560     }
       
   561 
       
   562     /**
       
   563      * Helper class to store the log entry , used to write to the database
       
   564      * 
       
   565      * @param databasePath The path to the database
       
   566      */
       
   567     public static class LogEntry
       
   568     {
       
   569         private String text;
       
   570 
       
   571         private PriorityEnum priority;
       
   572 
       
   573         private String component;
       
   574         
       
   575         private int lineNumber;
       
   576         
       
   577         private String logPath;
       
   578         
       
   579         private float elapsedTime;
       
   580         
       
   581         private String priorityText;
       
   582         
       
   583         private WhatEntry whatEntry;
       
   584 
       
   585     /**
       
   586      * Constructor for the helper class 
       
   587      */
       
   588         public LogEntry(String text, PriorityEnum priority, String component, 
       
   589                 String logPath, int lineNumber, 
       
   590                 float time, WhatEntry entry)
       
   591         {
       
   592             this.text = text;
       
   593             this.priority = priority;
       
   594             this.component = component;
       
   595             this.lineNumber = lineNumber;
       
   596             this.logPath = logPath.replace('\'', '/');
       
   597             this.elapsedTime = time;
       
   598             whatEntry = entry;
       
   599         }
       
   600 
       
   601     /**
       
   602      * Constructor for the helper class 
       
   603      */
       
   604         public LogEntry(String text, PriorityEnum priority, String component, 
       
   605                 String logPath, int lineNumber)
       
   606         {
       
   607             this(text, priority, component, logPath, lineNumber, -1, null);
       
   608         }
       
   609 
       
   610 
       
   611     /**
       
   612      * Constructor for the helper class.
       
   613      */
       
   614         public LogEntry(String text, String priorityTxt, String component, String logPath, 
       
   615                 int lineNumber, float time, WhatEntry entry) {
       
   616             PriorityEnum prty = null;
       
   617             String prtyText = priorityTxt.trim().toLowerCase();
       
   618             priorityText =  prtyText;
       
   619             if (prtyText.equals("error")) {
       
   620                 prty = PriorityEnum.ERROR;
       
   621             } else if (prtyText.equals("warning")) {
       
   622                 prty = PriorityEnum.WARNING;
       
   623             } else if (prtyText.equals("fatal")) {
       
   624                 prty = PriorityEnum.FATAL;
       
   625             } else if (prtyText.equals("info")) {
       
   626                 prty = PriorityEnum.INFO;
       
   627             } else if (prtyText.equals("remark")) {
       
   628                 prty = PriorityEnum.REMARK;
       
   629             } else if (prtyText.equals("default")) {
       
   630                 prty = PriorityEnum.DEFAULT;
       
   631             } else if (prtyText.equals("critical")) {
       
   632                 prty = PriorityEnum.CRITICAL;
       
   633             } else {
       
   634                 log.debug("Error: priority " + prtyText + " is not acceptable by metadata and set to Error");
       
   635                 prty = PriorityEnum.ERROR;
       
   636                 priorityText =  "error";
       
   637                 //throw new Exception("priority should not be null");
       
   638             }
       
   639 
       
   640             this.logPath = logPath.replace('\\', '/');
       
   641             this.text = text;
       
   642             priority = prty;
       
   643             this.component = component;
       
   644             this.lineNumber = lineNumber;
       
   645             this.elapsedTime = time;
       
   646             whatEntry = entry;
       
   647         }
       
   648 
       
   649         public void setElapsedTime(float time) {
       
   650             this.elapsedTime = time;
       
   651         }
       
   652     /**
       
   653      * Helper function to return to getLogPath
       
   654      * @
       
   655      */
       
   656 
       
   657         public String getLogPath()
       
   658         {
       
   659             return logPath;
       
   660         }
       
   661         
       
   662         public int getLineNumber()
       
   663         {
       
   664             return lineNumber;
       
   665         }
       
   666         
       
   667         public String getText()
       
   668         {
       
   669             return text;
       
   670         }
       
   671 
       
   672         public void setText(String text)
       
   673         {
       
   674             this.text = text;
       
   675         }
       
   676 
       
   677         public PriorityEnum getPriority()
       
   678         {
       
   679             return priority;
       
   680         }
       
   681 
       
   682         public String getPriorityText() {
       
   683             return priorityText;
       
   684         }
       
   685         
       
   686         public float getElapsedTime() {
       
   687             return elapsedTime;
       
   688         }
       
   689 
       
   690         public void setPriority(PriorityEnum priority)
       
   691         {
       
   692             this.priority = priority;
       
   693         }
       
   694 
       
   695         public String getComponent()
       
   696         {
       
   697             return component;
       
   698         }
       
   699 
       
   700         public void setComponent(String component)
       
   701         {
       
   702             this.component = component;
       
   703         }
       
   704         
       
   705         public WhatEntry getWhatEntry() {
       
   706             return whatEntry;
       
   707         }
       
   708         
       
   709         public String toString() {
       
   710             return "file:" + logPath + "[" + lineNumber + "], component:" + component + ", priority:" +
       
   711                    priorityText + " \n" + text;
       
   712         }
       
   713     }
       
   714 
       
   715     /** Levels of log entry types. */
       
   716     public enum PriorityEnum
       
   717     {
       
   718         // The values assigned to these enums should match the 
       
   719         // automatically assigned values created in the database table
       
   720         FATAL(1), ERROR(2), WARNING(3), INFO(4), REMARK(5), DEFAULT(6), CRITICAL(7);
       
   721         private final int value;
       
   722         PriorityEnum(int value)
       
   723         {
       
   724             this.value = value;
       
   725         }
       
   726         public int getValue() {
       
   727             return value;
       
   728         }
       
   729     
       
   730         public  static PriorityEnum getPriorityEnum( int i ) {
       
   731             final PriorityEnum[] values  = values();
       
   732             return i >= 0 && i < values .length ? values[i] : FATAL;
       
   733         }
       
   734     };
       
   735 
       
   736     /**
       
   737      * Helper class to store the Component and output association.
       
   738      */
       
   739     public static class WhatEntry
       
   740     {
       
   741         private String component;
       
   742         private List<WhatLogMember> members;
       
   743 
       
   744     /**
       
   745      * Constructor for the helper class 
       
   746      */
       
   747         public WhatEntry(String comp, List<WhatLogMember> mbrs)
       
   748         {
       
   749             component = comp;
       
   750             members = mbrs;
       
   751         }
       
   752         
       
   753         public String getComponent() {
       
   754             return component;
       
   755         }
       
   756 
       
   757         public List<WhatLogMember> getMembers() {
       
   758             return members;
       
   759         }
       
   760     }
       
   761 
       
   762 
       
   763     /**
       
   764      * Helper class to store the what log output into orm.
       
   765      */
       
   766     public static class WhatLogMember {
       
   767         private String member;
       
   768         private boolean memberExists;
       
   769 
       
   770         public WhatLogMember(String mbr, boolean exists) {
       
   771             member = mbr;
       
   772             memberExists = exists;
       
   773         }
       
   774         
       
   775         public String getMember() {
       
   776             return member;
       
   777         }
       
   778         
       
   779         public boolean getMemberExists() {
       
   780             return memberExists;
       
   781         }
       
   782     }
       
   783 }