@Override
protected void doService(ToolPageContext page) throws IOException, ServletException {
List<ObjectType> types = new ArrayList<ObjectType>(Database.Static.getDefault().getEnvironment().getTypes());
UUID selectedId = page.param(UUID.class, "typeId");
ObjectType selected = null;
ContentType sct = null;
for (ObjectType t : types) {
if (t.getId().equals(selectedId)) {
selected = t;
break;
}
}
if (selected != null) {
sct = Query.from(ContentType.class).where("internalName = ?", selected.getInternalName()).first();
if (page.isFormPost()) {
if (sct == null) {
sct = new ContentType();
sct.getState().setId(page.param(UUID.class, "id"));
}
if (page.tryStandardUpdate(sct)) {
return;
}
} else if (sct == null) {
sct = new ContentType();
sct.setDisplayName(selected.getDisplayName());
sct.setInternalName(selected.getInternalName());
}
for (ObjectField of : selected.getFields()) {
ToolUi ui = of.as(ToolUi.class);
if (!ui.isHidden()) {
boolean found = false;
for (ContentField cf : sct.getFields()) {
if (of.getInternalName().equals(cf.getInternalName())) {
found = true;
break;
}
}
if (!found) {
ContentField cf = new ContentField();
cf.setTab(ui.getTab());
cf.setDisplayName(of.getDisplayName());
cf.setInternalName(of.getInternalName());
sct.getFields().add(cf);
}
}
}
for (Iterator<ContentField> i = sct.getFields().iterator(); i.hasNext();) {
ContentField cf = i.next();
boolean found = false;
for (ObjectField of : selected.getFields()) {
if (of.getInternalName().equals(cf.getInternalName())) {