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

Former-commit-id: 7d7202ca50
pull/2/head
jensp 2017-04-13 12:30:02 +00:00
parent 33d8d6848f
commit 94d0837a0c
5 changed files with 324 additions and 6 deletions

View File

@ -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<ResultsTableRow> 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;
}
}
}

View File

@ -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 <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
*/
@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<ResultsTableRow> findAssets(final String title) {
final List<Asset> 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;
}
}

View File

@ -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 <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
*/
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<ResultsTableRow> iterator;
private ResultsTableRow currentRow;
public ResultsTableModel(final List<ResultsTableRow> 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();
}
}

View File

@ -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 <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
*/
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;
}
}

View File

@ -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);