buildframework/helium/sf/java/metadata/src/com/nokia/helium/metadata/ant/types/SysdefMetaDataInput.java
changeset 628 7c4a911dc066
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 package com.nokia.helium.metadata.ant.types;
       
    18 
       
    19 import java.io.File;
       
    20 import java.io.IOException;
       
    21 
       
    22 import javax.persistence.EntityManager;
       
    23 import javax.persistence.EntityManagerFactory;
       
    24 import javax.xml.parsers.ParserConfigurationException;
       
    25 import javax.xml.parsers.SAXParser;
       
    26 import javax.xml.parsers.SAXParserFactory;
       
    27 
       
    28 import org.apache.tools.ant.BuildException;
       
    29 import org.apache.tools.ant.Task;
       
    30 import org.apache.tools.ant.types.DataType;
       
    31 import org.xml.sax.Attributes;
       
    32 import org.xml.sax.SAXException;
       
    33 import org.xml.sax.helpers.DefaultHandler;
       
    34 
       
    35 import com.nokia.helium.metadata.MetaDataInput;
       
    36 import com.nokia.helium.metadata.MetadataException;
       
    37 import com.nokia.helium.metadata.model.metadata.SysdefCollection;
       
    38 import com.nokia.helium.metadata.model.metadata.SysdefCollectionDAO;
       
    39 import com.nokia.helium.metadata.model.metadata.SysdefComponent;
       
    40 import com.nokia.helium.metadata.model.metadata.SysdefComponentDAO;
       
    41 import com.nokia.helium.metadata.model.metadata.SysdefPackage;
       
    42 import com.nokia.helium.metadata.model.metadata.SysdefPackageDAO;
       
    43 import com.nokia.helium.metadata.model.metadata.SysdefUnit;
       
    44 import com.nokia.helium.metadata.model.metadata.SysdefUnitDAO;
       
    45 
       
    46 /**
       
    47  * The sysdefMetadataInput allows you to record a the current build
       
    48  * model in the database. Packages, collection, component and units
       
    49  * will be saved.
       
    50  *
       
    51  * @ant.type name="sysdefmetadatainput" category="Metadata"
       
    52  */
       
    53 public class SysdefMetaDataInput extends DataType implements MetaDataInput {
       
    54     private File file;
       
    55     private File epocroot;
       
    56     
       
    57     @Override
       
    58     public void extract(Task task, EntityManagerFactory factory)
       
    59         throws MetadataException {
       
    60         if (file == null) {
       
    61             throw new MetadataException("file attribute is not defined.");
       
    62         }
       
    63         getEpocroot();
       
    64         SAXParserFactory saxFactory = SAXParserFactory.newInstance();
       
    65         SAXParser parser;
       
    66         EntityManager entityManager = factory.createEntityManager();
       
    67         try {
       
    68             task.log("Extracting data from " + file);
       
    69             parser = saxFactory.newSAXParser();
       
    70             parser.parse(file, new SysdefParserHandler(entityManager));
       
    71         } catch (ParserConfigurationException e) {
       
    72             throw new MetadataException(e.getMessage(), e);
       
    73         } catch (SAXException e) {
       
    74             throw new MetadataException(e.getMessage(), e);
       
    75         } catch (IOException e) {
       
    76             throw new MetadataException(e.getMessage(), e);
       
    77         } finally {
       
    78             entityManager.close();
       
    79         }
       
    80     }
       
    81 
       
    82     /**
       
    83      * Defines epocroot. 
       
    84      * @param epocroot
       
    85      * @ant.not=required Default to EPOCROOT.
       
    86      */
       
    87     public void setEpocroot(File epocroot) {
       
    88         this.epocroot = epocroot;
       
    89     }
       
    90     
       
    91     /**
       
    92      * Get epocroot.
       
    93      * @return a File object representing epocroot, or throw a BuildException
       
    94      *         if epocroot attribute and EPOCROOT environment variable
       
    95      *         are not defined.
       
    96      */
       
    97     protected File getEpocroot() {
       
    98         if (epocroot != null) {
       
    99             return epocroot;
       
   100         } else if (System.getenv("EPOCROOT") != null) {
       
   101             return (new File(System.getenv("EPOCROOT") )).getAbsoluteFile();
       
   102         }
       
   103         throw new BuildException("EPOCROOT environment variable or epocroot attribute is not defined.");
       
   104     }
       
   105     
       
   106     /**
       
   107      * Define the location of the system definition file.
       
   108      * @param file
       
   109      * @ant.required
       
   110      */
       
   111     public void setFile(File file) {
       
   112         this.file = file;
       
   113     }
       
   114     
       
   115     /**
       
   116      * Internal Handler to parse the sysdef file 
       
   117      * using the SAX interface. 
       
   118      *
       
   119      */
       
   120     class SysdefParserHandler extends DefaultHandler {
       
   121 
       
   122         private static final String PACKAGE_TAG = "package";
       
   123         private static final String COLLECTION_TAG = "collection";
       
   124         private static final String COMPONENT_TAG = "component";
       
   125         private static final String UNIT_TAG = "unit";
       
   126         private static final String ID_ATTR = "id";
       
   127         private static final String NAME_ATTR = "name";
       
   128         private static final String BLDFILE_ATTR = "bldFile";
       
   129         private SysdefPackage currentPackage;
       
   130         private SysdefCollection currentCollection;
       
   131         private SysdefComponent currentComponent;
       
   132         private EntityManager entityManager;
       
   133         private SysdefPackageDAO packageDAO;
       
   134         private SysdefCollectionDAO collectionDAO;
       
   135         private SysdefComponentDAO componentDAO;
       
   136         private SysdefUnitDAO unitDAO;
       
   137         
       
   138         public SysdefParserHandler(EntityManager entityManager) {
       
   139             this.entityManager = entityManager;
       
   140             packageDAO = new SysdefPackageDAO();
       
   141             packageDAO.setEntityManager(entityManager);
       
   142             collectionDAO = new SysdefCollectionDAO();
       
   143             collectionDAO.setEntityManager(entityManager);
       
   144             componentDAO = new SysdefComponentDAO();
       
   145             componentDAO.setEntityManager(entityManager);
       
   146             unitDAO = new SysdefUnitDAO();
       
   147             unitDAO.setEntityManager(entityManager);
       
   148         }
       
   149 
       
   150         /**
       
   151          * {@inheritDoc}
       
   152          */
       
   153         @Override
       
   154         public void startElement(String uri, String localName, String qName,
       
   155                 Attributes attributes) throws SAXException {
       
   156             if (PACKAGE_TAG.equals(qName)) {
       
   157                 currentPackage = packageDAO.getPackageById(attributes.getValue(ID_ATTR));
       
   158                 if (currentPackage == null) {
       
   159                     entityManager.getTransaction().begin();
       
   160                     currentPackage = new SysdefPackage();
       
   161                     currentPackage.setPackageId(attributes.getValue(ID_ATTR));
       
   162                     currentPackage.setName(attributes.getValue(NAME_ATTR));
       
   163                     entityManager.persist(currentPackage);
       
   164                     entityManager.getTransaction().commit();
       
   165                 }
       
   166             } else if (currentPackage != null && COLLECTION_TAG.equals(qName)) {
       
   167                 currentCollection = collectionDAO.getCollectionById(attributes.getValue(ID_ATTR));
       
   168                 if (currentCollection == null) {
       
   169                     entityManager.getTransaction().begin();
       
   170                     currentCollection = new SysdefCollection();
       
   171                     currentCollection.setCollectionId(attributes.getValue(ID_ATTR));
       
   172                     currentCollection.setName(attributes.getValue(NAME_ATTR));
       
   173                     currentCollection.setSysdefPackage(currentPackage);
       
   174                     entityManager.persist(currentCollection);
       
   175                     entityManager.getTransaction().commit();
       
   176                 }
       
   177             } else if (currentCollection != null && COMPONENT_TAG.equals(qName)) {
       
   178                 currentComponent = componentDAO.getComponentById(attributes.getValue(ID_ATTR));
       
   179                 if (currentComponent == null) {
       
   180                     entityManager.getTransaction().begin();
       
   181                     currentComponent = new SysdefComponent();
       
   182                     currentComponent.setComponentId(attributes.getValue(ID_ATTR));
       
   183                     currentComponent.setName(attributes.getValue(NAME_ATTR));
       
   184                     currentComponent.setSysdefCollection(currentCollection);
       
   185                     entityManager.persist(currentComponent);
       
   186                     entityManager.getTransaction().commit();
       
   187                 }
       
   188             } else if (currentComponent != null && UNIT_TAG.equals(qName) && attributes.getValue(BLDFILE_ATTR) != null) {
       
   189                 SysdefUnit unit = unitDAO.getUnitByLocation(getEpocroot(), new File(attributes.getValue(BLDFILE_ATTR)));
       
   190                 if (unit == null) {
       
   191                     entityManager.getTransaction().begin();
       
   192                     unit = new SysdefUnit();
       
   193                     // Location will be relative to epocroot.
       
   194                     unit.setLocation(getEpocroot().toURI().relativize((new File(attributes.getValue(BLDFILE_ATTR))).getAbsoluteFile().toURI()).getPath());
       
   195                     unit.setSysdefComponent(currentComponent);
       
   196                     entityManager.persist(unit);
       
   197                     entityManager.getTransaction().commit();
       
   198                 }
       
   199             }
       
   200         }
       
   201 
       
   202         /**
       
   203          * {@inheritDoc}
       
   204          */
       
   205         @Override
       
   206         public void endElement(String uri, String localName, String qName)
       
   207             throws SAXException {
       
   208             super.endElement(uri, localName, qName);
       
   209             if (PACKAGE_TAG.equals(qName)) {
       
   210                 currentPackage = null;
       
   211             } else if (COLLECTION_TAG.equals(qName)) {
       
   212                 currentCollection = null;
       
   213             } else if (COMPONENT_TAG.equals(qName)) {
       
   214                 currentComponent = null;
       
   215             }
       
   216         }
       
   217         
       
   218     }
       
   219 
       
   220 }