List of usage examples for org.hibernate Session saveOrUpdate
void saveOrUpdate(Object object);
From source file:com.npower.dm.hibernate.management.DeviceManagementBeanImpl.java
License:Open Source License
/** * Find node in the set of nodes by the node path and set the value into it. * This method will create missing node with a null value. * //from ww w . j av a2 s .c om * For example: * node and children: * ABC-- A -- A11 -- A111 * B -- B11 -- B111 * C -- C11 * * NodePath: C/C11/C111 * * will create C111 node and return C111 node. * * * @param nodes * @param nodePath * @return * @throws HibernateException * @throws DMException */ private DeviceTreeNode findorCreateDeviceTreeNode(Session session, DeviceTreeNode parentNode, String nodePath) throws HibernateException, DMException { // erase the "/" prefix if (nodePath.startsWith("/")) { nodePath = nodePath.substring(1, nodePath.length()); } Set<DeviceTreeNode> nodes = parentNode.getChildren(); for (Iterator<DeviceTreeNode> i = nodes.iterator(); i.hasNext();) { DeviceTreeNode node = i.next(); if (nodePath.equals(node.getName())) { return node; } if (nodePath.startsWith(node.getName() + "/")) { return this.findorCreateDeviceTreeNode(session, node, nodePath.substring(nodePath.indexOf("/"), nodePath.length())); } } int index = nodePath.indexOf("/"); if (index < 0) { String nodeName = nodePath; DeviceTreeNodeEntity node = new DeviceTreeNodeEntity(nodeName, parentNode, null); parentNode.getChildren().add(node); session.saveOrUpdate(node); return node; } else { String nodeName = nodePath.substring(0, index); DeviceTreeNodeEntity node = new DeviceTreeNodeEntity(nodeName, parentNode, null); parentNode.getChildren().add(node); session.saveOrUpdate(node); return this.findorCreateDeviceTreeNode(session, node, nodePath.substring(index, nodePath.length())); } }
From source file:com.npower.dm.hibernate.management.DeviceManagementBeanImpl.java
License:Open Source License
public void update(Device device) throws DMException { if (device == null) { throw new NullPointerException("Could not add or update a null device into DM inventory."); }/*from w w w. j a v a 2 s . c o m*/ Session session = this.getHibernateSession(); try { ((DeviceEntity) device).setLastUpdatedTime(new Date()); session.saveOrUpdate(device); } catch (HibernateException e) { throw new DMException(e); } finally { } }
From source file:com.npower.dm.hibernate.management.DeviceManagementBeanImpl.java
License:Open Source License
public void update(DeviceGroup group) throws DMException { Session session = this.getHibernateSession(); try {//from w w w . jav a 2 s . c o m session.saveOrUpdate(group); } catch (HibernateException e) { throw new DMException(e); } finally { } }
From source file:com.npower.dm.hibernate.management.DeviceManagementBeanImpl.java
License:Open Source License
/** * Add a device into this DeviceGroup./*from w w w .j a v a 2 s . co m*/ * * @param group * @param device * @throws DMException */ public void add(DeviceGroup group, Device device) throws DMException { if (device == null) { throw new DMException("Could not insert a NULL device into DeviceGroup"); } if (device.getID() == 0) { throw new DMException( "The device is un-saved, please save the device into DMinventory, before add into DeviceGroup!"); } Session session = null; try { session = this.getHibernateSession(); if (group.getID() == 0) { // If this unsaved, saveOrUpdate first. make sure the DeviceGroup's ID was generated! session.saveOrUpdate(group); } Device4DeviceGroupID id = new Device4DeviceGroupID(); id.setDeviceGroupId(group.getID()); id.setDeviceId(device.getID()); Device4DeviceGroup device4DeviceGroup = new Device4DeviceGroup(id, group, device); session.saveOrUpdate(device4DeviceGroup); // Link the device4DeviceGroup with this device group. ((DeviceGroupEntity) group).getDevicesForDeviceGroup().add(device4DeviceGroup); // Link the device4DeviceGroup to device ((DeviceEntity) device).getDeviceGroupDevices().add(device4DeviceGroup); } catch (HibernateException ex) { throw new DMException(ex); } }
From source file:com.npower.dm.hibernate.management.DeviceManagementBeanImpl.java
License:Open Source License
/** * Remove a device from this DeviceGroup. * // w w w .ja v a 2s . co m * @param group * @param device * @throws DMException */ public void remove(DeviceGroup group, Device device) throws DMException { if (device == null) { throw new DMException("Could not delete a NULL device from DeviceGroup"); } if (device.getID() == 0) { throw new DMException( "The device is un-saved, please save the device into DMinventory, before delet it from DeviceGroup!"); } Session session = null; try { session = this.getHibernateSession(); if (group.getID() == 0) { // If this unsaved, saveOrUpdate first. make sure the DeviceGroup's ID was generated! session.saveOrUpdate(group); } Device4DeviceGroupID id = new Device4DeviceGroupID(); id.setDeviceGroupId(group.getID()); id.setDeviceId(device.getID()); Set<Device4DeviceGroup> set = ((DeviceGroupEntity) group).getDevicesForDeviceGroup(); for (Iterator<Device4DeviceGroup> i = set.iterator(); i.hasNext();) { Device4DeviceGroup device4DeviceGroup = (Device4DeviceGroup) i.next(); if (id.equals(device4DeviceGroup.getID())) { // Remove from DM inventory. session.delete(device4DeviceGroup); // Remove the link the device4DeviceGroup with this device group. set.remove(device4DeviceGroup); break; } } } catch (HibernateException ex) { throw new DMException(ex); } }
From source file:com.npower.dm.hibernate.management.DeviceManagementBeanImpl.java
License:Open Source License
/** * Store the node into DeviceTree related with the device. * For exmaple: //w w w.java 2 s . c om * The nodePath is "./DevInfo/Mod", nodeValue is "W32 dm client" * * @param nodePath * @param nodeValue * @throws DMException */ public DeviceTreeNode updateDeviceTreeNode(String deviceID, String nodePath, Object nodeValue) throws DMException { Session session = this.getHibernateSession(); try { Device device = this.getDeviceByID(deviceID); if (device == null) { throw new DMException("Could not found the device: " + deviceID); } if (log.isDebugEnabled()) { log.debug("saving node: " + nodePath + "=" + nodeValue); } // Update booted flag for device device.setBooted(true); this.update(device); // Save node into inventory DeviceTree dTree = device.getDeviceTree(); if (dTree == null) { DeviceTreeNodeEntity rootNode = new DeviceTreeNodeEntity(".", null); session.saveOrUpdate(rootNode); dTree = new DeviceTreeEntity(rootNode); session.saveOrUpdate(dTree); device.setDeviceTree(dTree); session.saveOrUpdate(device); } // erase the "." prefix if (nodePath.startsWith(".")) { nodePath = nodePath.substring(1, nodePath.length()); } // erase the "/" prefix if (nodePath.startsWith("/")) { nodePath = nodePath.substring(1, nodePath.length()); } DeviceTreeNode node = this.findorCreateDeviceTreeNode(session, dTree.getRootNode(), nodePath); if (nodeValue == null) { node.setStringValue(null); node.setFormat(DDFNode.DDF_FORMAT_NULL); node.setType(DDFNode.DEFAULT_MIMETYPE); } else if (nodeValue instanceof String) { // String node.setStringValue((String) nodeValue); node.setFormat(DDFNode.DDF_FORMAT_CHR); node.setType(DDFNode.DEFAULT_MIMETYPE); } else if (nodeValue instanceof Boolean) { // Boolean node.setStringValue(((Boolean) nodeValue).toString()); node.setFormat(DDFNode.DDF_FORMAT_BOOL); node.setType(DDFNode.DEFAULT_MIMETYPE); } else if (nodeValue instanceof Long) { // Long or Integer node.setStringValue(((Long) nodeValue).toString()); node.setFormat(DDFNode.DDF_FORMAT_CHR); node.setType(DDFNode.DEFAULT_MIMETYPE); } else if (nodeValue instanceof byte[]) { // Byte[] node.setBinary((byte[]) nodeValue); node.setFormat(DDFNode.DDF_FORMAT_BIN); node.setType(DDFNode.MIMETYPE_BINARY); } else if (nodeValue instanceof TreeNode) { // TreeNode TreeNode tNode = (TreeNode) nodeValue; if (TreeNode.FORMAT_NODE.equals(tNode.getFormat())) { // Node node.setStringValue(null); node.setFormat(tNode.getFormat()); node.setType(tNode.getType()); } else { // Leaf node.setFormat(tNode.getFormat()); node.setType(tNode.getType()); nodeValue = tNode.getValue(); if (nodeValue == null) { node.setStringValue(null); } else if (nodeValue instanceof String) { // String node.setStringValue((String) nodeValue); } else if (nodeValue instanceof Boolean) { // Boolean node.setStringValue(((Boolean) nodeValue).toString()); } else if (nodeValue instanceof Long) { // Long or Integer node.setStringValue(((Long) nodeValue).toString()); } else if (nodeValue instanceof byte[]) { // Byte[] node.setFormat(DDFNode.DDF_FORMAT_BIN); node.setBinary((byte[]) nodeValue); } else { String v = (nodeValue == null) ? "" : nodeValue.toString(); node.setStringValue(v); } } } else { String v = (nodeValue == null) ? "" : nodeValue.toString(); node.setStringValue(v); } session.saveOrUpdate(node); if (log.isDebugEnabled()) { log.debug("saved node: " + nodePath + "=" + nodeValue); } return node; } catch (HibernateException e) { throw new DMException(e); } finally { } }
From source file:com.npower.dm.hibernate.management.DeviceManagementBeanImpl.java
License:Open Source License
public DeviceTreeNode findDeviceTreeNode(String deviceID, String nodePath) throws DMException { Session session = this.getHibernateSession(); try {//from w w w.j a v a2 s . c o m Device device = this.getDeviceByID(deviceID); if (device == null) { throw new DMException("Could not found the device: " + deviceID); } DeviceTree dTree = device.getDeviceTree(); if (dTree == null) { DeviceTreeNodeEntity rootNode = new DeviceTreeNodeEntity(".", null); session.saveOrUpdate(rootNode); dTree = new DeviceTreeEntity(rootNode); session.saveOrUpdate(dTree); device.setDeviceTree(dTree); session.saveOrUpdate(device); } // erase the "." prefix if (nodePath.startsWith(".")) { nodePath = nodePath.substring(1, nodePath.length()); } // erase the "/" prefix if (nodePath.startsWith("/")) { nodePath = nodePath.substring(1, nodePath.length()); } DeviceTreeNode node = this.findDeviceTreeNode(session, dTree.getRootNode(), nodePath); return node; } catch (HibernateException e) { throw new DMException(e); } finally { } }
From source file:com.npower.dm.hibernate.management.ModelClassificationBeanImpl.java
License:Open Source License
public void update(ModelClassification modelClassification) throws DMException { Session session = this.getHibernateSession(); session.saveOrUpdate(modelClassification); }
From source file:com.npower.dm.hibernate.management.ModelClassificationBeanImpl.java
License:Open Source License
public void update(ModelClassification modelClassification, List<Model> models) throws DMException { Session session = this.getHibernateSession(); session.saveOrUpdate(modelClassification); for (AbstractPredefinedModelSelector pe : ((AbstractModelClassification) modelClassification) .getPredefinedModelSelectors()) { session.delete(pe);/*from w w w . j av a2s .co m*/ } if (models != null) { for (Model model : models) { PredefinedModelSelectorEntity pe = new PredefinedModelSelectorEntity(); pe.setModel(model); pe.setModelClassification((ModelClassificationEntity) modelClassification); session.save(pe); } } }
From source file:com.npower.dm.hibernate.management.ModelManagementBeanImpl.java
License:Open Source License
public void update(Model model) throws DMException { if (model == null) { throw new NullPointerException("Could not add a null model into database."); }//from w w w. j a v a 2 s . c om Manufacturer manufacturer = model.getManufacturer(); if (manufacturer == null) { throw new DMException("add a model with a null manufacturer."); } Session session = this.getHibernateSession(); try { // Link the model to manufactuer boolean isNew = (model.getID() == 0) ? true : false; if (isNew) { Set<Model> set = manufacturer.getModels(); set.add(model); } ((ModelEntity) model).setLastUpdatedTime(new Date()); session.saveOrUpdate(model); } catch (HibernateException e) { throw new DMException(e); } finally { if (session != null) { } } }