/**
* OLAT - Online Learning and Training<br>
* http://www.olat.org
* <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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <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>
* Copyright (c) since 2004 at Multimedia- & E-Learning Services (MELS),<br>
* University of Zurich, Switzerland.
* <p>
*/
package org.olat.course;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.olat.core.gui.UserRequest;
import org.olat.core.gui.components.Component;
import org.olat.core.gui.components.table.BaseTableDataModelWithoutFilter;
import org.olat.core.gui.components.table.DefaultColumnDescriptor;
import org.olat.core.gui.components.table.TableController;
import org.olat.core.gui.components.table.TableDataModel;
import org.olat.core.gui.components.table.TableGuiConfiguration;
import org.olat.core.gui.components.velocity.VelocityContainer;
import org.olat.core.gui.control.Controller;
import org.olat.core.gui.control.Event;
import org.olat.core.gui.control.WindowControl;
import org.olat.core.gui.control.controller.BasicController;
import org.olat.core.gui.translator.PackageTranslator;
import org.olat.core.id.OLATResourceable;
import org.olat.core.logging.AssertException;
import org.olat.core.util.coordinate.CoordinatorManager;
import org.olat.core.util.resource.OresHelper;
import org.olat.course.run.RunMainController;
import org.olat.repository.RepositoryEntry;
import org.olat.repository.RepositoryEntryStatus;
import org.olat.repository.RepositoryManager;
/**
* Description:<br>
* TODO: patrickb Class Description for CourseAdminController
*
* <P>
* Initial Date: 18.08.2008 <br>
* @author patrickb
*/
class CourseAdminController extends BasicController implements Controller {
final List<List> columns = new ArrayList<List>(5);
private VelocityContainer mainVC;
private TableController tableCtr;
protected CourseAdminController(UserRequest ureq, WindowControl control) {
super(ureq, control);
//Map<Long, ICourse> loadedCourses = CourseFactory.getLoadedCourses();
Map<Long, Map<String, Long>> loadedCoursesStats = CourseFactory.getLoadedCoursesStats();
Set<Long> keys = loadedCoursesStats.keySet();
List<Integer> snapOfLoadedCoursesKeysCol = new ArrayList<Integer>(keys.size());
List<String> snapOfLoadedCoursesTitleCol = new ArrayList<String>(keys.size());
List<String> snapOfLoadedCoursesAuthor = new ArrayList<String>(keys.size());
List<Long> snapOfLoadedCoursesTime = new ArrayList<Long>(keys.size());
List<Long> snapOfLoadedCoursesCalls = new ArrayList<Long>(keys.size());
mainVC = createVelocityContainer("loadedcourses");
for (Long long1 : keys) {
try{
// to optimize for the view -> use getCourseFromCache -> but this may return null from the underlying CacheWrapper
// in null case -> show "- - - -" as known form the usersession table.
// see also OLAT-3586
ICourse course = CourseFactory.getCourseFromCache(long1);
if(course != null){
OLATResourceable courseRunOres = OresHelper.createOLATResourceableInstance(RunMainController.ORES_TYPE_COURSE_RUN, course.getResourceableId());
int listenerCnt = CoordinatorManager.getCoordinator().getEventBus().getListeningIdentityCntFor(courseRunOres);
snapOfLoadedCoursesKeysCol.add(Integer.valueOf(listenerCnt));
String courseTitle = course.getCourseTitle();
RepositoryEntry repositoryEntry = RepositoryManager.getInstance().lookupRepositoryEntry(course, false);
if (repositoryEntry != null && RepositoryManager.getInstance().createRepositoryEntryStatus(repositoryEntry.getStatusCode()).isClosed()) {
PackageTranslator pT = new PackageTranslator(RepositoryEntryStatus.class.getPackage().getName(), ureq.getLocale());
courseTitle = "[" + pT.translate("title.prefix.closed") + "] ".concat(courseTitle);
}
snapOfLoadedCoursesTitleCol.add(courseTitle);
RepositoryEntry repoEntry = RepositoryManager.getInstance().lookupRepositoryEntry(course, true);
snapOfLoadedCoursesAuthor.add(repoEntry.getInitialAuthor());
Map<String, Long> tmp = loadedCoursesStats.get(long1);
if(tmp == null){
snapOfLoadedCoursesTime.add(Long.valueOf(-1));
snapOfLoadedCoursesCalls.add(Long.valueOf(-1));
}else{
snapOfLoadedCoursesTime.add(tmp.get(CourseAdminTableModel.LOADTIME));
snapOfLoadedCoursesCalls.add(tmp.get(CourseAdminTableModel.NR_OF_LAUNCHES));
}
}else{
snapOfLoadedCoursesKeysCol.add(Integer.valueOf(-1));
snapOfLoadedCoursesTitleCol.add("invalidated::"+long1);
snapOfLoadedCoursesAuthor.add("-");
snapOfLoadedCoursesTime.add(Long.valueOf(-2));
snapOfLoadedCoursesCalls.add(Long.valueOf(-2));
}
}catch(AssertException ae){
// course.getCourseTitle -> title not available
// lookupRepositoryEntry(course) ->
// usual case is not to catch AssertExceptions!
logError("Problem during creation of course activity table model", ae);
}
}
columns.add(snapOfLoadedCoursesKeysCol);
columns.add(snapOfLoadedCoursesTime);
columns.add(snapOfLoadedCoursesCalls);
columns.add(snapOfLoadedCoursesTitleCol);
columns.add(snapOfLoadedCoursesAuthor);
TableGuiConfiguration tableConfig = new TableGuiConfiguration();
tableCtr = new TableController(tableConfig, ureq, getWindowControl(), getTranslator(), null);
tableCtr.addColumnDescriptor(new DefaultColumnDescriptor("current.active.user", 0, null, ureq.getLocale()));
tableCtr.addColumnDescriptor(new DefaultColumnDescriptor(CourseAdminTableModel.LOADTIME, 1, null, ureq.getLocale()));
tableCtr.addColumnDescriptor(new DefaultColumnDescriptor(CourseAdminTableModel.NR_OF_LAUNCHES, 2, null, ureq.getLocale()));
tableCtr.addColumnDescriptor(new DefaultColumnDescriptor("course.title", 3, null, ureq.getLocale()));
tableCtr.addColumnDescriptor(new DefaultColumnDescriptor("course.author", 4, null, ureq.getLocale()));
tableCtr.setTableDataModel(new CourseAdminTableModel());
listenTo(tableCtr);
if(snapOfLoadedCoursesKeysCol.size() > 0){
mainVC.put("table", tableCtr.getInitialComponent());
}
putInitialPanel(mainVC);
}
/**
* @see org.olat.core.gui.control.DefaultController#doDispose()
*/
@Override
protected void doDispose() {
// TODO Auto-generated method stub
}
/**
* @see org.olat.core.gui.control.DefaultController#event(org.olat.core.gui.UserRequest, org.olat.core.gui.components.Component, org.olat.core.gui.control.Event)
*/
@Override
protected void event(UserRequest ureq, Component source, Event event) {
}
class CourseAdminTableModel extends BaseTableDataModelWithoutFilter implements TableDataModel {
public static final String LOADTIME = "course.load.time";
public static final String NR_OF_LAUNCHES = "course.times.loaded";
public int getColumnCount() {
return columns.size();
}
public Object getValueAt(int row, int col) {
return columns.get(col).get(row);
}
public int getRowCount() {
return columns.get(0).size();
}
}
}