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);
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;
if (newName.equalsIgnoreCase(oldName)) {
valid = true;
} else {
final CdiUtil cdiUtil = CdiUtil.createCdiUtil();
final MultiPartArticleFormController controller = cdiUtil
.findBean(MultiPartArticleFormController.class);
final Optional<Folder> folder = controller.getArticleFolder(article);
if (folder.isPresent()) {
valid = validateNameUniqueness(folder.get(), event);

View File

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

View File

@ -24,6 +24,8 @@ import org.librecms.contentsection.ContentItemRepository;
import org.librecms.contentsection.Folder;
import org.librecms.contenttypes.MultiPartArticle;
import java.util.Locale;
import java.util.Objects;
import java.util.Optional;
import javax.enterprise.context.RequestScoped;
@ -46,8 +48,9 @@ public class MultiPartArticleFormController {
@Transactional(Transactional.TxType.REQUIRED)
protected Optional<Folder> getArticleFolder(final MultiPartArticle article) {
final Optional<ContentItem> mpa = itemRepo
.findById(article.getObjectId());
final Optional<ContentItem> mpa = itemRepo.findById(
article.getObjectId()
);
if (mpa.isPresent()) {
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 java.util.List;
import java.util.Locale;
import java.util.Objects;
import java.util.stream.Collectors;
import javax.enterprise.context.RequestScoped;
@ -138,4 +140,70 @@ class MultiPartArticleSectionStepController {
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
.selectedLocale(state, selectedLanguageParam);
final MultiPartArticleSectionStepController controller = CdiUtil
.createCdiUtil()
.findBean(MultiPartArticleSectionStepController.class);
final Object[] parameterObj = {
controller.getSectionTitle(
moveSectionModel
.getSelectedSection(state)
.getTitle()
.getValue(selectedLocale)
.getSelectedSection(state),
selectedLocale
)
};
target.setLabel(new GlobalizedMessage(

View File

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

View File

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

View File

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