From 97a81693fc908e11d15d4d524819cdee754cf94c Mon Sep 17 00:00:00 2001 From: jensp Date: Thu, 13 Apr 2017 12:30:02 +0000 Subject: [PATCH] CCM NG/ccm-cms : AssetSearchPage CCM NG/ccm-core: GlobalizationHelper now provides a method for retrieving the value most suitable for the negioated locale form a LocalizedString git-svn-id: https://svn.libreccm.org/ccm/ccm_ng@4673 8810af33-2d31-482b-a856-94f89814c4df --- .../{ => searchpage}/AssetSearchPage.java | 58 ++++++++++++- .../searchpage/AssetSearchPageController.java | 85 +++++++++++++++++++ .../assets/searchpage/ResultsTableModel.java | 76 +++++++++++++++++ .../ui/assets/searchpage/ResultsTableRow.java | 64 ++++++++++++++ .../libreccm/l10n/GlobalizationHelper.java | 47 +++++++++- 5 files changed, 324 insertions(+), 6 deletions(-) rename ccm-cms/src/main/java/com/arsdigita/cms/ui/assets/{ => searchpage}/AssetSearchPage.java (72%) create mode 100644 ccm-cms/src/main/java/com/arsdigita/cms/ui/assets/searchpage/AssetSearchPageController.java create mode 100644 ccm-cms/src/main/java/com/arsdigita/cms/ui/assets/searchpage/ResultsTableModel.java create mode 100644 ccm-cms/src/main/java/com/arsdigita/cms/ui/assets/searchpage/ResultsTableRow.java diff --git a/ccm-cms/src/main/java/com/arsdigita/cms/ui/assets/AssetSearchPage.java b/ccm-cms/src/main/java/com/arsdigita/cms/ui/assets/searchpage/AssetSearchPage.java similarity index 72% rename from ccm-cms/src/main/java/com/arsdigita/cms/ui/assets/AssetSearchPage.java rename to ccm-cms/src/main/java/com/arsdigita/cms/ui/assets/searchpage/AssetSearchPage.java index e40950dcd..2ce5e16df 100644 --- a/ccm-cms/src/main/java/com/arsdigita/cms/ui/assets/AssetSearchPage.java +++ b/ccm-cms/src/main/java/com/arsdigita/cms/ui/assets/searchpage/AssetSearchPage.java @@ -16,26 +16,35 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, * MA 02110-1301 USA */ -package com.arsdigita.cms.ui.assets; +package com.arsdigita.cms.ui.assets.searchpage; +import com.arsdigita.bebop.Component; import com.arsdigita.bebop.Form; import com.arsdigita.bebop.FormData; import com.arsdigita.bebop.FormProcessException; import com.arsdigita.bebop.Label; +import com.arsdigita.bebop.Link; import com.arsdigita.bebop.PageState; import com.arsdigita.bebop.SimpleContainer; import com.arsdigita.bebop.Table; +import com.arsdigita.bebop.Text; import com.arsdigita.bebop.event.FormInitListener; import com.arsdigita.bebop.event.FormSectionEvent; import com.arsdigita.bebop.form.Submit; import com.arsdigita.bebop.form.TextField; import com.arsdigita.bebop.parameters.LongParameter; import com.arsdigita.bebop.parameters.StringParameter; +import com.arsdigita.bebop.table.TableCellRenderer; import com.arsdigita.bebop.table.TableColumn; import com.arsdigita.bebop.table.TableColumnModel; +import com.arsdigita.bebop.table.TableModel; +import com.arsdigita.bebop.table.TableModelBuilder; import com.arsdigita.cms.dispatcher.CMSPage; import com.arsdigita.globalization.GlobalizedMessage; import com.arsdigita.toolbox.ui.LayoutPanel; +import com.arsdigita.util.LockableImpl; +import java.util.List; +import org.libreccm.cdi.utils.CdiUtil; import org.librecms.CmsConstants; @@ -122,10 +131,55 @@ public class AssetSearchPage extends CMSPage { new GlobalizedMessage( "cms.ui.assets.search_page.results_table.type", CmsConstants.CMS_BUNDLE))); - + resultsTable.setModelBuilder(new ResultsTableModelBuilder()); mainPanel.setBody(resultsTable); } + private class ResultsTableModelBuilder + extends LockableImpl + implements TableModelBuilder { + + @Override + public TableModel makeModel(final Table table, final PageState state) { + + final CdiUtil cdiUtil = CdiUtil.createCdiUtil(); + final AssetSearchPageController controller = cdiUtil + .findBean(AssetSearchPageController.class); + + final List rows = controller + .findAssets((String) query.getValue(state)); + return new ResultsTableModel(rows); + } + + } + + private class TitleCellRenderer + extends LockableImpl + implements TableCellRenderer { + + @Override + public Component getComponent(final Table table, + final PageState state, + final Object value, + final boolean isSelected, + final Object key, + final int row, + final int column) { + + if (value == null) { + return new Text("???"); + } + + final Link link = new Link(new Text(value.toString()), ""); + + return link; + + + } + + + } + } diff --git a/ccm-cms/src/main/java/com/arsdigita/cms/ui/assets/searchpage/AssetSearchPageController.java b/ccm-cms/src/main/java/com/arsdigita/cms/ui/assets/searchpage/AssetSearchPageController.java new file mode 100644 index 000000000..e62e8c967 --- /dev/null +++ b/ccm-cms/src/main/java/com/arsdigita/cms/ui/assets/searchpage/AssetSearchPageController.java @@ -0,0 +1,85 @@ +/* + * Copyright (C) 2017 LibreCCM Foundation. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301 USA + */ +package com.arsdigita.cms.ui.assets.searchpage; + +import java.util.List; +import java.util.ResourceBundle; +import java.util.stream.Collectors; +import javax.enterprise.context.RequestScoped; +import javax.inject.Inject; +import javax.transaction.Transactional; +import org.libreccm.configuration.ConfigurationManager; +import org.libreccm.l10n.GlobalizationHelper; +import org.librecms.assets.AssetTypeInfo; +import org.librecms.assets.AssetTypesManager; +import org.librecms.contentsection.Asset; +import org.librecms.contentsection.AssetManager; +import org.librecms.contentsection.AssetRepository; + +/** + * + * @author Jens Pelzetter + */ +@RequestScoped +class AssetSearchPageController { + + @Inject + private AssetRepository assetRepo; + + @Inject + private AssetTypesManager assetTypesManager; + + @Inject + private AssetManager assetManager; + + @Inject + private GlobalizationHelper globalizationHelper; + + @Transactional(Transactional.TxType.REQUIRED) + protected List findAssets(final String title) { + + final List assets = assetRepo.findByTitle(title); + + return assets + .stream() + .map(asset -> createRow(asset)) + .collect(Collectors.toList()); + + } + + private ResultsTableRow createRow(final Asset asset) { + final ResultsTableRow row = new ResultsTableRow(); + + row.setAssetUuid(asset.getUuid()); + row.setTitle(globalizationHelper + .getValueFromLocalizedString(asset.getTitle())); + + final AssetTypeInfo typeInfo = assetTypesManager + .getAssetTypeInfo(asset.getClass()); + final ResourceBundle bundle = ResourceBundle + .getBundle(typeInfo.getLabelBundle(), + globalizationHelper.getNegotiatedLocale()); + row.setType(bundle.getString(typeInfo.getLabelKey())); + + row.setPlace(assetManager.getAssetPath(asset)); + + return row; + } + +} diff --git a/ccm-cms/src/main/java/com/arsdigita/cms/ui/assets/searchpage/ResultsTableModel.java b/ccm-cms/src/main/java/com/arsdigita/cms/ui/assets/searchpage/ResultsTableModel.java new file mode 100644 index 000000000..85c84d05c --- /dev/null +++ b/ccm-cms/src/main/java/com/arsdigita/cms/ui/assets/searchpage/ResultsTableModel.java @@ -0,0 +1,76 @@ +/* + * Copyright (C) 2017 LibreCCM Foundation. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301 USA + */ +package com.arsdigita.cms.ui.assets.searchpage; + +import com.arsdigita.bebop.table.TableModel; +import java.util.Iterator; +import java.util.List; + +/** + * + * @author Jens Pelzetter + */ +class ResultsTableModel implements TableModel { + + private static final int COL_TITLE = 0; + private static final int COL_PLACE = 1; + private static final int COL_TYPE = 2; + + private final Iterator iterator; + private ResultsTableRow currentRow; + + public ResultsTableModel(final List rows) { + iterator = rows.iterator(); + } + + @Override + public int getColumnCount() { + return 3; + } + + @Override + public boolean nextRow() { + if (iterator.hasNext()) { + currentRow = iterator.next(); + return true; + } else { + return false; + } + } + + @Override + public Object getElementAt(final int columnIndex) { + switch(columnIndex) { + case COL_TITLE: + return currentRow.getTitle(); + case COL_PLACE: + return currentRow.getPlace(); + case COL_TYPE: + return currentRow.getType(); + default: + throw new IllegalArgumentException("Illegal column index."); + } + } + + @Override + public Object getKeyAt(final int columnIndex) { + return currentRow.getAssetUuid(); + } + +} diff --git a/ccm-cms/src/main/java/com/arsdigita/cms/ui/assets/searchpage/ResultsTableRow.java b/ccm-cms/src/main/java/com/arsdigita/cms/ui/assets/searchpage/ResultsTableRow.java new file mode 100644 index 000000000..aaf2292d0 --- /dev/null +++ b/ccm-cms/src/main/java/com/arsdigita/cms/ui/assets/searchpage/ResultsTableRow.java @@ -0,0 +1,64 @@ +/* + * Copyright (C) 2017 LibreCCM Foundation. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301 USA + */ +package com.arsdigita.cms.ui.assets.searchpage; + +/** + * + * @author Jens Pelzetter + */ +class ResultsTableRow { + + private String assetUuid; + private String title; + private String place; + private String type; + + public String getAssetUuid() { + return assetUuid; + } + + public void setAssetUuid(final String assetUuid) { + this.assetUuid = assetUuid; + } + + public String getTitle() { + return title; + } + + public void setTitle(final String title) { + this.title = title; + } + + public String getPlace() { + return place; + } + + public void setPlace(final String place) { + this.place = place; + } + + public String getType() { + return type; + } + + public void setType(final String type) { + this.type = type; + } + +} diff --git a/ccm-core/src/main/java/org/libreccm/l10n/GlobalizationHelper.java b/ccm-core/src/main/java/org/libreccm/l10n/GlobalizationHelper.java index 22c7b02aa..3e24bb380 100644 --- a/ccm-core/src/main/java/org/libreccm/l10n/GlobalizationHelper.java +++ b/ccm-core/src/main/java/org/libreccm/l10n/GlobalizationHelper.java @@ -73,10 +73,49 @@ public class GlobalizationHelper { @Inject private ConfigurationManager confManager; -// private final KernelConfig kernelConfig; -// public GlobalizationHelper() { -// kernelConfig = confManager.findConfiguration(KernelConfig.class); -// } + /** + * An utility method for getting an value from a {@link LocalizedString}. + * + * First tries to get a value for the negotiated locale. If the + * {@code localizedString} does not have a value for the negotiated locale + * the default locale set in {@link KernelConfig} is used. If that also + * values the first value available locale (ordered alphabetically) is used. + * + * @param localizedString + * @return + */ + public String getValueFromLocalizedString( + final LocalizedString localizedString) { + + if (localizedString.hasValue(getNegotiatedLocale())) { + return localizedString.getValue(getNegotiatedLocale()); + } + + final KernelConfig kernelConfig = confManager + .findConfiguration(KernelConfig.class); + if (localizedString.hasValue(kernelConfig.getDefaultLocale())) { + return localizedString.getValue(kernelConfig.getDefaultLocale()); + } + + if (localizedString.getAvailableLocales().isEmpty()) { + return ""; + } + + return localizedString + .getValues() + .entrySet() + .stream() + .sorted((entry1, entry2) -> { + return entry1 + .getKey() + .toString() + .compareTo(entry2.getKey().toString()); + }) + .findFirst() + .get() + .getValue(); + } + public Locale getNegotiatedLocale() { final KernelConfig kernelConfig = confManager.findConfiguration( KernelConfig.class);