if (context == null) {
return new ArrayList<ChoiceListValue>();
}
JCRNodeWrapper node = (JCRNodeWrapper) context.get("contextNode");
ExtendedNodeType realNodeType = (ExtendedNodeType) context.get("contextType");
String propertyName = context.containsKey("dependentProperties") ? ((List<String>)context.get("dependentProperties")).get(0) : null;
SortedSet<View> views = new TreeSet<View>();
try {
final List<String> nodeTypeList = new ArrayList<String>();
String nextParam = "";
if (param.contains(",")) {
nextParam = StringUtils.substringAfter(param, ",");
param = StringUtils.substringBefore(param, ",");
}
if ("subnodes".equals(param)) {
if (propertyName == null) {
propertyName = "j:allowedTypes";
}
if (context.containsKey(propertyName)) {
List<String> types = (List<String>)context.get(propertyName);
for (String type : types) {
nodeTypeList.add(type);
}
} else if (node != null && node.hasProperty(propertyName)) {
JCRPropertyWrapper property = node.getProperty(propertyName);
if (property.isMultiple()) {
Value[] types = property.getValues();
for (Value type : types) {
nodeTypeList.add(type.getString());
}
} else {
nodeTypeList.add(property.getValue().getString());
}
} else if (node != null && !"j:allowedTypes".equals(propertyName) && node.hasProperty("j:allowedTypes")) {
Value[] types = node.getProperty("j:allowedTypes").getValues();
for (Value type : types) {
nodeTypeList.add(type.getString());
}
} else if (node !=null) {
// No restrictions get node type list from already existing nodes
NodeIterator nodeIterator = node.getNodes();
while (nodeIterator.hasNext()) {
Node next = (Node) nodeIterator.next();
String name = next.getPrimaryNodeType().getName();
if (!nodeTypeList.contains(name)) {
nodeTypeList.add(name);
}
}
}
param = nextParam;
} else if ("reference".equals(param)) {
if (propertyName == null) {
propertyName = "j:node";
}
if (context.containsKey(propertyName)) {
JCRSessionWrapper session = JCRSessionFactory.getInstance().getCurrentUserSession();
List<String> refNodeUuids = (List<String>)context.get(propertyName);
for (String refNodeUuid : refNodeUuids) {
try {
JCRNodeWrapper refNode = (JCRNodeWrapper) session.getNodeByUUID(refNodeUuid);
nodeTypeList.addAll(refNode.getNodeTypes());
} catch (Exception e) {
logger.warn("Referenced node not found to retrieve its nodetype for initializer", e);
}
}
} else if (node != null && node.hasProperty(propertyName)) {
try {
JCRNodeWrapper refNode = (JCRNodeWrapper) node.getProperty(propertyName).getNode();
nodeTypeList.addAll(refNode.getNodeTypes());
} catch (ItemNotFoundException e) {
}
} else if (node != null && !"j:node".equals(propertyName) && node.hasProperty("j:node")) {
try {
JCRNodeWrapper refNode = (JCRNodeWrapper) node.getProperty("j:node")
.getNode();
nodeTypeList.addAll(refNode.getNodeTypes());
} catch (ItemNotFoundException e) {
}
}
param = nextParam;
} else if ("mainresource".equals(param)) {
JCRNodeWrapper matchingParent;
JCRNodeWrapper parent;
if (node == null) {
parent = (JCRNodeWrapper) context.get("contextParent");
} else {
parent = node.getParent();
}
try {
while (true) {
if (parent.isNodeType("jnt:template")) {
matchingParent = parent;
break;
}
parent = parent.getParent();
}
if (matchingParent.hasProperty("j:applyOn")) {
Value[] vs = matchingParent.getProperty("j:applyOn").getValues();
for (Value v : vs) {
nodeTypeList.add(v.getString());
}
}
} catch (ItemNotFoundException e) {
}
if (nodeTypeList.isEmpty()) {
nodeTypeList.add("jnt:page");
}
param = nextParam;
} else if (param != null && param.indexOf(":") > 0) {
nodeTypeList.add(param);
param = nextParam;
} else {
if (node != null) {
nodeTypeList.addAll(node.getNodeTypes());
} else if (realNodeType != null) {
nodeTypeList.add(realNodeType.getName());
}
}
if (nodeTypeList.isEmpty()) {
nodeTypeList.add("nt:base");