Search in sources :

Example 41 with Category

use of cern.laser.business.data.Category in project ACS by ACS-Community.

the class ACSCategoryDAOTest method testGettingCategories.

/**
	 * Test findCategory and getCategory
	 * @throws Exception
	 */
public void testGettingCategories() throws Exception {
    Category root = categoryDAO.getCategoryByPath("ROOT");
    assertNotNull("Found a null ROOT category", root);
    assertEquals(categoryDAO.getCategory(root.getCategoryId()), categoryDAO.findCategory(root.getCategoryId()));
    // Try to get a non existent Category 
    Category cat = categoryDAO.getCategory(Integer.valueOf(-1));
    assertNull("A category with ID=-1 should not be found by getCategory", cat);
    cat = root;
    try {
        cat = categoryDAO.findCategory(Integer.valueOf(-1));
    } catch (Exception e) {
        // We expect to be here as the category does not exist
        cat = null;
    }
    assertNull("A category with ID=-1 should not be found", cat);
}
Also used : Category(cern.laser.business.data.Category)

Example 42 with Category

use of cern.laser.business.data.Category in project ACS by ACS-Community.

the class ACSCategoryDAOImpl method assignDefaultCategory.

/**
	 * Assign the default category to the alarms not assigned to any category
	 * 
	 * Scans all the alarms to check for alarms without any category and assign the default
	 * category to them.
	 * 
	 * @param defCategory The default category
	 */
private void assignDefaultCategory(Category defCategory) {
    if (defCategory == null) {
        throw new IllegalArgumentException("Invalid null category");
    }
    String[] IDs = ((ACSAlarmDAOImpl) alarmDao).getAllAlarmIDs();
    for (String alarmID : IDs) {
        Alarm alarm = alarmDao.getAlarm(alarmID);
        if (alarm == null) {
            logger.log(AcsLogLevel.WARNING, "Got a null alarm for ID=" + alarmID);
            continue;
        }
        Collection<Category> categories = alarm.getCategories();
        if (categories == null) {
            categories = new HashSet<Category>();
        }
        if (categories.size() == 0) {
            categories.add(defCategory);
            alarm.setCategories(categories);
            StringBuilder str = new StringBuilder("Alarm ");
            str.append(alarm.getAlarmId());
            str.append(" assigned to default category ");
            str.append(defCategory.getPath());
            logger.log(AcsLogLevel.DEBUG, str.toString());
        }
    }
}
Also used : Category(cern.laser.business.data.Category) Alarm(cern.laser.business.data.Alarm)

Example 43 with Category

use of cern.laser.business.data.Category in project ACS by ACS-Community.

the class ACSCategoryDAOTest method testDeleteCategory.

/**
	 * Test the deletion of a category
	 * @throws Exception
	 */
public void testDeleteCategory() throws Exception {
    Integer[] initialIDs = categoryDAO.getAllCategoryIDs();
    assertNotNull(initialIDs);
    int initialLen = initialIDs.length;
    initialIDs = null;
    Category root = categoryDAO.getCategoryByPath("ROOT");
    assertNotNull("Found a null ROOT category", root);
    Category catToDelete = categoryDAO.getCategory((root.getCategoryId() + 1));
    assertNotNull("Category not found", catToDelete);
    // Delete the category
    categoryDAO.deleteCategory(catToDelete);
    assertEquals(initialLen - 1, categoryDAO.getAllCategoryIDs().length);
    // Try to get the deleted CAT
    Category removedCat = categoryDAO.getCategory(catToDelete.getCategoryId());
    assertNull("The category should have been deleted", removedCat);
}
Also used : Category(cern.laser.business.data.Category)

Example 44 with Category

use of cern.laser.business.data.Category in project ACS by ACS-Community.

the class ACSCategoryDAOTest method testUpdateCategory.

/**
	 * Test if updating a category works
	 */
