CategoryMaintActionForm form = (CategoryMaintActionForm) actionForm;
AdminBean adminBean = getAdminBean(request);
Site site = adminBean.getSite();
initSiteProfiles(form, site);
Category referenceCategory = CategoryDAO.load(site.getSiteId(), Format.getLong(form.getCreateCatId()));
Category parent = null;
int seqNum = 0;
if (form.getCreateMode().equals("C")) { // append child node
for (Category child : referenceCategory.getCategoryChildren()) {
if (child.getSeqNum() >= seqNum) {
seqNum = child.getSeqNum() + 1;
}
}
parent = referenceCategory;
} else if (form.getCreateMode().equals("B")) { // before current
parent = referenceCategory.getCategoryParent();
seqNum = referenceCategory.getSeqNum();
for (Category child : parent.getCategoryChildren()) {
if (child.getSeqNum() >= seqNum) {
child.setSeqNum(child.getSeqNum() + 1);
em.persist(child);
}
}
} else if (form.getCreateMode().equals("A")) { // after current
parent = referenceCategory.getCategoryParent();
seqNum = referenceCategory.getSeqNum() + 1;
for (Category child : parent.getCategoryChildren()) {
if (child.getSeqNum() >= seqNum) {
child.setSeqNum(child.getSeqNum() + 1);
em.persist(child);
}
}
}
int count = 0;
String catShortTitle = "New category";
boolean exist = true;
while (exist) {
exist = false;
for (Category child : parent.getCategoryChildren()) {
if (child.getCategoryLanguage().getCatShortTitle().equals(catShortTitle)) {
exist = true;
count++;
catShortTitle = "New category " + count;
break;
}
}
}
Category category = new Category();
category.setSite(site);
category.setCategoryParent(parent);
category.setSeqNum(seqNum);
category.setPublished(Constants.PUBLISHED_YES);
category.setRecUpdateBy(adminBean.getUser().getUserId());
category.setRecCreateBy(adminBean.getUser().getUserId());
category.setRecUpdateDatetime(new Date(System.currentTimeMillis()));
category.setRecCreateDatetime(new Date(System.currentTimeMillis()));
parent.getCategoryChildren().add(category);
CategoryLanguage categoryLanguage = new CategoryLanguage();
categoryLanguage.setCatShortTitle(catShortTitle);
categoryLanguage.setCatTitle("");
categoryLanguage.setCatDesc("");
categoryLanguage.setMetaKeywords("");
categoryLanguage.setMetaDescription("");
categoryLanguage.setCategory(category);
categoryLanguage.setRecUpdateBy(adminBean.getUser().getUserId());
categoryLanguage.setRecCreateBy(adminBean.getUser().getUserId());
categoryLanguage.setRecUpdateDatetime(new Date(System.currentTimeMillis()));
categoryLanguage.setRecCreateDatetime(new Date(System.currentTimeMillis()));
categoryLanguage.setSiteProfileClass(site.getSiteProfileClassDefault());
category.getCategoryLanguages().add(categoryLanguage);
category.setCategoryLanguage(categoryLanguage);
em.persist(categoryLanguage);
String fullCategoryPath = getFullCategoryPath(category, site.getSiteId(), form.getSiteProfileClassDefaultId());
category.setCatNaturalKey(Utility.encode(fullCategoryPath));
em.persist(category);
form.setCatId(Format.getLong(category.getCatId()));
form.setCategoryParentId(category.getCategoryParent().getCatId().toString());
form.setCatTitle("");
form.setMetaKeywords("");
form.setMetaDescription("");
form.setCatShortTitle(categoryLanguage.getCatShortTitle());
form.setCatDesc("");
form.setPublished(category.getPublished() == Constants.PUBLISHED_YES ? true : false);
form.setMode(Constants.MODE_UPDATE);
String jsonCategoryTree = Utility.makeJSONCategoryTree(site.getSiteId(), form.getSiteProfileClassId(), form.isSiteProfileClassDefault()).toHtmlString();
form.setJsonCategoryTree(jsonCategoryTree);
form.setPublished(true);
form.setSequence(false);