Skip to content
Snippets Groups Projects
Commit 9834efee authored by srosse's avatar srosse
Browse files

OO-291: make the area working + unit tests too

parent c76c5cb7
No related branches found
No related tags found
No related merge requests found
......@@ -73,6 +73,8 @@ public interface BGAreaManager {
* @return The area or null if the area does not exists
*/
public abstract BGArea findBGArea(String areaName, OLATResource resource);
public BGArea loadArea(Long key);
/**
* Update the given area in the database
......
......@@ -42,6 +42,7 @@ import org.olat.core.commons.persistence.DBFactory;
import org.olat.core.id.Identity;
import org.olat.core.logging.OLATRuntimeException;
import org.olat.core.manager.BasicManager;
import org.olat.core.util.StringHelper;
import org.olat.core.util.coordinate.CoordinatorManager;
import org.olat.core.util.coordinate.SyncerCallback;
import org.olat.core.util.coordinate.SyncerExecutor;
......@@ -101,6 +102,11 @@ public class BGAreaManagerImpl extends BasicManager implements BGAreaManager {
}
return areas;
}
@Override
public BGArea loadArea(Long key) {
return dbInstance.getCurrentEntityManager().find(BGAreaImpl.class, key);
}
/**
* @see org.olat.group.area.BGAreaManager#findBGArea(java.lang.String,
......@@ -219,8 +225,8 @@ public class BGAreaManagerImpl extends BasicManager implements BGAreaManager {
if(areas == null || areas.isEmpty()) return Collections.emptyList();
StringBuilder sb = new StringBuilder();
sb.append("select bgarel.businessGroup from ").append(BGtoAreaRelationImpl.class.getName()).append(" as bgarel ")
.append(" where bgarel.groupArea.key in (:areakeys)");
sb.append("select distinct bgarel.businessGroup from ").append(BGtoAreaRelationImpl.class.getName()).append(" as bgarel ")
.append(" where bgarel.groupArea.key in (:areaKeys)");
List<Long> areaKeys = new ArrayList<Long>();
for(BGArea area:areas) {
......@@ -241,17 +247,29 @@ public class BGAreaManagerImpl extends BasicManager implements BGAreaManager {
public List<BusinessGroup> findBusinessGroupsOfAreaAttendedBy(Identity identity, String areaName, OLATResource resource) {
StringBuilder sb = new StringBuilder();
sb.append("select bgi from ").append(BusinessGroupImpl.class.getName()).append(" as bgi ")
.append(", org.olat.basesecurity.SecurityGroupMembershipImpl as sgmi , org.olat.group.area.BGtoAreaRelationImpl as bgarel")
.append(", org.olat.basesecurity.SecurityGroupMembershipImpl as sgmi")
.append(", org.olat.group.area.BGtoAreaRelationImpl as bgarel")
.append(", org.olat.group.area.BGAreaImpl as area")
.append(" where area.name=:name and bgarel.businessGroup=bgi")
.append(" and bgarel.groupArea=area and bgi.partipiciantGroup=sgmi.securityGroup and sgmi.identity.key=:identityKey")
.append(" and area.resource.key=:resourceKey");
.append(" where bgarel.groupArea=area and bgi.partipiciantGroup=sgmi.securityGroup and bgarel.businessGroup=bgi")
.append(" and sgmi.identity.key=:identityKey");
List<BusinessGroup> groups = dbInstance.getCurrentEntityManager().createQuery(sb.toString(), BusinessGroup.class)
.setParameter("identityKey", identity.getKey())
.setParameter("resourceKey", resource.getKey())
.getResultList();
if(StringHelper.containsNonWhitespace(areaName)) {
sb.append(" and area.name=:name");
}
if(resource != null) {
sb.append(" and area.resource.key=:resourceKey");
}
TypedQuery<BusinessGroup> query = dbInstance.getCurrentEntityManager().createQuery(sb.toString(), BusinessGroup.class)
.setParameter("identityKey", identity.getKey());
if(StringHelper.containsNonWhitespace(areaName)) {
query.setParameter("name", areaName);
}
if(resource != null) {
query.setParameter("resourceKey", resource.getKey());
}
List<BusinessGroup> groups = query.getResultList();
return groups;
}
......@@ -267,7 +285,7 @@ public class BGAreaManagerImpl extends BasicManager implements BGAreaManager {
if(groups == null || groups.isEmpty()) return Collections.emptyList();
StringBuilder sb = new StringBuilder();
sb.append("select bgarel.groupArea from ").append(BGtoAreaRelationImpl.class.getName()).append(" as bgarel ")
sb.append("select distinct bgarel.groupArea from ").append(BGtoAreaRelationImpl.class.getName()).append(" as bgarel ")
.append("where bgarel.businessGroup.key in (:groupKeys)");
TypedQuery<BGArea> areaQuery = dbInstance.getCurrentEntityManager().createQuery(sb.toString(), BGArea.class);
......@@ -371,7 +389,7 @@ public class BGAreaManagerImpl extends BasicManager implements BGAreaManager {
*/
private void removeBGFromArea(Long businessGroupKey, Long bgAreaKey) {
StringBuilder sb = new StringBuilder();
sb.append("delete from ").append(BGtoAreaRelationImpl.class.getName()).append(" as bgarel where bgarel.groupArea.key=:areaKey and bgarel.businessGroup=:groupKey");
sb.append("delete from ").append(BGtoAreaRelationImpl.class.getName()).append(" as bgarel where bgarel.groupArea.key=:areaKey and bgarel.businessGroup.key=:groupKey");
dbInstance.getCurrentEntityManager().createQuery(sb.toString())
.setParameter("areaKey", bgAreaKey)
......
......@@ -25,7 +25,6 @@
package org.olat.group.ui.area;
import java.util.Iterator;
import java.util.List;
import org.apache.commons.lang.StringEscapeUtils;
......@@ -39,10 +38,7 @@ 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.gui.translator.Translator;
import org.olat.core.logging.activity.ThreadLocalUserActivityLogger;
import org.olat.core.util.Util;
import org.olat.group.BusinessGroup;
import org.olat.group.BusinessGroupService;
import org.olat.group.GroupLoggingAction;
......@@ -61,12 +57,7 @@ import org.olat.util.logging.activity.LoggingResourceable;
* @author gnaegi
*/
public class BGAreaEditController extends BasicController {
private static final String PACKAGE = Util.getPackageName(BGAreaEditController.class);
private static final String VELOCITY_ROOT = Util.getPackageVelocityRoot(PACKAGE);
// helpers
private Translator trans;
// GUI components
private TabbedPane tabbedPane;
private VelocityContainer editVC, detailsTabVC, groupsTabVC;
......@@ -76,7 +67,7 @@ public class BGAreaEditController extends BasicController {
// area, context and group references
private BGArea area;
private OLATResource resource;
private List allGroups, inAreaGroups;
private List<BusinessGroup> allGroups, inAreaGroups;
// managers
private final BGAreaManager areaManager;
private final BusinessGroupService businessGroupService;
......@@ -91,7 +82,6 @@ public class BGAreaEditController extends BasicController {
public BGAreaEditController(UserRequest ureq, WindowControl wControl, BGArea area) {
super(ureq, wControl);
this.trans = new PackageTranslator(PACKAGE, ureq.getLocale());
this.area = area;
areaManager = CoreSpringFactory.getImpl(BGAreaManager.class);
resource = area.getResource();
......@@ -113,44 +103,41 @@ public class BGAreaEditController extends BasicController {
* initialize the main velocity wrapper container
*/
private void initEditVC() {
editVC = new VelocityContainer("edit", VELOCITY_ROOT + "/edit.html", trans, this);
editVC = createVelocityContainer("edit");
editVC.put("tabbedpane", tabbedPane);
editVC.contextPut("title", trans.translate("area.edit.title", new String[] { StringEscapeUtils.escapeHtml(this.area.getName()).toString() }));
editVC.contextPut("title", translate("area.edit.title", new String[] { StringEscapeUtils.escapeHtml(area.getName()).toString() }));
}
/**
* initialize the area details tab
*/
private void initAndAddDetailsTab(UserRequest ureq, WindowControl wControl) {
this.detailsTabVC = new VelocityContainer("detailstab", VELOCITY_ROOT + "/detailstab.html", this.trans, this);
//TODO:pb: refactor BGControllerFactory.create..AreaController to be
//usefull here
if (this.areaController != null) {
removeAsListenerAndDispose(this.areaController);
}
this.areaController = new BGAreaFormController(ureq, wControl, this.area, false);
listenTo(this.areaController);
this.detailsTabVC.put("areaForm", this.areaController.getInitialComponent());
this.tabbedPane.addTab(this.trans.translate("tab.details"), this.detailsTabVC);
detailsTabVC = createVelocityContainer("detailstab");
removeAsListenerAndDispose(areaController);
areaController = new BGAreaFormController(ureq, wControl, area, false);
listenTo(areaController);
detailsTabVC.put("areaForm", areaController.getInitialComponent());
tabbedPane.addTab(translate("tab.details"), detailsTabVC);
}
/**
* initalize the group to area association tab
*/
private void initAndAddGroupsTab() {
groupsTabVC = new VelocityContainer("groupstab", VELOCITY_ROOT + "/groupstab.html", trans, this);
tabbedPane.addTab(trans.translate("tab.groups"), groupsTabVC);
groupsTabVC = createVelocityContainer("groupstab");
tabbedPane.addTab(translate("tab.groups"), groupsTabVC);
this.allGroups = businessGroupService.findBusinessGroups(null, null, false, false, resource, 0, -1);
this.inAreaGroups = areaManager.findBusinessGroupsOfArea(this.area);
this.groupsDataModel = new GroupsToAreaDataModel(this.allGroups, this.inAreaGroups);
allGroups = businessGroupService.findBusinessGroups(null, null, false, false, resource, 0, -1);
inAreaGroups = areaManager.findBusinessGroupsOfArea(area);
groupsDataModel = new GroupsToAreaDataModel(allGroups, inAreaGroups);
groupsChoice = new Choice("groupsChoice", trans);
groupsChoice = new Choice("groupsChoice", getTranslator());
groupsChoice.setSubmitKey("submit");
groupsChoice.setCancelKey("cancel");
groupsChoice.setTableDataModel(groupsDataModel);
groupsChoice.addListener(this);
groupsTabVC.put(groupsChoice);
groupsTabVC.put("groupsChoice", groupsChoice);
groupsTabVC.contextPut("noGroupsFound", (allGroups.size() > 0 ? Boolean.FALSE : Boolean.TRUE));
}
......@@ -160,15 +147,14 @@ public class BGAreaEditController extends BasicController {
*/
@Override
public void event(UserRequest ureq, Component source, Event event) {
if (source == this.groupsChoice) {
if (source == groupsChoice) {
if (event == Choice.EVNT_VALIDATION_OK) {
doUpdateGroupAreaRelations();
// do logging
if (this.inAreaGroups.size()==0) {
if (inAreaGroups.isEmpty()) {
ThreadLocalUserActivityLogger.log(GroupLoggingAction.BGAREA_UPDATED_NOW_EMPTY, getClass());
} else {
for (Iterator it = inAreaGroups.iterator(); it.hasNext();) {
BusinessGroup aGroup = (BusinessGroup) it.next();
for (BusinessGroup aGroup : inAreaGroups) {
ThreadLocalUserActivityLogger.log(GroupLoggingAction.BGAREA_UPDATED_MEMBER_GROUP, getClass(),
LoggingResourceable.wrap(aGroup));
}
......@@ -183,24 +169,19 @@ public class BGAreaEditController extends BasicController {
if (event == Event.DONE_EVENT) {
BGArea updatedArea = doAreaUpdate();
if (updatedArea == null) {
this.areaController.resetAreaName();
getWindowControl().setWarning(this.trans.translate("error.area.name.exists"));
areaController.resetAreaName();
getWindowControl().setWarning(translate("error.area.name.exists"));
} else {
this.area = updatedArea;
this.editVC.contextPut("title", this.trans.translate("area.edit.title", new String[] { StringEscapeUtils.escapeHtml(this.area.getName()).toString() }));
area = updatedArea;
editVC.contextPut("title", translate("area.edit.title", new String[] { StringEscapeUtils.escapeHtml(area.getName()).toString() }));
}
} else if (event == Event.CANCELLED_EVENT) {
// area might have been changed, reload from db
this.area = this.areaManager.reloadArea(this.area);
//TODO:pb: refactor BGControllerFactory.create..AreaController to be
//usefull here
if (this.areaController != null) {
removeAsListenerAndDispose(this.areaController);
}
this.areaController = new BGAreaFormController(ureq, getWindowControl(), this.area, false);
listenTo(this.areaController);
this.detailsTabVC.put("areaForm", this.areaController.getInitialComponent());
area = areaManager.reloadArea(area);
removeAsListenerAndDispose(areaController);
areaController = new BGAreaFormController(ureq, getWindowControl(), area, false);
listenTo(areaController);
detailsTabVC.put("areaForm", areaController.getInitialComponent());
}
}
}
......@@ -211,9 +192,9 @@ public class BGAreaEditController extends BasicController {
* @return the updated area
*/
public BGArea doAreaUpdate() {
this.area.setName(this.areaController.getAreaName());
this.area.setDescription(this.areaController.getAreaDescription());
return this.areaManager.updateBGArea(this.area);
area.setName(areaController.getAreaName());
area.setDescription(areaController.getAreaDescription());
return areaManager.updateBGArea(area);
}
/**
......@@ -221,25 +202,21 @@ public class BGAreaEditController extends BasicController {
*/
public void doUpdateGroupAreaRelations() {
// 1) add groups to area
List addedGroups = groupsChoice.getAddedRows();
Iterator iterator = addedGroups.iterator();
while (iterator.hasNext()) {
Integer position = (Integer) iterator.next();
List<Integer> addedGroups = groupsChoice.getAddedRows();
for (Integer position:addedGroups) {
BusinessGroup group = groupsDataModel.getGroup(position.intValue());
// refresh group to prevent stale object exception and context proxy
// issues
group = CoreSpringFactory.getImpl(BusinessGroupService.class).loadBusinessGroup(group);
// refresh group also in table model
this.allGroups.set(position.intValue(), group);
allGroups.set(position.intValue(), group);
// add group now to area and update in area group list
areaManager.addBGToBGArea(group, area);
this.inAreaGroups.add(group);
inAreaGroups.add(group);
}
// 2) remove groups from area
List removedGroups = groupsChoice.getRemovedRows();
iterator = removedGroups.iterator();
while (iterator.hasNext()) {
Integer position = (Integer) iterator.next();
List<Integer> removedGroups = groupsChoice.getRemovedRows();
for (Integer position:removedGroups) {
BusinessGroup group = groupsDataModel.getGroup(position.intValue());
areaManager.removeBGFromArea(group, area);
this.inAreaGroups.remove(group);
......
This diff is collapsed.
......@@ -37,7 +37,6 @@ import java.util.List;
import java.util.Set;
import java.util.UUID;
import org.apache.log4j.Logger;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
......@@ -51,6 +50,8 @@ import org.olat.collaboration.CollaborationToolsFactory;
import org.olat.core.commons.persistence.DBFactory;
import org.olat.core.id.Identity;
import org.olat.core.id.User;
import org.olat.core.logging.OLog;
import org.olat.core.logging.Tracing;
import org.olat.core.util.Encoder;
import org.olat.group.BusinessGroup;
import org.olat.group.BusinessGroupService;
......@@ -70,7 +71,7 @@ import org.springframework.beans.factory.annotation.Autowired;
public class BusinessGroupServiceImplTest extends OlatTestCase {
//
private static Logger log = Logger.getLogger(BusinessGroupServiceImplTest.class.getName());
private static OLog log = Tracing.createLoggerFor(BusinessGroupServiceImplTest.class);
/*
* ::Test Setup::
*/
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment