if (create) {
try {
PortalConfig existing = storage.getPortalConfig(portalConfig.getType(), portalConfig.getName());
if (existing != null) throw new EntityAlreadyExistsException("Cannot create site. Site " + id + " already exists.");
} catch (Exception e) {
throw new ApiException("Exception occurred checking if site already existed before creating site " + id, e);
}
// In order to properly create a site (which includes creating it from a template) it seemed much harder
// to get it working properly (NewPortalConfigListener)
if (areWeInATestEnvironment()) {
try {
storage.create(portalConfig); // Just create an empty site
} catch (Exception e) {
throw new ApiException("Exception creating site " + id + " in testing environment.");
}
} else {
try {
switch (id.getType()) {
case SITE:
service.createUserPortalConfig(portalConfig.getType(), portalConfig.getName(), templateName);
break;
case SPACE:
service.createGroupSite(portalConfig.getName());
break;
case DASHBOARD:
service.createUserSite(portalConfig.getName());
break;
}
} catch (Exception e) {
throw new ApiException("Could not create site " + id, e);
}
}
// Retrieve the site that was created above and replay any changes done via the Site api object.
PortalConfig created;
try {
created = storage.getPortalConfig(portalConfig.getType(), portalConfig.getName());
if (created == null) throw new ApiException("Could not find site after we successfully created it.");
} catch (Exception e) {
throw new ApiException("Exception occurred retrieving previously created site " + id);
}
SiteImpl createdSite = new SiteImpl(created);
// Now replay/set the data that may have changed since the call from Portal.create to Portal.save
if (changed.contains("access")) {
createdSite.setAccessPermission(getAccessPermission());
}
if (changed.contains("edit")) {
createdSite.setEditPermission(getEditPermission());
}
if (changed.contains("skin")) {
createdSite.setSkin(getSkin());
}
if (changed.contains("locale")) {
createdSite.setLocale(getLocale());
}
if (changed.contains("description")) {
createdSite.setDescription(getDescription());
}
if (changed.contains("displayName")) {
createdSite.setDisplayName(getDisplayName());
}
createdSite.getAttributes().putAll(getAttributes());
this.attributes = createdSite.getAttributes();
// Now we can set the internal PortalConfig object which should now properly reflect the site that was created
// above and the changes done via the API
this.portalConfig = createdSite.portalConfig;
}
// Attributes is the only object that can be modified outside the context of this object
portalConfig.setProperties(Util.from(attributes));
try {
storage.save(portalConfig);
changed = null;
create = false;
templateName = null;
} catch (Exception e) {
throw new ApiException("Exception occurred trying to save site " + id, e);
}
}