Search in sources :

Example 11 with CategoryImpl

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

the class ACSCategoryDAOImpl method setParentID.

/**
	 * Set the parent ID of the passed category
	 * 
	 * Each category has a parent ID that can be evaluated by reading
	 * the name of the category.
	 * If the name does not contain ':' then the parent ID is the ROOT.
	 * Otherwise its parent is the category whose name is represented
	 * by the substring before the ':'
	 * 
	 * @param cat
	 */
private void setParentID(CategoryImpl cat) {
    if (cat.getPath().equals("ROOT")) {
        // ROOT has no parent
        cat.setParentId(null);
        return;
    }
    String name = cat.getPath();
    int pos = name.lastIndexOf(':');
    if (pos == -1) {
        // This category parent is ROOT
        Category root = getCategoryByPath("ROOT");
        cat.setParentId(root.getCategoryId());
        cat.setPath(cat.getPath());
        cat.setName(cat.getPath());
        return;
    }
    String parentID = name.substring(0, pos);
    CategoryImpl parent = (CategoryImpl) getCategoryByPath(parentID);
    if (parent == null) {
        logger.log(AcsLogLevel.WARNING, "Parent category of " + parentID + " NOT found");
        return;
    }
    cat.setParentId(parent.getCategoryId());
}
Also used : CategoryImpl(cern.laser.business.data.CategoryImpl) Category(cern.laser.business.data.Category)

Example 12 with CategoryImpl

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

the class ACSCategoryDAOImpl method linkWithAlarms.

public void linkWithAlarms() {
    if (conf == null)
        throw new IllegalStateException("null configuration accessor");
    if (alarmDao == null)
        throw new IllegalStateException("missing alarm DAO");
    String path = ALARM_CATEGORY_DEFINITION_PATH;
    String xml;
    try {
        xml = conf.getConfiguration(path);
    } catch (Exception e) {
        throw new RuntimeException("Failed to read " + path);
    }
    AlarmCategoryDefinitions acds;
    try {
        acds = (AlarmCategoryDefinitions) AlarmCategoryDefinitions.unmarshalAlarmCategoryDefinitions(new StringReader(xml));
    } catch (Exception e) {
        throw new RuntimeException("Failed to parse " + path);
    }
    AlarmCategoryLinkDefinitionListType cltc = acds.getCategoryLinksToCreate();
    if (cltc == null)
        throw new RuntimeException("Missing category-links-to-create in " + path);
    AlarmCategoryLinkType[] links = cltc.getAlarmCategoryLink();
    for (int a = 0; a < links.length; a++) {
        AlarmCategoryLinkType l = links[a];
        alma.alarmsystem.alarmmessage.generated.Alarm linkAlarm = l.getAlarm();
        alma.alarmsystem.alarmmessage.generated.Category linkCat = l.getCategory();
        if (linkAlarm == null || linkCat == null)
            throw new RuntimeException("Missing alarm or category in a link in " + path);
        AlarmDefinition ad = linkAlarm.getAlarmDefinition();
        CategoryDefinition cd = linkCat.getCategoryDefinition();
        if (ad == null || cd == null)
            throw new RuntimeException("Missing alarm-definition or category-definition in " + path);
        String lff = ad.getFaultFamily();
        String lfm = ad.getFaultMember();
        if (lff == null || lfm == null)
            throw new RuntimeException("Missing fault-family or fault-member in " + path);
        String alarmId = Triplet.toIdentifier(lff, lfm, new Integer(ad.getFaultCode()));
        String catPath = cd.getPath();
        if (catPath == null)
            throw new RuntimeException("Missing category path in " + path);
        Alarm alarm = alarmDao.getAlarm(alarmId);
        Category cat = getCategoryByPath(catPath);
        if (alarm == null)
            throw new RuntimeException("Missing alarm with ID " + alarmId);
        if (cat == null)
            throw new RuntimeException("Missing category with path " + catPath);
        cat.addAlarm(alarm);
    }
    Iterator<Category> i = catPathToCategory.values().iterator();
    while (i.hasNext()) {
        Category ci = i.next();
        String cPath = ci.getPath();
        int lastcolon = cPath.lastIndexOf(':');
        if (lastcolon >= 0) {
            String parentCPath = cPath.substring(0, lastcolon);
            CategoryImpl cp = (CategoryImpl) catPathToCategory.get(parentCPath);
            if (cp != null) {
                cp.addChildCategory(ci);
            }
        }
    }
}
Also used : Category(cern.laser.business.data.Category) AlarmDefinition(alma.alarmsystem.alarmmessage.generated.AlarmDefinition) AlarmCategoryLinkType(alma.alarmsystem.alarmmessage.generated.AlarmCategoryLinkType) LaserObjectNotFoundException(cern.laser.business.LaserObjectNotFoundException) CategoryImpl(cern.laser.business.data.CategoryImpl) Alarm(cern.laser.business.data.Alarm) StringReader(java.io.StringReader) AlarmCategoryLinkDefinitionListType(alma.alarmsystem.alarmmessage.generated.AlarmCategoryLinkDefinitionListType) AlarmCategoryDefinitions(alma.alarmsystem.alarmmessage.generated.AlarmCategoryDefinitions) CategoryDefinition(alma.alarmsystem.alarmmessage.generated.CategoryDefinition)

Example 13 with CategoryImpl

use of cern.laser.business.data.CategoryImpl 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");
    }
    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) CategoryImpl(cern.laser.business.data.CategoryImpl) DocumentBuilder(javax.xml.parsers.DocumentBuilder)

Example 14 with CategoryImpl

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

the class ACSCategoryDAOImpl method adjustParentIDs.

/**
	 * Set the ID of this category in the children list of its parents
	 * 
	 * A category contains a list of all its children.
	 * The first category is ROOT.
	 * If a category is child of another category is inferred by its name.
	 * If a category has no parents, it is set to be a ROOT child.
	 * 
	 * @param childrenName The name of this category
	 * @param ID The ID of this category 
	 */
private void adjustParentIDs(String childrenName, int ID) {
    if (childrenName == null || childrenName.length() == 0) {
        throw new IllegalArgumentException("Invalid children name " + childrenName);
    }
    // If the name does not contains ':' then it is a child of ROOT
    if (!childrenName.contains(":")) {
        CategoryImpl root = (CategoryImpl) getCategoryByPath("ROOT");
        Set<Integer> children = root.getChildrenIds();
        if (children == null) {
            children = new HashSet<Integer>();
        }
        children.add(new Integer(ID));
        root.setChildrenIds(children);
        return;
    }
    // The name contains ':' 
    int pos = childrenName.lastIndexOf(':');
    String parentID = childrenName.substring(0, pos);
    CategoryImpl parent = (CategoryImpl) getCategoryByPath(parentID);
    if (parent == null) {
        logger.log(AcsLogLevel.WARNING, "Parent category of " + parentID + " NOT found");
        return;
    }
    Set<Integer> children = parent.getChildrenIds();
    if (children == null) {
        children = new HashSet<Integer>();
    }
    children.add(new Integer(ID));
    parent.setChildrenIds(children);
}
Also used : CategoryImpl(cern.laser.business.data.CategoryImpl)

Example 15 with CategoryImpl

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

the class ACSCategoryDAOImpl method getAlarms.

public String[] getAlarms(Integer categoryId) {
    Category c = getCategory(categoryId);
    if (c == null)
        return new String[0];
    Set ids = ((CategoryImpl) c).getAlarmIds();
    String[] result = new String[ids.size()];
    ids.toArray(result);
    return result;
}
Also used : CategoryImpl(cern.laser.business.data.CategoryImpl) Category(cern.laser.business.data.Category) HashSet(java.util.HashSet) Set(java.util.Set)

Aggregations

CategoryImpl (cern.laser.business.data.CategoryImpl)24 Category (cern.laser.business.data.Category)14 HashSet (java.util.HashSet)8 CategoryDefinition (alma.alarmsystem.alarmmessage.generated.CategoryDefinition)6 LaserObjectNotFoundException (cern.laser.business.LaserObjectNotFoundException)6 Alarm (cern.laser.business.data.Alarm)5 StringReader (java.io.StringReader)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)3 ResponsiblePerson (cern.laser.business.data.ResponsiblePerson)3 Source (cern.laser.business.data.Source)3 IOException (java.io.IOException)3 Categories (alma.acs.alarmsystem.generated.Categories)2 CategoryDefinitionListType (alma.alarmsystem.alarmmessage.generated.CategoryDefinitionListType)2 CategoryDefinitions (alma.alarmsystem.alarmmessage.generated.CategoryDefinitions)2 AdminUser (cern.laser.business.data.AdminUser)2 Building (cern.laser.business.data.Building)2