@Transactional(readOnly = false) public void save(T entity) { @SuppressWarnings("unchecked") Class<T> entityClass = Reflections.getClassGenricType(getClass(), 1); // 如果没有设置父节点,则代表为跟节点,有则获取父节点实体 if (entity.getParent() == null || StringUtils.isBlank(entity.getParentId()) || "0".equals(entity.getParentId())) { entity.setParent(null); } else { entity.setParent(super.get(entity.getParentId())); } if (entity.getParent() == null) { T parentEntity = null; try { parentEntity = entityClass.getConstructor(String.class).newInstance("0"); } catch (Exception e) { throw new ServiceException(e); } entity.setParent(parentEntity); entity.getParent().setParentIds(StringUtils.EMPTY); } // 获取修改前的parentIds,用于更新子节点的parentIds String oldParentIds = entity.getParentIds(); // 设置新的父节点串 entity.setParentIds(entity.getParent().getParentIds() + entity.getParent().getId() + ","); // 保存或更新实体 super.save(entity); // 更新子节点 parentIds T o = null; try { o = entityClass.newInstance(); } catch (Exception e) { throw new ServiceException(e); } o.setParentIds("%," + entity.getId() + ",%"); List<T> list = dao.findByParentIdsLike(o); for (T e : list) { if (e.getParentIds() != null && oldParentIds != null) { e.setParentIds(e.getParentIds().replace(oldParentIds, entity.getParentIds())); preUpdateChild(entity, e); dao.updateParentIds(e); } } }
/** * Add a child node to a parent * * @param parent * @param child * @return */ public <T extends TreeNode> T addNewChild(T parent, T child) { if (!parent.getChildren().contains(child)) { parent.getChildren().add(child); child.setParent(parent); } update(parent); return (T) parent.getChildren().getLast(); }
/** * Adds a new node to this element. * * @param <T> the type of the node to add * @param child the child to add * @return the added child */ public <T extends ERXML.Node> T add(T child) { child.setParent(this); if (_children == null) { _children = new LinkedList<Node>(); } _children.add(child); return child; }
/** Test of setParent method, of class AbstractSqlTemplateLoader. */ @Test public void testSetParent() { assertThat(instance.load("123", "aaa"), is(nullValue())); SqlTemplateLoader parent = mock(SqlTemplateLoader.class); SqlTemplate templ = mock(SqlTemplate.class); when(parent.load("123", "aaa", UTF_8)).thenReturn(templ); instance.setParent(parent); assertThat(instance.load("123", "aaa"), is(sameInstance(templ))); }
@SuppressWarnings("unchecked") @Override public IntervalAndSubIntervals<T> clone() { IntervalAndSubIntervals<T> copy = (IntervalAndSubIntervals<T>) super.clone(); copy.reset(); for (T m : this) { T mcopy = (T) m.clone(); mcopy.setParent(copy); copy.add(mcopy); } return copy; }
public T build(T parent, String code, String name) { try { T t = clTask.newInstance(); t.setParent(parent); t.setCode(code); t.setName(name); return t; } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } throw new RuntimeException("x"); }
/** Apply a variant. */ @Override @SuppressWarnings("unchecked") public IntervalAndSubIntervals<T> apply(Variant variant) { if (!shouldApply(variant)) return this; IntervalAndSubIntervals<T> newMarker = (IntervalAndSubIntervals<T>) super.apply(variant); if (newMarker == null) return null; newMarker.reset(); for (T m : this) { T mcopy = (T) m.apply(variant); // Do not add if interval is completely removed if (mcopy != null) { mcopy.setParent(newMarker); newMarker.add(mcopy); } } return newMarker; }
public <T extends SceneNodeComponent & Poolable> T newComponent(Class<T> componentType) { T component = PoolService.obtain(componentType); component.setParent(this); return component; }
public void addNode(T node) { node.setParent(this); children.add(node); }
public void setParent(Composite parent) { scrollable.setParent(parent); }