Skip to content

Instantly share code, notes, and snippets.

@apolzon
Created March 11, 2010 18:02
Show Gist options
  • Save apolzon/329441 to your computer and use it in GitHub Desktop.
Save apolzon/329441 to your computer and use it in GitHub Desktop.
/**
* This method is used by getAllBookUnitWithDepthRow()
*
* @param bookUnit
* @param delete
* @return List of bookunit with depth
*/
private List<BookUnitWithDepthRow> getAllBookUnitWithDepth(BookUnit bookUnit, boolean delete)
{
bookUnit = fetchBookUnitFromSessionBook(bookUnit);
List<BookUnitWithDepthRow> returnVal = null;
if (bookUnit.getUnitType() == BookUnitType.BOOK)
{
Book book = (Book) bookUnit;
BookUnitWithDepthRow row = new BookUnitWithDepthRow(0, bookUnit);
List<Section> listOfSections = new ArrayList<Section>();
for (Section sec : book.getSections())
{
if (!sec.isDeleted())
{
listOfSections.add(sec);
}
else if (delete)
{
listOfSections.add(sec);
}
}
returnVal = new ArrayList<BookUnitWithDepthRow>();
returnVal.add(row);
if (listOfSections != null)
{
for (Section section : listOfSections)
{
if (!returnVal.isEmpty())
{
if (returnVal.get(returnVal.size() - 1).getDepth() > 1)
{
int i = 0;// this gives number of special hidden
// section to be filled in
if (returnVal.get(returnVal.size() - 1).getBookUnit().getUnitType() == BookUnitType.SECTION)
{
// Value of i if the last bookUnit in list is
// Section
// Number of section to be printed is 1 less
// than the depth of last bookUnit on list
i = returnVal.get(returnVal.size() - 1).getDepth() - 1;
}
else
{
// Value of i if last bookUnit on list is
// chapter
// Number od section to be printed is 2 less
// than the depth of last bookUnit in list
i = returnVal.get(returnVal.size() - 1).getDepth() - 2;
}
while (i >= 1)
{
HiddenSection hiddenSection=this.createSpecialHiddenSection();
hiddenSection.setParent(this.findParentBookUnit(returnVal, i + 1));
hiddenSection.setSortOrder(this.lastSortOrderOfParent(hiddenSection.getParent()) + 1);
BookUnitWithDepthRow temprow = new BookUnitWithDepthRow(i + 1, hiddenSection);
returnVal.add(temprow);
i--;
}
if (section.isHidden())
{
HiddenSection defaultHiddenSection = this.createHiddenSection();
defaultHiddenSection.setParent(book);
defaultHiddenSection.setUnitTitle("special hidden section");
defaultHiddenSection.setSortOrder(this.getLastSortOrder(book) + 1);
BookUnitWithDepthRow temprow1 = new BookUnitWithDepthRow(1, defaultHiddenSection);
returnVal.add(temprow1);
}
}
returnVal = this.getAllBookUnitWithDepthInternal(section, 1, returnVal, delete);
}
}
if (!returnVal.isEmpty())
{
if (returnVal.get(returnVal.size() - 1).getDepth() > 1)
{
int i = 0;// this gives number of special hidden section
// to be filled in
if (returnVal.get(returnVal.size() - 1).getBookUnit().getUnitType() == BookUnitType.SECTION)
{
// Value of i if the last bookUnit in list is
// Section
// Number of section to be printed is 1 less than
// the depth of last bookUnit on list
i = returnVal.get(returnVal.size() - 1).getDepth() - 1;
}
else
{
// Value of i if last bookUnit on list is chapter
// Number od section to be printed is 2 less than
// the depth of last bookUnit in list
i = returnVal.get(returnVal.size() - 1).getDepth() - 2;
}
while (i >= 1)
{
HiddenSection hiddenSection=this.createSpecialHiddenSection();
hiddenSection.setParent(this.findParentBookUnit(returnVal, i + 1));
hiddenSection.setSortOrder(this.lastSortOrderOfParent(hiddenSection.getParent()) + 1);
BookUnitWithDepthRow temprow = new BookUnitWithDepthRow(i + 1, hiddenSection);
returnVal.add(temprow);
i--;
}
}
}
// /Added default special hidden section to print default target
// line
HiddenSection defaultHiddenSection=this.createHiddenSection();
defaultHiddenSection.setParent(book);
defaultHiddenSection.setUnitTitle("special hidden section");
defaultHiddenSection.setSortOrder(this.getLastSortOrder(book) + 1);
BookUnitWithDepthRow temprow1 = new BookUnitWithDepthRow(1, defaultHiddenSection);
returnVal.add(temprow1);
}
else
{
// /Do Nothing
}
}
return returnVal;
}
/**
* This method provide bookunit with their depths used by
* getAllBookUnitWithDepth()
*
* @param bookUnit
* @param depth
* @param listOfBookUnits
* @param deleted
* @return list of bookunits with their depth
*/
private List<BookUnitWithDepthRow> getAllBookUnitWithDepthInternal(BookUnit bookUnit, int depth, List<BookUnitWithDepthRow> listOfBookUnits, boolean deleted)
{
bookUnit = fetchBookUnitFromSessionBook(bookUnit);
if (bookUnit.getUnitType() == BookUnitType.SECTION)
{
if (!listOfBookUnits.isEmpty())
{
if (listOfBookUnits.get(listOfBookUnits.size() - 1).getDepth() - depth > 1)
{
int i = listOfBookUnits.get(listOfBookUnits.size() - 1).getDepth();
while (i > depth)
{
HiddenSection hiddenSection = this.createSpecialHiddenSection();
hiddenSection.setParent(this.findParentBookUnit(listOfBookUnits, i));
hiddenSection.setSortOrder(this.lastSortOrderOfParent(hiddenSection.getParent()) + 1);
BookUnitWithDepthRow temprow = new BookUnitWithDepthRow(i, hiddenSection);
listOfBookUnits.add(temprow);
i--;
}
}
else if (listOfBookUnits.get(listOfBookUnits.size() - 1).getDepth() - depth == 1)
{
if (!(listOfBookUnits.get(listOfBookUnits.size() - 1).getBookUnit().getUnitType() == BookUnitType.CHAPTER))
{
int i = listOfBookUnits.get(listOfBookUnits.size() - 1).getDepth() - 1;
HiddenSection hiddenSection = this.createSpecialHiddenSection();
hiddenSection.setParent(this.findParentBookUnit(listOfBookUnits, i + 1));
hiddenSection.setSortOrder(this.lastSortOrderOfParent(hiddenSection.getParent()) + 1);
BookUnitWithDepthRow temprow = new BookUnitWithDepthRow(i + 1, hiddenSection);
listOfBookUnits.add(temprow);
}
}
}
Section section = (Section) bookUnit;
if (!bookUnit.getDeletedStatus())
{
BookUnitWithDepthRow row = new BookUnitWithDepthRow(depth, bookUnit);
listOfBookUnits.add(row);
}
else if (deleted)
{
BookUnitWithDepthRow row = new BookUnitWithDepthRow(depth, bookUnit);
listOfBookUnits.add(row);
}
List<BookUnit> bookUnits = new ArrayList<BookUnit>();
bookUnits.addAll(section.getSubsections());
bookUnits.addAll(section.getChapters());
PropertyComparator.sort(bookUnits, new MutableSortDefinition("sortOrder", true, true));
for (BookUnit unit : bookUnits)
{
if ((unit.getUnitType() == BookUnitType.SECTION))
{
listOfBookUnits = this.getAllBookUnitWithDepthInternal(unit, depth + 1, listOfBookUnits, deleted);
}
else if ((unit.getUnitType() == BookUnitType.CHAPTER))
{
int depthAdj = 1;
if (unit.getParent().isHidden())
{
depthAdj -= 1;
}
listOfBookUnits = this.getAllBookUnitWithDepthInternal(unit, depth + depthAdj, listOfBookUnits, deleted);
}
}
return listOfBookUnits;
}
else if (bookUnit.getUnitType() == BookUnitType.CHAPTER)
{
if (!listOfBookUnits.isEmpty())
{
if (listOfBookUnits.get(listOfBookUnits.size() - 1).getDepth() - depth > 1)
{
// int i = listOfBookUnits.get(listOfBookUnits.size()-
// 1).getDepth()-2;
int i = 0;
if (listOfBookUnits.get(listOfBookUnits.size() - 1).getBookUnit().getUnitType() == BookUnitType.SECTION)
{
i = listOfBookUnits.get(listOfBookUnits.size() - 1).getDepth();
}
else if (listOfBookUnits.get(listOfBookUnits.size() - 1).getBookUnit().getUnitType() == BookUnitType.CHAPTER)
{
i = listOfBookUnits.get(listOfBookUnits.size() - 1).getDepth() - 2;
}
while (i >= depth)
{
HiddenSection hiddenSection = this.createSpecialHiddenSection();
hiddenSection.setParent(this.findParentBookUnit(listOfBookUnits, i));
hiddenSection.setSortOrder(this.lastSortOrderOfParent(hiddenSection.getParent()) + 1);
BookUnitWithDepthRow temprow = new BookUnitWithDepthRow(i, hiddenSection);
listOfBookUnits.add(temprow);
i--;
}
}
else if (listOfBookUnits.get(listOfBookUnits.size() - 1).getDepth() - depth == 1)
{
if (listOfBookUnits.get(listOfBookUnits.size() - 1).getBookUnit().getUnitType() == BookUnitType.CHAPTER)
{
int i = listOfBookUnits.get(listOfBookUnits.size() - 1).getDepth() - 1;
HiddenSection hiddenSection = this.createSpecialHiddenSection();
hiddenSection.setParent(this.findParentBookUnit(listOfBookUnits, i));
hiddenSection.setSortOrder(this.lastSortOrderOfParent(hiddenSection.getParent()) + 1);
BookUnitWithDepthRow temprow = new BookUnitWithDepthRow(i, hiddenSection);
listOfBookUnits.add(temprow);
}
else
{
for (int a = 1; a <= 2; a++)
{
int i = listOfBookUnits.get(listOfBookUnits.size() - 1).getDepth() - a;
HiddenSection hiddenSection = this.createSpecialHiddenSection();
hiddenSection.setParent(this.findParentBookUnit(listOfBookUnits, i + 1));
hiddenSection.setSortOrder(this.lastSortOrderOfParent(hiddenSection.getParent()) + 1);
BookUnitWithDepthRow temprow = new BookUnitWithDepthRow(i + 1, hiddenSection);
listOfBookUnits.add(temprow);
}
}
}
}
if (!bookUnit.getDeletedStatus())
{
BookUnitWithDepthRow row = new BookUnitWithDepthRow(depth, bookUnit);
listOfBookUnits.add(row);
}
else if (deleted)
{
BookUnitWithDepthRow row = new BookUnitWithDepthRow(depth, bookUnit);
listOfBookUnits.add(row);
}
return listOfBookUnits;
}
return null;
}
/**
* Returns bookunt with depth , non deleted bookunit or deleted if required
*
* @param bookUnit
* @param true or false
* @return List of bookunit with depth
*/
public List<BookUnitWithDepthRow> getAllBookUnitWithDepthRow(boolean delete, EmssUser emssUser)
{
List<BookUnitWithDepthRow> tempList = this.getAllBookUnitWithDepth(this.getBookSessionHelper().getBook(), delete);
this.getBookSessionHelper().getBook().getAllBookUnits();
this.isAssignedUser(emssUser);
return tempList;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment