@SuppressWarnings("unchecked")
public void importLayout(org.dom4j.Element layout) {
String ownerUsername = layout.valueOf("@username");
IPerson person = null;
UserProfile profile = null;
try {
person = new PersonImpl();
person.setUserName(ownerUsername);
int ownerId = identityStore.getPortalUID(person);
if (ownerId == -1) {
String msg = "No userId for username=" + ownerUsername;
throw new RuntimeException(msg);
}
person.setID(ownerId);
person.setSecurityContext(new BrokenSecurityContext());
profile = this.getUserProfileByFname(person, "default");
} catch (Throwable t) {
String msg = "Unrecognized user " + person.getUserName() + "; you must import users before their layouts.";
throw new RuntimeException(msg, t);
}
// (6) Add database Ids & (5) Add dlm:plfID ...
int nextId = 1;
for (Iterator<org.dom4j.Element> it = (Iterator<org.dom4j.Element>) layout.selectNodes("folder | dlm:*").iterator(); it.hasNext();) {
nextId = addIdAttributesIfNecessary(it.next(), nextId);
}
// Now update UP_USER...
final SimpleJdbcTemplate jdbcTemplate = new SimpleJdbcTemplate(RDBMServices.getDataSource());
jdbcTemplate.update("UPDATE up_user SET next_struct_id = ? WHERE user_id = ?", nextId, person.getID());
// (4) Convert external DLM pathrefs to internal form (noderefs)...
for (Iterator<org.dom4j.Attribute> itr = (Iterator<org.dom4j.Attribute>) layout.selectNodes("//@dlm:origin").iterator(); itr.hasNext();) {
org.dom4j.Attribute a = itr.next();
String noderef = getDlmNoderef(ownerUsername, a.getValue(), null, true, layout);
if (noderef != null) {
// Change the value only if we have a valid pathref...
a.setValue(noderef);
// For dlm:origin only, also use the noderef as the ID attribute...
a.getParent().addAttribute("ID", noderef);
}
}
for (Iterator<org.dom4j.Attribute> itr = (Iterator<org.dom4j.Attribute>) layout.selectNodes("//@dlm:target").iterator(); itr.hasNext();) {
org.dom4j.Attribute a = itr.next();
String noderef = getDlmNoderef(ownerUsername, a.getValue(), null, true, layout);
if (noderef != null) {
// Change the value only if we have a valid pathref...
a.setValue(noderef);
}
}
for (Iterator<org.dom4j.Attribute> names = (Iterator<org.dom4j.Attribute>) layout.selectNodes("//dlm:*/@name").iterator(); names.hasNext();) {
org.dom4j.Attribute a = names.next();
org.dom4j.Attribute fname = a.getParent().attribute("fname");
String noderef = null;
if (fname != null) {
noderef = getDlmNoderef(ownerUsername, a.getValue(), fname.getValue(), false, layout);
// Remove the fname attribute now that we're done w/ it...
fname.getParent().remove(fname);
} else {
noderef = getDlmNoderef(ownerUsername, a.getValue(), null, true, layout);
}
if (noderef != null) {
// Change the value only if we have a valid pathref...
a.setValue(noderef);
}
}
// (3) Restore chanID attributes on <channel> elements...
for (Iterator<org.dom4j.Element> it = (Iterator<org.dom4j.Element>) layout.selectNodes("//channel").iterator(); it.hasNext();) {
org.dom4j.Element c = it.next();
final String fname = c.valueOf("@fname");
IChannelDefinition cd = this.channelRegistryStore.getChannelDefinition(fname);
if (cd == null) {
throw new IllegalArgumentException("No published channel for fname=" + fname + " referenced by layout for " + ownerUsername);
}
c.addAttribute("chanID", String.valueOf(cd.getId()));
}
// (2) Restore locale info...
// (This step doesn't appear to be needed for imports)
// (1) Process structure & theme attributes...
Document layoutDom = null;
try {
// UserPreferences up = this.getUserPreferences(person, profile);
UserPreferences up = new UserPreferences(profile);
up.setStructureStylesheetUserPreferences(this.getDistributedSSUP(person,
profile.getProfileId(), profile.getStructureStylesheetId()));
up.setThemeStylesheetUserPreferences(this.getDistributedTSUP(person,
profile.getProfileId(), profile.getThemeStylesheetId()));
// Structure Attributes.
boolean saSet = false;
StructureStylesheetUserPreferences ssup = up.getStructureStylesheetUserPreferences();
// ssup must be manually cleaned out.
for (Enumeration<String> names = (Enumeration<String>) ssup.getFolderAttributeNames(); names.hasMoreElements();) {
String n = names.nextElement();
for (Enumeration<String> fIds = (Enumeration<String>) ssup.getFolders(); fIds.hasMoreElements();) {
String f = fIds.nextElement();
if (ssup.getDefinedFolderAttributeValue(f, n) != null) {
ssup.removeFolder(f);
}
}
}
for (Enumeration<String> names = (Enumeration<String>) ssup.getChannelAttributeNames(); names.hasMoreElements();) {
String n = names.nextElement();
for (Enumeration<String> chds = (Enumeration<String>) ssup.getChannels(); chds.hasMoreElements();) {
String c = chds.nextElement();
if (ssup.getDefinedChannelAttributeValue(c, n) != null) {
ssup.removeChannel(c);
}
}
}
for (Iterator<org.dom4j.Element> it = (Iterator<org.dom4j.Element>) layout.selectNodes("//structure-attribute").iterator(); it.hasNext();) {
org.dom4j.Element sa = (org.dom4j.Element) it.next();
String idAttr = sa.getParent().valueOf("@ID");
if (sa.getParent().getName().equals("folder")) {
ssup.setFolderAttributeValue(idAttr, sa.valueOf("name"), sa.valueOf("value"));
saSet = true;
} else if (sa.getParent().getName().equals("channel")) {
ssup.setChannelAttributeValue(idAttr, sa.valueOf("name"), sa.valueOf("value"));
saSet = true;
} else {
String msg = "Unrecognized parent element for user preferences attribute: " + sa.getParent().getName();
throw new RuntimeException(msg);
}
// Remove these elements or else DLM will choke...
sa.getParent().remove(sa);
}
// Theme Attributes.
boolean taSet = false;
ThemeStylesheetUserPreferences tsup = up.getThemeStylesheetUserPreferences();
// tsup must be manually cleaned out.
for (Enumeration<String> names = (Enumeration<String>) tsup.getChannelAttributeNames(); names.hasMoreElements();) {
String n = names.nextElement();
for (Enumeration<String> chds = (Enumeration<String>) tsup.getChannels(); chds.hasMoreElements();) {
String c = chds.nextElement();
if (tsup.getDefinedChannelAttributeValue(c, n) != null) {
tsup.removeChannel(c);
}
}
}
for (Iterator<org.dom4j.Element> it = (Iterator<org.dom4j.Element>) layout.selectNodes("//theme-attribute").iterator(); it.hasNext();) {
org.dom4j.Element ta = (org.dom4j.Element) it.next();
String idAttr = ta.getParent().valueOf("@ID");
// Theme attributes are channels only...
tsup.setChannelAttributeValue(idAttr, ta.valueOf("name"), ta.valueOf("value"));
taSet = true;
// Remove these elements or else DLM will choke...
ta.getParent().remove(ta);
}
// From this point forward we need the user's PLF set as DLM expects it...
for (Iterator<org.dom4j.Text> it = (Iterator<org.dom4j.Text>) layout.selectNodes("descendant::text()").iterator(); it.hasNext();) {
// How many years have we used Java & XML, and this still isn't easy?
org.dom4j.Text txt = it.next();
if (txt.getText().trim().length() == 0) {
txt.getParent().remove(txt);
}
}
org.dom4j.Element copy = layout.createCopy();
org.dom4j.Document doc = fac.createDocument(copy);
doc.normalize();
layoutDom = writer.write(doc);
person.setAttribute(Constants.PLF, layoutDom);
if (saSet) {
this.setStructureStylesheetUserPreferences(person, profile.getProfileId(), ssup);
}
if (taSet) {
this.setThemeStylesheetUserPreferences(person, profile.getProfileId(), tsup);
}
} catch (Throwable t) {
log.error("Unable to set UserPreferences for user: " + person.getUserName());
throw new RuntimeException(t);
}
// Finally store the layout...
try {