// @SuppressWarnings(value = { "unchecked" })
@SuppressWarnings("unchecked")
private void saveElements(List<Node> elementList, Connection con,
Long parentAutoId, String parentId) throws SQLException,
ClassNotFoundException {
VarBean varBean = null;
Node elements, idNode;
List<Node> childrenList;
int orderNr = 0;
Class<?> theClass = null;
// Id:s
String currentId = "";
// Order nr
int theOptionNo = 0;
// Loop the elements
for (Node elementNode : elementList) {
// TODO: i think varBean should be nulled here....
log.log(Level.DEBUG, "Name of node: " + elementNode.getName());
idNode = elementNode.selectSingleNode("id");
if (idNode != null) {
// Options do not have id:s
log.log(Level.DEBUG, "id: "
+ elementNode.selectSingleNode("id").getText());
// Get the id for the element
currentId = idNode.getText();
}
// Get the class associated with the classname
theClass = Class.forName(elementNode.selectSingleNode("classname")
.getText());
// save container
if (Container.class.isAssignableFrom(theClass)) {
// remove other with same id and parent
// TODO: add this back, or?
// VarcontainerBean vbRemove = (VarcontainerBean)
// VarcontainerFinder
// .findByPrimaryKey(con, new VarcontainerKey(currentId));
// if (null != vbRemove) {
// log.log(Level.WARN,
// "removing duplicate container (and all it's children): "
// + vbRemove.getId());
// // vbRemove.remove(con);
// // removeContainer(con, currentId);
// removeContainer(con, vbRemove);
// }
varBean = new VarcontainerBean();
((VarcontainerBean) varBean).setDocument(getDocumentName());
// metalevel is optional
if (null != elementNode.selectSingleNode("metalevel")) {
((VarcontainerBean) varBean).setMetalevel(elementNode
.selectSingleNode("metalevel").getText());
}
// saveAttributes(con, currentId,
// elementNode.selectNodes("attributes/*"));
// saveScripts(con, currentId,
// elementNode.selectNodes("scripts/*"));
// saveIntervals(con, currentId,
// elementNode.selectNodes("intervals/*"));
// saveServices(con, currentId,
// elementNode.selectNodes("services/*"));
} else if (OptionVariable.class.isAssignableFrom(theClass)) {
// Options, get the id from the select variable
// saveOption(theVarId, elementNode, theOptionNo, con);
// TODO: this should have parentautoid somehow...
saveOption(parentId, elementNode, theOptionNo, con);
theOptionNo++;
} else {
// remove other with same id and parent
// TODO: add this back, or?
// VarvarBean vbRemove = (VarvarBean) VarvarFinder
// .findByPrimaryKey(con, new VarvarKey(currentId));
// if (null != vbRemove) {
// log.log(Level.WARN, "removing duplicate container: "
// + vbRemove.getId());
// vbRemove.remove(con);
// }
varBean = new VarvarBean();
// orderNr = ++theVarOrderNr;
// Reset list option nr
theOptionNo = 0;
// saveAttributes(con, currentId,
// elementNode.selectNodes("attributes/*"));
// saveScripts(con, currentId,
// elementNode.selectNodes("scripts/*"));
// saveIntervals(con, currentId,
// elementNode.selectNodes("intervals/*"));
// saveServices(con, currentId,
// elementNode.selectNodes("services/*"));
// dataId
if (elementNode.selectSingleNode("dataId") != null) {
((VarvarBean) varBean).setDataid(elementNode
.selectSingleNode("dataId").getText());
}
}
if (varBean != null) {
varBean.setParentautoid(parentAutoId);
varBean.setParentid(parentId);
varBean.setOrdernumber(Integer.valueOf(orderNr));
// Type (classname)
if (elementNode.selectSingleNode("classname") != null) {
varBean.setType(elementNode.selectSingleNode("classname")
.getText());
}
// Id
if (elementNode.selectSingleNode("id") != null) {
varBean.setId(elementNode.selectSingleNode("id").getText());
}
// Printing
if (elementNode.selectSingleNode("printing") != null) {
List<Node> list = elementNode.selectSingleNode("printing")
.selectNodes("child::*");
if (list.size() > 0) {
StringBuffer buf = new StringBuffer();
// Collect the printing types, separated with a comma,
// stored in the same database column
for (Node d : list) {
if ("true".equals(d.getStringValue())) {
buf.append(d.getName()).append(",");
}
}
if (buf.length() > 0) {
// only store printing info if we have some
String res = buf.toString();
res = res.substring(0, res.length() - 1);
varBean.setPrinting(res);
}
}
}
// Explanation
Node exNode = elementNode.selectSingleNode("comment");
if (exNode != null && !exNode.getText().equals("")) {
varBean.setExplanation(exNode.getText());
}
Timestamp ts = new Timestamp(Calendar.getInstance()
.getTimeInMillis());
// Tscreated
varBean.setTscreated(ts);
// Tsupdated
varBean.setTsupdated(ts);
// Createdby
varBean.setCreatedby("System");
// Updatedby
varBean.setUpdatedby("System");
varBean.setNodename(elementNode.getName());
// Save the var bean
if (log.isDebugEnabled()) {
log.log(Level.DEBUG,
"Creating variable: " + varBean.toString());
}
varBean.create(con);
saveTexts(currentId, elementNode.selectSingleNode("texts"), con);
// we need autoid before we can do this...
if (Container.class.isAssignableFrom(theClass)) {
// containers
saveContainerAttributes(con, varBean.getAutoid(),
currentId, elementNode.selectNodes("attributes/*"));
saveContainerScripts(con, varBean.getAutoid(), currentId,
elementNode.selectNodes("scripts/*"));
saveContainerIntervals(con, varBean.getAutoid(), currentId,
elementNode.selectNodes("intervals/*"));
saveContainerServices(con, varBean.getAutoid(), currentId,
elementNode.selectNodes("services/*"));
} else {
// vars
saveAttributes(con, varBean.getAutoid(), currentId,
elementNode.selectNodes("attributes/*"));
saveScripts(con, varBean.getAutoid(), currentId,
elementNode.selectNodes("scripts/*"));
saveIntervals(con, varBean.getAutoid(), currentId,
elementNode.selectNodes("intervals/*"));
saveServices(con, varBean.getAutoid(), currentId,
elementNode.selectNodes("services/*"));
}
}
// Recursively read the rest
elements = elementNode.selectSingleNode("elements");
if (elements != null) {
childrenList = elements.selectNodes("child::*");
saveElements(childrenList, con,
varBean != null ? varBean.getAutoid() : null, currentId);
}
orderNr++;
}
}