public void testUpdateCategory() {
    // The root is the only one having childs
    Category root = categoryDAO.getCategoryByPath("ROOT");
    assertNotNull("Found a null ROOT category", root);
    // Get the number of defined categories
    Integer[] IDs = categoryDAO.getAllCategoryIDs();
    int len = IDs.length;
    // get a category to update
    Category cat = categoryDAO.getCategory(root.getCategoryId() + 1);
    assertNotNull(cat);
    CategoryDefinition def = cat.getDefinition();
    assertNotNull(def);
    String newDesc = "New Description";
    def.setDescription(newDesc);
    cat.setDefinition(def);
    // Update the category
    categoryDAO.updateCategory(cat);
    // Check if the size of the categories is right
    assertEquals(len, categoryDAO.getAllCategoryIDs().length);
    // Get the category
    cat = categoryDAO.getCategory(cat.getCategoryId());
    assertNotNull("Category not found", cat);
    // Check if the description has been updated
    assertEquals(newDesc, cat.getDescription());
}
Also used : Category(cern.laser.business.data.Category) CategoryDefinition(cern.laser.business.definition.data.CategoryDefinition)

Example 45 with Category

use of cern.laser.business.data.Category in project ACS by ACS-Community.

the class ACSCategoryDAOImpl method loadCategories.

/**
	 * Load the categories from the CDB.
	 * <P>
	 * Loads all the category from the CDB and build an internal
	 * representation of category.
	 * The category is also added to all the alarms having the
	 * fault family specified in the XML.
	 * <P>
	 * All the categories derive from ROOT that is built here
	 * as default (in this way the user does ot need to add the ROOT
	 * entry in the CDB).
	 * 
	 * @return list of Category entries read from CDB 
	 * @throws Exception In case of error reading the values from the CDB
	 */
public alma.acs.alarmsystem.generated.Category[] loadCategories() throws Exception {
    if (conf == null) {
        throw new IllegalStateException("Missing dal");
    }
    categories.clear();
    String xml;
    try {
        xml = conf.getConfiguration(CATEGORY_DEFINITION_PATH);
    } catch (Throwable t) {
        throw new RuntimeException("Couldn't read alarm list", t);
    }
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder;
    try {
        builder = factory.newDocumentBuilder();
    } catch (Exception e) {
        throw new Exception("Error building the DocumentBuilder from the DocumentBuilderFactory", e);
    }
    StringReader stringReader = new StringReader(xml);
    InputSource inputSource = new InputSource(stringReader);
    Document doc;
    try {
        doc = builder.parse(inputSource);
        if (doc == null) {
            throw new Exception("The builder returned a null Document after parsing");
        }
    } catch (Exception e) {
        throw new Exception("Error parsing XML: " + xml, e);
    }
    NodeList docChilds = doc.getChildNodes();
    if (docChilds == null || docChilds.getLength() != 1) {
        throw new Exception("Malformed xml: only one node (categories) expected");
    }
    Node categoriesNode = docChilds.item(0);
    Unmarshaller FF_unmarshaller = new Unmarshaller(Categories.class);
    FF_unmarshaller.setValidation(false);
    FF_unmarshaller.setWhitespacePreserve(true);
    Categories daoCategories;
    try {
        daoCategories = (Categories) FF_unmarshaller.unmarshal(categoriesNode);
        logger.log(AcsLogLevel.DEBUG, "Categories definition read");
    } catch (Exception e) {
        throw new Exception("Error parsing " + CATEGORY_DEFINITION_PATH, e);
    }
    alma.acs.alarmsystem.generated.Category[] daoCategory = daoCategories.getCategory();
    if (daoCategory == null || daoCategory.length == 0) {
        logger.log(AcsLogLevel.DEBUG, "No category defined");
    }
    // Add the root Category
    addRootCategory();
    // Goes through all the Categories read from the CDB
    for (alma.acs.alarmsystem.generated.Category category : daoCategory) {
        cern.laser.business.definition.data.CategoryDefinition definition = new cern.laser.business.definition.data.CategoryDefinition(category.getPath(), category.getDescription());
        CategoryImpl ci = new CategoryImpl();
        // will get filled in later
        ci.setAlarmIds(new HashSet());
        ci.setCategoryId(new Integer(nextCatID++));
        ci.setChildrenIds(new HashSet<Integer>());
        ci.setDescription(definition.getDescription());
        ci.setName(definition.getPath());
        ci.setPath(definition.getPath());
        ci.setAlarmIds(new HashSet());
        setParentID(ci);
        // Stores the categories
        categories.put(ci.getCategoryId(), ci);
        catPathToCategory.put(ci.getPath(), ci);
        logger.log(AcsLogLevel.DEBUG, "Category " + ci.getName() + " added with ID=" + ci.getCategoryId());
        // Check if the category is defined as default.
        if (category.hasIsDefault() && category.getIsDefault() == true) {
            if (defaultCategory != null) {
                StringBuilder str = new StringBuilder("CDB misconfiguration: default category defined more then once (actual default: ");
                str.append(defaultCategory.getPath());
                str.append(", new default: ");
                str.append(category.getPath());
                logger.log(AcsLogLevel.WARNING, str.toString());
            } else {
                defaultCategory = ci;
            }
        }
        // A category contains a set of child ids.
        // This method adjusts the references of categories between the parent 
        // and the child
        adjustParentIDs(ci.getName(), ci.getCategoryId());
        // Connect alarms to this category
        for (alma.acs.alarmsystem.generated.Category cat : daoCategories.getCategory()) {
            if (cat.getPath().equals(ci.getPath())) {
                String[] families = cat.getAlarms().getFaultFamily();
                for (String faultFamily : families) {
                    assignCategoryToAlarms(ci, faultFamily);
                }
            }
        }
    }
    // Assign core alarms to ROOT category
    assignCategoryOfCoreAlarms();
    // Log a message if no category has been defined in the CDB
    if (defaultCategory == null) {
        logger.log(AcsLogLevel.WARNING, "No default category defined in CDB");
    } else {
        // Check if there are alarms without category to assign to the default
        assignDefaultCategory(defaultCategory);
    }
    return daoCategory;
}
Also used : InputSource(org.xml.sax.InputSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) Category(cern.laser.business.data.Category) Node(org.w3c.dom.Node) Document(org.w3c.dom.Document) StringReader(java.io.StringReader) Unmarshaller(org.exolab.castor.xml.Unmarshaller) CategoryDefinition(alma.alarmsystem.alarmmessage.generated.CategoryDefinition) HashSet(java.util.HashSet) Categories(alma.acs.alarmsystem.generated.Categories) NodeList(org.w3c.dom.NodeList) LaserObjectNotFoundException(cern.laser.business.LaserObjectNotFoundException) ValidationException(org.exolab.castor.xml.ValidationException) MarshalException(org.exolab.castor.xml.MarshalException) IOException(java.io.IOException) CategoryImpl(cern.laser.business.data.CategoryImpl) DocumentBuilder(javax.xml.parsers.DocumentBuilder)

