public void Down(String pk) // Troca nó de posição com o irmão posterior ficando mais afastado da mãe { long irmaoposterior = 0; try { // Irmão posterior (mais afastado da mãe) byte[] b = GetHid(pk); SqlHierarchyId h = Conversions.Bytes2HierarchyId(b); foreach (var t in repo.Get.OrderBy(p => p.hid)) { SqlHierarchyId hid = Conversions.Bytes2HierarchyId(t.hid); if (Conversions.Bytes2HierarchyId(t.hid) > h && hid.GetAncestor(1) == h.GetAncestor(1)) { irmaoposterior = (long)t.GetType().GetProperty(PKName).GetValue(t, null); break; } } if (irmaoposterior > 0) { TrocaPosicaoFilhos(pk, irmaoposterior.ToString()); } } catch (Exception) { } }
public void Demote(String pk) // Transfere nó para nível imediatamente superior (número do nível) // (como filho do irmão anterior) { long novaMae = 0; try { // Irmão anterior (mais próximo à mãe) byte[] b = GetHid(pk); SqlHierarchyId h = Conversions.Bytes2HierarchyId(b); foreach (var t in repo.Get.OrderByDescending(p => p.hid)) { SqlHierarchyId hid = Conversions.Bytes2HierarchyId(t.hid); if (Conversions.Bytes2HierarchyId(t.hid) < h && hid.GetAncestor(1) == h.GetAncestor(1)) { novaMae = (long)t.GetType().GetProperty(PKName).GetValue(t, null); break; } } if (novaMae == 0) { throw new Exception(""); } MovSubTree(pk, novaMae.ToString()); } catch (Exception ex) { throw new Exception("Não é possível o rebaixamento de nível: não existe irmão mais próximo à mãe." + ex.Message); } }
public void Promote(String pk) { // Transfere nó para nível imediatamente inferior (número do nível) try { byte[] b = GetHid(pk); SqlHierarchyId h = Conversions.Bytes2HierarchyId(b); if (h.GetLevel() < 2) { throw new Exception("Não é possível a promoção de nível 1 ou 0."); } long novamae = 0; byte[] grandP = Conversions.HierarchyId2Bytes(h.GetAncestor(2)); foreach (var t in repo.Get) { byte[] bh = t.hid; if (bh.SequenceEqual(grandP)) { novamae = (long)t.GetType().GetProperty(PKName).GetValue(t, null); break; } } MovSubTree(pk, novamae.ToString()); } catch (Exception ex) { throw new Exception("Erro na promoção: " + ex.Message); } }
public static IEnumerable ListAncestors(SqlHierarchyId h) { while (!h.IsNull) { yield return(h); h = h.GetAncestor(1); } }
private static SqlHierarchyId Previous <T>(SqlHierarchyId node) where T : TreeEntity { using (ExecutionMode.Global()) return(Database.Query <T>() .Select(t => (SqlHierarchyId?)t.Route) .Where(n => (bool)(n !.Value.GetAncestor(1) == node.GetAncestor(1)) && (bool)(n.Value < node)) .OrderByDescending(n => n).FirstOrDefault() ?? SqlHierarchyId.Null); }
public static HierarchyId CommonAncestor( SqlHierarchyId h1, HierarchyId h2 ) { while (!h1.IsDescendantOf(h2)) { h1 = h1.GetAncestor(1); } return(h1); }
public byte[] GetNextSonHid(String mae) // Obtém hid do próximo novo filho { byte[] b = null; try { // obter hid da mãe if (string.IsNullOrEmpty(mae)) { b = Conversions.HierarchyId2Bytes(SqlHierarchyId.GetRoot()); } else { SqlHierarchyId maeHid = Conversions.Bytes2HierarchyId(repo.Find(new object[] { long.Parse(mae) }).hid); long m = long.Parse(mae); // byte[] LastChildHid = null; // = repo.Get.Where(p => p.Parent == m).Select(p => p.hid).Max(); SqlHierarchyId lastChHid = SqlHierarchyId.Null; foreach (T r in repo.Get) { SqlHierarchyId h = Conversions.Bytes2HierarchyId(r.hid); if (h.GetAncestor(1) == maeHid) { if (lastChHid.IsNull || h > lastChHid) { lastChHid = h; } } } if (lastChHid == SqlHierarchyId.Null) { b = Conversions.HierarchyId2Bytes(maeHid.GetDescendant(SqlHierarchyId.Null, SqlHierarchyId.Null)); } else { b = Conversions.HierarchyId2Bytes(maeHid.GetDescendant(lastChHid, SqlHierarchyId.Null)); } } } catch (Exception ex) { throw new Exception("Erro em GetNextSonHid: " + ex.Message); } return(b); }
public static SqlHierarchyId NextSibling(this SqlHierarchyId sibling) { return(sibling.GetAncestor(1).GetDescendant(sibling, SqlHierarchyId.Null)); }