/**
* <a href="http://www.openolat.org">
* OpenOLAT - Online Learning and Training</a><br>
* <p>
* Licensed under the Apache License, Version 2.0 (the "License"); <br>
* you may not use this file except in compliance with the License.<br>
* You may obtain a copy of the License at the
* <a href="http://www.apache.org/licenses/LICENSE-2.0">Apache homepage</a>
* <p>
* Unless required by applicable law or agreed to in writing,<br>
* software distributed under the License is distributed on an "AS IS" BASIS, <br>
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. <br>
* See the License for the specific language governing permissions and <br>
* limitations under the License.
* <p>
* Initial code contributed and copyrighted by<br>
* frentix GmbH, http://www.frentix.com
* <p>
*/
package org.olat.modules.video.ui;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.olat.core.CoreSpringFactory;
import org.olat.core.commons.persistence.DefaultResultInfos;
import org.olat.core.commons.persistence.ResultInfos;
import org.olat.core.commons.persistence.SortKey;
import org.olat.core.gui.components.form.flexible.elements.FlexiTableFilter;
import org.olat.core.gui.components.form.flexible.impl.elements.table.FlexiTableDataSourceDelegate;
import org.olat.core.util.StringHelper;
import org.olat.repository.RepositoryEntryMyView;
import org.olat.repository.RepositoryService;
import org.olat.repository.model.SearchMyRepositoryEntryViewParams;
import org.olat.repository.model.SearchMyRepositoryEntryViewParams.Filter;
import org.olat.repository.model.SearchMyRepositoryEntryViewParams.OrderBy;
import org.olat.repository.ui.list.RepositoryEntryRow;
/**
*
* Initial date: 28.01.2014<br>
* @author srosse, stephane.rosse@frentix.com, http://www.frentix.com
*
*/
public class VideoEntryDataSource implements FlexiTableDataSourceDelegate<RepositoryEntryRow> {
private final SearchMyRepositoryEntryViewParams searchParams;
private final RepositoryService repositoryService;
private Integer count;
public VideoEntryDataSource(SearchMyRepositoryEntryViewParams searchParams) {
this.searchParams = searchParams;
repositoryService = CoreSpringFactory.getImpl(RepositoryService.class);
}
public void setFilters(List<Filter> filters) {
searchParams.setFilters(filters);
count = null;
}
public void setOrderBy(OrderBy orderBy) {
searchParams.setOrderBy(orderBy);
}
public void resetCount() {
count = null;
}
@Override
public int getRowCount() {
if(count == null) {
count = repositoryService.countMyView(searchParams);
}
return count.intValue();
}
@Override
public List<RepositoryEntryRow> reload(List<RepositoryEntryRow> rows) {
return Collections.emptyList();
}
@Override
public ResultInfos<RepositoryEntryRow> getRows(String query, List<FlexiTableFilter> filters,
List<String> condQueries, int firstResult, int maxResults, SortKey... orderBy) {
if(condQueries != null && condQueries.size() > 0) {
String filter = condQueries.get(0);
if(StringHelper.containsNonWhitespace(filter)) {
searchParams.setFilters(Collections.singletonList(Filter.valueOf(filter)));
} else {
searchParams.setFilters(null);
}
}
if(orderBy != null && orderBy.length > 0 && orderBy[0] != null) {
OrderBy o = OrderBy.valueOf(orderBy[0].getKey());
searchParams.setOrderBy(o);
searchParams.setOrderByAsc(orderBy[0].isAsc());
}
if(StringHelper.containsNonWhitespace(query)) {
searchParams.setText(query);
} else {
searchParams.setText(null);
}
List<RepositoryEntryMyView> views = repositoryService.searchMyView(searchParams, firstResult, maxResults);
List<RepositoryEntryRow> rows = processViewModel(views);
ResultInfos<RepositoryEntryRow> results = new DefaultResultInfos<RepositoryEntryRow>(firstResult + rows.size(), -1, rows);
if(firstResult == 0 && views.size() < maxResults) {
count = new Integer(views.size());
}
return results;
}
private List<RepositoryEntryRow> processViewModel(List<RepositoryEntryMyView> repoEntries) {
List<RepositoryEntryRow> items = new ArrayList<RepositoryEntryRow>();
for(RepositoryEntryMyView entry:repoEntries) {
RepositoryEntryRow row = new RepositoryEntryRow(entry);
items.add(row);
}
return items;
}
}