Aggregations

Category (cern.laser.business.data.Category)47 Alarm (cern.laser.business.data.Alarm)16 CategoryImpl (cern.laser.business.data.CategoryImpl)14 HashSet (java.util.HashSet)9 AdminUser (cern.laser.business.data.AdminUser)8 LaserDefinitionNotValidException (cern.laser.business.definition.LaserDefinitionNotValidException)8 LaserDefinitionDuplicationException (cern.laser.business.definition.LaserDefinitionDuplicationException)7 LaserDefinitionNotAllowedException (cern.laser.business.definition.LaserDefinitionNotAllowedException)7 LaserDefinitionNotFoundException (cern.laser.business.definition.LaserDefinitionNotFoundException)7 CategoryDefinition (alma.alarmsystem.alarmmessage.generated.CategoryDefinition)6 LaserObjectNotFoundException (cern.laser.business.LaserObjectNotFoundException)6 AlarmCacheException (cern.laser.business.cache.AlarmCacheException)6 Source (cern.laser.business.data.Source)6 ResponsiblePerson (cern.laser.business.data.ResponsiblePerson)5 AlarmCategoryDefinitions (alma.alarmsystem.alarmmessage.generated.AlarmCategoryDefinitions)4 AlarmCategoryLinkDefinitionListType (alma.alarmsystem.alarmmessage.generated.AlarmCategoryLinkDefinitionListType)4 AlarmCategoryLinkType (alma.alarmsystem.alarmmessage.generated.AlarmCategoryLinkType)4 AlarmDefinition (alma.alarmsystem.alarmmessage.generated.AlarmDefinition)4 AlarmImpl (cern.laser.business.data.AlarmImpl)4 LaserDefinitionException (cern.laser.business.definition.LaserDefinitionException)4