import com.atlassian.jira.component.ComponentAccessor
import com.deiser.jira.profields.ao.v2.layout.LayoutMgr
import com.deiser.jira.profields.ao.v2.viewitem.AOViewItem
import com.deiser.jira.profields.ao.v2.viewitem.ViewItemMgr
import com.deiser.jira.profields.api.layout.view.SectionView
import com.deiser.jira.profields.api.layout.view.ViewItem
import com.google.common.collect.Lists
import groovy.transform.Field
import org.apache.commons.collections.ListUtils
import java.util.stream.Collectors
@Field ViewItemMgr viewItemMgr = ComponentAccessor.getOSGiComponentInstanceOfType(ViewItemMgr.class)
@Field LayoutMgr layoutMgr = ComponentAccessor.getOSGiComponentInstanceOfType(LayoutMgr.class)
@Field List println = []
execute()
//EXECUTE
def execute(){
def layouts = layoutMgr.get()
layouts.each {
iterateByLayout(it)
}
return println
}
//LAYOUTS
def iterateByLayout(layout){
def sourceItems = viewItemMgr.getByLayout(layout)
checkAndFix(layout, sourceItems)
}
def checkAndFix(layout, sourceItems){
//Get all structure
def structure = getAllSections(sourceItems)
addToPrint(layout, structure)
//Remove all duplicated sections
def duplicatedSections = removeAllDuplicatedSections(structure)
addToPrintDeletedDuplicatedSections(duplicatedSections)
//Remove all orphan
def deletedOrphans = removeUntilThereAreNoOrphans()
addToPrintDeletedOrphans(deletedOrphans)
//Split
addToPrintSplit()
}
//SECTIONS
def getAllSections(sourceItems){
return sourceItems.stream()
.filter{it -> isSection(it)}
.map{it -> new ViewNode(item: it, children: getAllColumns(it, sourceItems), type: "Section")}
.collect(Collectors.toList())
}
boolean isSection(viewItem){
return viewItem.parentId == null
}
int compareSection(viewNode1, viewNode2){
boolean comparative = viewNode1.item.name == viewNode2.item.name &&
viewNode1.item.position == viewNode2.item.position &&
((viewNode1.children == null && viewNode2.children == null) ||
(viewNode1.children.size() == viewNode2.children.size()))
return comparative ? 0 : 1
}
def removeAllDuplicatedSections(structure){
def sectionsUniques = structure.toUnique{ sect1, sect2 -> compareSection(sect1, sect2) }
def thereAreDuplicatedSections = structure.size() != sectionsUniques.size();
if(thereAreDuplicatedSections){
def repeatedSections = ListUtils.removeAll(structure, sectionsUniques)
def repeatedViewItemSections = repeatedSections.stream().map { it -> it.item }.collect(Collectors.toList())
this.viewItemMgr.delete(repeatedViewItemSections)
return repeatedViewItemSections
}
return [];
}
//COLUMNS
def getAllColumns(section, sourceItems){
return columns = sourceItems.stream()
.filter{ it -> isColumn(section, it) }
.map{it-> new ViewNode(item: it, children: getAllContainer(it, sourceItems), type: "Column")}
.collect(Collectors.toList())
.sort{ a -> a.item.position}
}
boolean isColumn(section, viewItem){
return viewItem.parentId != null &&
viewItem.parentId == section.ID &&
viewItem.name == null &&
viewItem.fieldId == null
}
//CONTAINERS
def getAllContainer(column, sourceItems){
return sourceItems.stream()
.filter{it -> isContainer(column, it)}
.map{it -> new ViewNode(item: it, children: getAllFields(it, sourceItems), type: "Container")}
.collect(Collectors.toList())
.sort{ a -> a.item.position}
}
boolean isContainer(column, viewItem){
return viewItem.parentId != null &&
viewItem.parentId == column.ID &&
viewItem.name
}
//FIELDS
def getAllFields(container, sourceItems){
return sourceItems.stream()
.filter{it -> isField(container, it)}
.map{it -> new ViewNode(item: it, children: null, type: "Field")}
.collect(Collectors.toList())
.sort{ a -> a.item.position}
}
def isField(container, viewItem){
return viewItem.parentId != null &&
viewItem.parentId == container.ID &&
viewItem.getFieldId() != null
}
class ViewNode {
AOViewItem item
List<ViewNode> children;
String type;
def compare(ViewNode node){
return item.name == node.item.name &&
item.position == node.item.position &&
item.fieldId == node.item.fieldId &&
item.required == node.item.required &&
item.layout.ID == node.item.layout.ID &&
compareChildren(node.children)
}
def compareChildren(List<AOViewItem> nodeChildren){
if(children == null && nodeChildren == null) return true;
if(children.size() != nodeChildren.size()) return false;
def compareResult = true
children.eachWithIndex { ViewNode item, int index ->
compareResult &= item.compare(nodeChildren.get(index))
if(compareResult == false) return false
}
return compareResult
}
def printToString(){
def itemString = item.position + " ID["+ item.ID +"] NAME["+ item.name +"] PARENT-ID["+ item.parentId +"] FIELD-ID["+ item.fieldId +"] <br/>"
def childrenString = this.children == null
? ""
: this.children.stream()
.map{it -> it.printToString()}
.collect(Collectors.joining(""))
def tab = ""
switch(type){
case "Section":
break;
case "Column":
tab += " "
break;
case "Container":
tab += " " + " "
break;
case "Field":
tab += " " + " " + " " + " "
break;
}
return tab + type + " --> " + itemString + childrenString
}
}
//REMOVE ORPHAN
def removeUntilThereAreNoOrphans(){
def hasOrphan = true
def printlnToOrphans = []
while(hasOrphan){
hasOrphan = removeOrphan(printlnToOrphans)
}
return printlnToOrphans
}
def removeOrphan(println){
def hasRemoved = false
viewItemMgr.get().each {
def parentId = it.getParentId()
if(parentId != null && parentId >= 0){
if(viewItemMgr.get(parentId) == null){
println.add( "ID: "+ it.ID +", Name: "+ it.name +" / ")
viewItemMgr.delete(it)
hasRemoved = true
}
}
}
return hasRemoved
}
//PRINTS
def addToPrint(layout, structure){
println.add("Layout ("+ layout.ID +") <b>"+ layout.name +"</b><br/>Structure:<br/>")
println.add(structure.stream().map{it -> it.printToString()}.collect(Collectors.toList()))
}
def addToPrintDeletedDuplicatedSections(duplicatedSections){
if(duplicatedSections){
def prefix = "<br/> <b>Deleted Sections:</b>";
def duplicatedString = duplicatedSections.stream().map{it -> "ID: "+ it.ID +", Name: "+ it.name +" / "}.collect(Collectors.joining(" / "))
println.add(prefix + duplicatedString + "<br/>")
}
}
def addToPrintSplit(){
println.add(" ------------ ------------ ------------ ------------ ------------ ------------ <br/>")
}
def addToPrintDeletedOrphans(deletedOrphans){
if(!deletedOrphans.isEmpty()){
println.add("<b>Orphans Deleted:</b> " + deletedOrphans + "<br/>")
}
} |