/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package framework.audit;
import framework.utils.Converter;
import framework.utils.Pair;
import framework.utils.XMLData;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
/**
*
* @author vip
*/
public class Parser {
public static String xmlCheckupToUserView(String xml, String metadata) throws JDOMException {
String target = "\n";
XMLData data = new XMLData(xml, metadata);
//название анализа
//Фиелды до таблицы
ArrayList<Pair<Integer, String>> preField = data.getPreFieldList();
for(int i = 0; i < preField.size(); i++) {
String fieldName = preField.get(i).second + ": ";
String fieldValue = data.getField(preField.get(i).first);
if (fieldValue == null || fieldValue.trim().isEmpty()) {
continue;
}
target += fieldName + fieldValue + "\n";
}
ArrayList<String> probeList = data.getProbeList();
if (!probeList.isEmpty()) {
//добавляем заголовок таблицы
ArrayList<String> valueList = data.getProbeValueList();
//добавляем строки таблицы
for (int i = 0; i < probeList.size(); i++) {
if (!probeList.get(i).startsWith("separator")) {
String str = data.getProbeTitle(probeList.get(i)) + " ";
for (int j = 0; j < valueList.size(); j++) {
Object value = data.getProbeValue(probeList.get(i), valueList.get(j));
String metric = data.getProbeValueMetric(probeList.get(i), valueList.get(j));
String valueMetric = ( value == null ? "" : value.toString() + " " + (metric == null ? "" : metric));
if (value != null && !value.toString().trim().isEmpty()) {
str += valueMetric + ", ";
}
}
String strProbeNumber = "";
String probeNumber = data.getProbeNumber(probeList.get(i));
if (probeNumber != null) {
if (probeNumber.isEmpty()) {
strProbeNumber = " (Номер пробы не указан)";
} else {
strProbeNumber = " (Номер пробы " + probeNumber + ")";
}
}
if (!str.trim().isEmpty()) {
target += str + strProbeNumber + "\n";
}
}
}
}
//Фиелды после таблицы
ArrayList<Pair<Integer, String>> postField = data.getPostFieldList();
for(int i = 0; i < postField.size(); i++) {
String fieldName = postField.get(i).second + ": ";
String fieldValue = data.getField(postField.get(i).first);
if (fieldValue == null || fieldValue.trim().isEmpty()) {
continue;
}
target += fieldName + fieldValue + "\n";
}
return target;
}
public static String xmlCertificateToUserView(String xml, String metadata) throws JDOMException {
String target = "\n";
//Распарсим названия параметров
//Мап <Реальное имя, имя>
HashMap<String, String> paramMap = new HashMap<String, String>();
Document meta = Converter.stringToXml(metadata);
Element rootMeta = meta.getRootElement();
for (Element el : (List<Element>) rootMeta.getChildren("Param")) {
String name = el.getChildText("Name");
String rname = el.getChildText("RealName");
paramMap.put(rname, name);
}
//Парсим хмл
Document doc = Converter.stringToXml(xml);
Element root = doc.getRootElement();
for (Element el : (List<Element>) root.getChildren()) {
String name = paramMap.get(el.getName());
if (name == null) {
continue;
}
String text = el.getTextTrim();
if (text.startsWith("\"") && text.endsWith("\"")) {
text = text.substring(1, text.length() - 1);
}
target += name + ": " + text + "\n";
}
return target;
}
public static String xmlCollaboratorToUserView(String xml, String metadata) throws JDOMException {
String target = "\n";
//Парсим хмл
Document doc = Converter.stringToXml(xml);
Element root = doc.getRootElement();
for (Element el : (List<Element>) root.getChildren()) {
String name = el.getName();
if (name == null) {
continue;
}
String text = el.getTextTrim();
if (text.startsWith("\"") && text.endsWith("\"")) {
text = text.substring(1, text.length() - 1);
}
target += name + ": " + text + "\n";
}
return target;
}
}