Fixed several potential lazy init execeptions in forms for MPA

ccm-docs
Jens Pelzetter 2020-03-12 12:11:31 +01:00
parent 280ddbecb7
commit 68f3351138
8 changed files with 198 additions and 31 deletions

View File

@ -129,15 +129,15 @@ public class MultiPartArticleEditForm extends MultiPartArticleForm
.selectedLocale(state, selectedLanguageParam); .selectedLocale(state, selectedLanguageParam);
final String newName = (String) data.get(MultiPartArticleForm.NAME); final String newName = (String) data.get(MultiPartArticleForm.NAME);
final String oldName = article.getName().getValue(selectedLocale); final MultiPartArticleFormController controller = CdiUtil
.createCdiUtil()
.findBean(MultiPartArticleFormController.class);
final String oldName = controller.getName(article, selectedLocale);
final boolean valid; final boolean valid;
if (newName.equalsIgnoreCase(oldName)) { if (newName.equalsIgnoreCase(oldName)) {
valid = true; valid = true;
} else { } else {
final CdiUtil cdiUtil = CdiUtil.createCdiUtil();
final MultiPartArticleFormController controller = cdiUtil
.findBean(MultiPartArticleFormController.class);
final Optional<Folder> folder = controller.getArticleFolder(article); final Optional<Folder> folder = controller.getArticleFolder(article);
if (folder.isPresent()) { if (folder.isPresent()) {
valid = validateNameUniqueness(folder.get(), event); valid = validateNameUniqueness(folder.get(), event);

View File

@ -233,14 +233,17 @@ public abstract class MultiPartArticleForm
final Locale selectedLocale = SelectedLanguageUtil final Locale selectedLocale = SelectedLanguageUtil
.selectedLocale(state, selectedLanguageParam); .selectedLocale(state, selectedLanguageParam);
final MultiPartArticleFormController controller = CdiUtil
.createCdiUtil()
.findBean(MultiPartArticleFormController.class);
if (article != null) { if (article != null) {
data.put(NAME, article.getName().getValue(selectedLocale)); data.put(NAME, controller.getName(article, selectedLocale));
data.put(TITLE, article.getTitle().getValue(selectedLocale)); data.put(TITLE, controller.getTitle(article, selectedLocale));
if (!CMSConfig.getConfig().isHideLaunchDate()) { if (!CMSConfig.getConfig().isHideLaunchDate()) {
data.put(LAUNCH_DATE, article.getLaunchDate()); data.put(LAUNCH_DATE, article.getLaunchDate());
} }
data.put(SUMMARY, article.getSummary().getValue(selectedLocale)); data.put(SUMMARY, controller.getSummary(article, selectedLocale));
} }
return article; return article;

View File

@ -24,6 +24,8 @@ import org.librecms.contentsection.ContentItemRepository;
import org.librecms.contentsection.Folder; import org.librecms.contentsection.Folder;
import org.librecms.contenttypes.MultiPartArticle; import org.librecms.contenttypes.MultiPartArticle;
import java.util.Locale;
import java.util.Objects;
import java.util.Optional; import java.util.Optional;
import javax.enterprise.context.RequestScoped; import javax.enterprise.context.RequestScoped;
@ -46,8 +48,9 @@ public class MultiPartArticleFormController {
@Transactional(Transactional.TxType.REQUIRED) @Transactional(Transactional.TxType.REQUIRED)
protected Optional<Folder> getArticleFolder(final MultiPartArticle article) { protected Optional<Folder> getArticleFolder(final MultiPartArticle article) {
final Optional<ContentItem> mpa = itemRepo final Optional<ContentItem> mpa = itemRepo.findById(
.findById(article.getObjectId()); article.getObjectId()
);
if (mpa.isPresent()) { if (mpa.isPresent()) {
return itemManager.getItemFolder(mpa.get()); return itemManager.getItemFolder(mpa.get());
@ -56,4 +59,62 @@ public class MultiPartArticleFormController {
} }
} }
@Transactional(Transactional.TxType.REQUIRED)
public String getName(
final MultiPartArticle fromMpa, final Locale forLocale
) {
Objects.requireNonNull(fromMpa);
Objects.requireNonNull(forLocale);
final MultiPartArticle mpa = itemRepo
.findById(fromMpa.getObjectId(), MultiPartArticle.class)
.orElseThrow(
() -> new IllegalArgumentException(
String.format(
"No MultiPartArticle with ID %d available",
fromMpa.getObjectId()
)
)
);
return mpa.getName().getValue(forLocale);
}
@Transactional(Transactional.TxType.REQUIRED)
public String getTitle(
final MultiPartArticle fromMpa, final Locale forLocale
) {
Objects.requireNonNull(fromMpa);
Objects.requireNonNull(forLocale);
final MultiPartArticle mpa = itemRepo
.findById(fromMpa.getObjectId(), MultiPartArticle.class)
.orElseThrow(
() -> new IllegalArgumentException(
String.format(
"No MultiPartArticle with ID %d available",
fromMpa.getObjectId()
)
)
);
return mpa.getTitle().getValue(forLocale);
}
@Transactional(Transactional.TxType.REQUIRED)
public String getSummary(
final MultiPartArticle fromMpa, final Locale forLocale
) {
Objects.requireNonNull(fromMpa);
Objects.requireNonNull(forLocale);
final MultiPartArticle mpa = itemRepo
.findById(fromMpa.getObjectId(), MultiPartArticle.class)
.orElseThrow(
() -> new IllegalArgumentException(
String.format(
"No MultiPartArticle with ID %d available",
fromMpa.getObjectId()
)
)
);
return mpa.getSummary().getValue(forLocale);
}
} }

View File

@ -25,6 +25,8 @@ import org.librecms.contenttypes.MultiPartArticleSectionManager;
import org.librecms.contenttypes.MultiPartArticleSectionRepository; import org.librecms.contenttypes.MultiPartArticleSectionRepository;
import java.util.List; import java.util.List;
import java.util.Locale;
import java.util.Objects;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import javax.enterprise.context.RequestScoped; import javax.enterprise.context.RequestScoped;
@ -127,15 +129,81 @@ class MultiPartArticleSectionStepController {
protected void moveAfter(final MultiPartArticle article, protected void moveAfter(final MultiPartArticle article,
final MultiPartArticleSection section, final MultiPartArticleSection section,
final MultiPartArticleSection after) { final MultiPartArticleSection after) {
final MultiPartArticle theArticle = itemRepo final MultiPartArticle theArticle = itemRepo
.findById(article.getObjectId(), .findById(article.getObjectId(),
MultiPartArticle.class) MultiPartArticle.class)
.orElseThrow(() -> new IllegalArgumentException(String.format( .orElseThrow(() -> new IllegalArgumentException(String.format(
"No MultiPartArticle with ID %d in the database.", "No MultiPartArticle with ID %d in the database.",
article.getObjectId()))); article.getObjectId())));
sectionManager.moveSectionAfter(theArticle, section, after); sectionManager.moveSectionAfter(theArticle, section, after);
} }
@Transactional(Transactional.TxType.REQUIRED)
public String getSectionTitle(
final MultiPartArticleSection ofSection, final Locale forLocale
) {
Objects.requireNonNull(ofSection);
Objects.requireNonNull(forLocale);
final MultiPartArticleSection section = sectionRepo
.findById(ofSection.getSectionId())
.orElseThrow(
() -> new IllegalArgumentException(
String.format(
"No section with ID %d available.", ofSection
.getSectionId()
)
)
);
return section.getTitle().getValue(forLocale);
}
@Transactional(Transactional.TxType.REQUIRED)
public String getSectionText(
final MultiPartArticleSection ofSection, final Locale forLocale
) {
Objects.requireNonNull(ofSection);
Objects.requireNonNull(forLocale);
final MultiPartArticleSection section = sectionRepo
.findById(ofSection.getSectionId())
.orElseThrow(
() -> new IllegalArgumentException(
String.format(
"No section with ID %d available.", ofSection
.getSectionId()
)
)
);
return section.getText().getValue(forLocale);
}
@Transactional(Transactional.TxType.REQUIRED)
public void updateSection(
final MultiPartArticleSection section,
final String title,
final String text,
final boolean pageBreak,
final Locale locale
) {
Objects.requireNonNull(section);
Objects.requireNonNull(locale);
final MultiPartArticleSection update = sectionRepo
.findById(section.getSectionId())
.orElseThrow(
() -> new IllegalArgumentException(
String.format(
"No section with ID %d available.",
section.getSectionId()
)
)
);
update.getTitle().addValue(locale, title);
update.getText().addValue(locale, title);
update.setPageBreak(pageBreak);
}
} }

View File

@ -158,11 +158,16 @@ public class MultiPartArticleSectionsStep extends ResettableContainer {
final Locale selectedLocale = SelectedLanguageUtil final Locale selectedLocale = SelectedLanguageUtil
.selectedLocale(state, selectedLanguageParam); .selectedLocale(state, selectedLanguageParam);
final MultiPartArticleSectionStepController controller = CdiUtil
.createCdiUtil()
.findBean(MultiPartArticleSectionStepController.class);
final Object[] parameterObj = { final Object[] parameterObj = {
controller.getSectionTitle(
moveSectionModel moveSectionModel
.getSelectedSection(state) .getSelectedSection(state),
.getTitle() selectedLocale
.getValue(selectedLocale) )
}; };
target.setLabel(new GlobalizedMessage( target.setLabel(new GlobalizedMessage(

View File

@ -35,7 +35,6 @@ import com.arsdigita.globalization.GlobalizedMessage;
import org.librecms.contenttypes.MultiPartArticle; import org.librecms.contenttypes.MultiPartArticle;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import org.libreccm.cdi.utils.CdiUtil; import org.libreccm.cdi.utils.CdiUtil;
@ -110,9 +109,14 @@ public class SectionDeleteForm extends Form
final Locale selectedLocale = SelectedLanguageUtil final Locale selectedLocale = SelectedLanguageUtil
.selectedLocale(state, selectedLanguageParam); .selectedLocale(state, selectedLanguageParam);
final MultiPartArticleSectionStepController controller = CdiUtil
.createCdiUtil()
.findBean(MultiPartArticleSectionStepController.class);
sectionNameLabel.setLabel( sectionNameLabel.setLabel(
section.getTitle().getValue(selectedLocale), controller.getSectionTitle(section, selectedLocale),
state); state
);
} }
} }

View File

@ -198,12 +198,19 @@ public class SectionEditForm extends Form {
final Locale selectedLocale = SelectedLanguageUtil final Locale selectedLocale = SelectedLanguageUtil
.selectedLocale(state, selectedLanguageParam); .selectedLocale(state, selectedLanguageParam);
final MultiPartArticleSectionStepController controller = CdiUtil
.createCdiUtil()
.findBean(MultiPartArticleSectionStepController.class);
final MultiPartArticleSection section = selectedSectionModel final MultiPartArticleSection section = selectedSectionModel
.getSelectedSection(state); .getSelectedSection(state);
data.put(TITLE, section.getTitle().getValue(selectedLocale)); data.put(
data.put(TEXT, section.getText().getValue(selectedLocale)); TITLE, controller.getSectionTitle(section, selectedLocale)
);
data.put(
TEXT, controller.getSectionText(section, selectedLocale)
);
if (section.isPageBreak()) { if (section.isPageBreak()) {
data.put(PAGE_BREAK, new Object[]{"true"}); data.put(PAGE_BREAK, new Object[]{"true"});
@ -281,9 +288,8 @@ public class SectionEditForm extends Form {
section = selectedSectionModel.getSelectedSection(state); section = selectedSectionModel.getSelectedSection(state);
} }
section.getTitle().addValue(selectedLocale, // section.getTitle().addValue(selectedLocale,
(String) data.get(TITLE)); // (String) data.get(TITLE));
final Object[] pageBreakVal = (Object[]) data.get(PAGE_BREAK); final Object[] pageBreakVal = (Object[]) data.get(PAGE_BREAK);
final boolean pageBreak; final boolean pageBreak;
if (pageBreakVal == null if (pageBreakVal == null
@ -293,7 +299,7 @@ public class SectionEditForm extends Form {
} else { } else {
pageBreak = true; pageBreak = true;
} }
section.setPageBreak(pageBreak); // section.setPageBreak(pageBreak);
final String text; final String text;
if (data.get(TEXT) == null) { if (data.get(TEXT) == null) {
@ -301,12 +307,24 @@ public class SectionEditForm extends Form {
} else { } else {
text = (String) data.get(TEXT); text = (String) data.get(TEXT);
} }
section.getText().addValue(selectedLocale, text); // section.getText().addValue(selectedLocale, text);
sectionRepo.save(section);
// sectionRepo.save(section);
if (selectedSectionModel.getSelectedKey(state) == null) { if (selectedSectionModel.getSelectedKey(state) == null) {
section.getTitle().addValue(selectedLocale,
(String) data.get(TITLE));
section.setPageBreak(pageBreak);
section.getText().addValue(selectedLocale, text);
controller.addSection(article, section); controller.addSection(article, section);
} else {
controller.updateSection(
section,
(String) data.get(TITLE),
text,
pageBreak,
selectedLocale
);
} }
if (sectionsStep != null) { if (sectionsStep != null) {

View File

@ -34,6 +34,7 @@ import org.librecms.contenttypes.MultiPartArticleSection;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Locale;
class SectionTableModel implements TableModel { class SectionTableModel implements TableModel {
@ -109,13 +110,20 @@ class SectionTableModel implements TableModel {
if (columnModel == null) { if (columnModel == null) {
return null; return null;
} }
final MultiPartArticleSectionStepController controller = CdiUtil
.createCdiUtil()
.findBean(MultiPartArticleSectionStepController.class);
switch (columnIndex) { switch (columnIndex) {
case SectionTable.COL_INDEX_TITLE: case SectionTable.COL_INDEX_TITLE:
return currentSection return controller.getSectionTitle(
.getTitle() currentSection,
.getValue(SelectedLanguageUtil SelectedLanguageUtil.selectedLocale(
.selectedLocale(pageState, selectedLanguageParam)); pageState, selectedLanguageParam
)
)
;
case SectionTable.COL_PAGE_BREAK: case SectionTable.COL_PAGE_BREAK:
if (currentSection.isPageBreak()) { if (currentSection.isPageBreak()) {
return new Label( return new Label(