buildframework/helium/sf/java/metadata/src/com/nokia/helium/metadata/ant/types/AbstractComponentBaseMetadataInput.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 
       
    18 package com.nokia.helium.metadata.ant.types;
       
    19 
       
    20 import java.util.Hashtable;
       
    21 import java.util.Map;
       
    22 
       
    23 import javax.persistence.EntityManager;
       
    24 
       
    25 import com.nokia.helium.metadata.model.metadata.Component;
       
    26 import com.nokia.helium.metadata.model.metadata.LogFile;
       
    27 
       
    28 /**
       
    29  * LogMetaDataInput which implements some abstract component handling. 
       
    30  *
       
    31  */
       
    32 public abstract class AbstractComponentBaseMetadataInput extends LogMetaDataInput {
       
    33     protected static final String DEFAULT_COMPONENT = "Default";
       
    34     private Map<String, Component> components = new Hashtable<String, Component>();
       
    35     private Component defaultComponent;
       
    36     
       
    37     /**
       
    38      * Provides an entity manager to manipulate the data.
       
    39      * @return
       
    40      */
       
    41     protected abstract EntityManager getEntityManager();
       
    42     
       
    43     /**
       
    44      * Defines the name of the default component.
       
    45      * @return a String with the name of the default component.
       
    46      */
       
    47     protected String getDefaultComponentName() {
       
    48         return DEFAULT_COMPONENT;
       
    49     }
       
    50     
       
    51     /**
       
    52      * Get the default component instance from the database.
       
    53      * Or create it if needed.
       
    54      * @param logFile logFile to associate the component to.
       
    55      * @return the Component instance. 
       
    56      */
       
    57     protected Component getDefaultComponent(LogFile logFile) {
       
    58         if (defaultComponent == null) {
       
    59             defaultComponent = getComponent(getDefaultComponentName(), logFile);
       
    60         }
       
    61         return defaultComponent;
       
    62     }
       
    63 
       
    64     /**
       
    65      * Get the database instance for the name component.
       
    66      * @param name the name of the component
       
    67      * @param logFile logFile to associate the component to.
       
    68      * @return the Component instance.
       
    69      */
       
    70     protected Component getComponent(String name, LogFile logFile) {
       
    71         if (!components.containsKey(name)) {
       
    72             Component component = createComponent(name, logFile);
       
    73             components.put(name, component);
       
    74             return component;
       
    75         }
       
    76         return components.get(name);
       
    77     }
       
    78     
       
    79     /**
       
    80      * Creates a new component. This method is called by the 
       
    81      * getComponent method if the component entity is not found
       
    82      * in the cache.
       
    83      * @param name the name of the component
       
    84      * @param logFile logFile to associate the component to.
       
    85      * @return the Component instance.
       
    86      */
       
    87     protected Component createComponent(String name, LogFile logFile) {
       
    88         Component component = new Component();
       
    89         component.setComponent(name);
       
    90         component.setLogFile(logFile);
       
    91         getEntityManager().getTransaction().begin();
       
    92         getEntityManager().persist(component);
       
    93         getEntityManager().getTransaction().commit();
       
    94         return component;
       
    95     }
       
    96 
       
    97     /**
       
    98      * Clear the components cache.
       
    99      */
       
   100     protected void clear() {
       
   101         components = new Hashtable<String, Component>();
       
   102         defaultComponent = null;
       
   103     }
       
   104 }