From 2c1a45a6b0e134e70f650a2c8eca2a4b36f0f01d Mon Sep 17 00:00:00 2001
From: fkiefer <none@none>
Date: Fri, 17 Jun 2016 15:43:04 +0200
Subject: [PATCH] enable choice of binders and owned sections enable floating
 entries with ownership

---
 .../modules/portfolio/PortfolioService.java   |   2 +-
 .../modules/portfolio/manager/PageDAO.java    |   9 +-
 .../manager/PortfolioServiceImpl.java         |   8 +-
 .../modules/portfolio/model/PageImpl.java     |   2 +-
 .../olat/modules/portfolio/model/PageRow.java |   8 +
 .../portfolio/ui/MyPageListController.java    |   2 +-
 .../modules/portfolio/ui/PageController.java  |   2 +-
 .../ui/PageMetadataEditController.java        | 139 ++++++++++++------
 .../portfolio/ui/_content/page_row.html       |   2 +-
 .../manager/PortfolioServiceTest.java         |   4 +-
 10 files changed, 123 insertions(+), 55 deletions(-)

diff --git a/src/main/java/org/olat/modules/portfolio/PortfolioService.java b/src/main/java/org/olat/modules/portfolio/PortfolioService.java
index dc7bb2fdcb2..c015a1aeefc 100644
--- a/src/main/java/org/olat/modules/portfolio/PortfolioService.java
+++ b/src/main/java/org/olat/modules/portfolio/PortfolioService.java
@@ -137,7 +137,7 @@ public interface PortfolioService {
 	 * @param summary
 	 * @param section
 	 */
-	public Page appendNewPage(String title, String summary, String imagePath, SectionRef section);
+	public Page appendNewPage(Identity owner, String title, String summary, String imagePath, SectionRef section);
 	
 	public Page getPageByKey(Long key);
 	
diff --git a/src/main/java/org/olat/modules/portfolio/manager/PageDAO.java b/src/main/java/org/olat/modules/portfolio/manager/PageDAO.java
index a75129d70f5..a891e5b79fc 100644
--- a/src/main/java/org/olat/modules/portfolio/manager/PageDAO.java
+++ b/src/main/java/org/olat/modules/portfolio/manager/PageDAO.java
@@ -142,18 +142,19 @@ public class PageDAO {
 	public List<Page> getOwnedPages(IdentityRef owner) {
 		StringBuilder sb = new StringBuilder();
 		sb.append("select page from pfpage as page")
-		  .append(" inner join page.section as section")
-		  .append(" inner join section.binder as binder")
 		  .append(" inner join fetch page.body as body")
+		  .append(" left join page.section as section")
+		  .append(" left join section.binder as binder")
 		  .append(" where exists (select pageMember from bgroupmember as pageMember")
 		  .append("     inner join pageMember.identity as ident on (ident.key=:ownerKey and pageMember.role='").append(GroupRoles.owner.name()).append("')")
-		  .append("  	where pageMember.group.key=binder.baseGroup.key or pageMember.group.key=page.baseGroup.key")
+		  .append("  	where pageMember.group.key=page.baseGroup.key or pageMember.group.key=binder.baseGroup.key or pageMember.group.key=page.baseGroup.key")
 		  .append(" )");
 		
-		return dbInstance.getCurrentEntityManager()
+		List<Page> pages = dbInstance.getCurrentEntityManager()
 			.createQuery(sb.toString(), Page.class)
 			.setParameter("ownerKey", owner.getKey())
 			.getResultList();
+		return pages;
 	}
 	
 	public Page loadByKey(Long key) {
diff --git a/src/main/java/org/olat/modules/portfolio/manager/PortfolioServiceImpl.java b/src/main/java/org/olat/modules/portfolio/manager/PortfolioServiceImpl.java
index dbd29aa7382..19c1597dbbd 100644
--- a/src/main/java/org/olat/modules/portfolio/manager/PortfolioServiceImpl.java
+++ b/src/main/java/org/olat/modules/portfolio/manager/PortfolioServiceImpl.java
@@ -282,9 +282,11 @@ public class PortfolioServiceImpl implements PortfolioService {
 	}
 
 	@Override
-	public Page appendNewPage(String title, String summary, String imagePath, SectionRef section) {
-		Section reloadedSection = binderDao.loadSectionByKey(section.getKey());
-		return pageDao.createAndPersist(title, summary, null, reloadedSection, null);
+	public Page appendNewPage(Identity owner, String title, String summary, String imagePath, SectionRef section) {
+		Section reloadedSection = section == null ? null : binderDao.loadSectionByKey(section.getKey());
+		Page page = pageDao.createAndPersist(title, summary, null, reloadedSection, null);
+		groupDao.addMembership(page.getBaseGroup(), owner, PortfolioRoles.owner.name());
+		return page;
 	}
 
 	@Override
diff --git a/src/main/java/org/olat/modules/portfolio/model/PageImpl.java b/src/main/java/org/olat/modules/portfolio/model/PageImpl.java
index b9212ecd195..d68b937050a 100644
--- a/src/main/java/org/olat/modules/portfolio/model/PageImpl.java
+++ b/src/main/java/org/olat/modules/portfolio/model/PageImpl.java
@@ -100,7 +100,7 @@ public class PageImpl implements Persistable, ModifiedInfo, CreateInfo, Page {
 	@JoinColumn(name="fk_group_id", nullable=false, insertable=true, updatable=false)
 	private Group baseGroup;
 	
-	@ManyToOne(targetEntity=SectionImpl.class,fetch=FetchType.LAZY,optional=false)
+	@ManyToOne(targetEntity=SectionImpl.class,fetch=FetchType.LAZY,optional=true)
 	@JoinColumn(name="fk_section_id", nullable=true, insertable=true, updatable=true)
 	private Section section;
 	
diff --git a/src/main/java/org/olat/modules/portfolio/model/PageRow.java b/src/main/java/org/olat/modules/portfolio/model/PageRow.java
index f40e554d59f..60cced6e6d5 100644
--- a/src/main/java/org/olat/modules/portfolio/model/PageRow.java
+++ b/src/main/java/org/olat/modules/portfolio/model/PageRow.java
@@ -70,8 +70,16 @@ public class PageRow {
 	public String getSectionTitle() {
 		return section.getTitle();
 	}
+	public boolean isSection () {
+		if(section == null){
+			return false;
+		}
+		return true;
+		
+	}
 	
 	public String getSectionLongTitle() {
+		
 		long pos = section.getPos();
 		return (pos+1) + ". " + section.getTitle();
 	}
diff --git a/src/main/java/org/olat/modules/portfolio/ui/MyPageListController.java b/src/main/java/org/olat/modules/portfolio/ui/MyPageListController.java
index 59c298cfaa1..8828d972f4a 100644
--- a/src/main/java/org/olat/modules/portfolio/ui/MyPageListController.java
+++ b/src/main/java/org/olat/modules/portfolio/ui/MyPageListController.java
@@ -101,7 +101,7 @@ public class MyPageListController extends AbstractPageListController {
 	private void doCreateNewPage(UserRequest ureq) {
 		if(newPageCtrl != null) return;
 		
-		newPageCtrl = new PageMetadataEditController(ureq, getWindowControl(), null, false, null, false);
+		newPageCtrl = new PageMetadataEditController(ureq, getWindowControl(), null, true, null, true);
 		listenTo(newPageCtrl);
 		
 		String title = translate("create.new.page");
diff --git a/src/main/java/org/olat/modules/portfolio/ui/PageController.java b/src/main/java/org/olat/modules/portfolio/ui/PageController.java
index 5529b1c0902..436ab3a7238 100644
--- a/src/main/java/org/olat/modules/portfolio/ui/PageController.java
+++ b/src/main/java/org/olat/modules/portfolio/ui/PageController.java
@@ -173,7 +173,7 @@ public class PageController extends FormBasicController implements TooledControl
 			binder = portfolioService.getBinderBySection(section);
 		}
 		
-		editMetadataCtrl = new PageMetadataEditController(ureq, getWindowControl(), binder, false, section, false, page);
+		editMetadataCtrl = new PageMetadataEditController(ureq, getWindowControl(), binder, true, section, true, page);
 		listenTo(editMetadataCtrl);
 		
 		String title = translate("edit.page.metadata");
diff --git a/src/main/java/org/olat/modules/portfolio/ui/PageMetadataEditController.java b/src/main/java/org/olat/modules/portfolio/ui/PageMetadataEditController.java
index cc2045006b2..c93f9132bb5 100644
--- a/src/main/java/org/olat/modules/portfolio/ui/PageMetadataEditController.java
+++ b/src/main/java/org/olat/modules/portfolio/ui/PageMetadataEditController.java
@@ -20,8 +20,10 @@
 package org.olat.modules.portfolio.ui;
 
 import java.io.File;
+import java.util.HashMap;
 import java.util.HashSet;
 import java.util.List;
+import java.util.Map;
 import java.util.Set;
 
 import org.olat.core.gui.UserRequest;
@@ -74,6 +76,10 @@ public class PageMetadataEditController extends FormBasicController {
 	
 	private final boolean chooseBinder;
 	private final boolean chooseSection;
+	private final boolean allowFloatingEntry;
+	
+	
+	private boolean floatingEntry = false;
 	
 	@Autowired
 	private PortfolioService portfolioService;
@@ -88,6 +94,7 @@ public class PageMetadataEditController extends FormBasicController {
 		
 		this.chooseBinder = chooseBinder;
 		this.chooseSection = chooseSection;
+		this.allowFloatingEntry = false;
 		
 		initForm(ureq);
 	}
@@ -104,6 +111,7 @@ public class PageMetadataEditController extends FormBasicController {
 		
 		this.chooseBinder = chooseBinder;
 		this.chooseSection = chooseSection;
+		this.allowFloatingEntry = false;
 		
 		initForm(ureq);
 	}
@@ -138,44 +146,45 @@ public class PageMetadataEditController extends FormBasicController {
 			}
 		}
 		
-		//list of binder
-		if(chooseBinder) {
+		// list of binder
+		if (chooseBinder) {
+			List<Binder> binders = portfolioService.searchOwnedBinders(getIdentity());
+
+			String[] theKeys = new String[binders.size()+1];
+			String[] theValues = new String[binders.size()+1];
+
+			for (int i = 0; i < binders.size(); ++i) {
+				theKeys[i] = binders.get(i).getKey().toString();
+				theValues[i] = StringHelper.escapeHtml(binders.get(i).getTitle());
+			} 
 			
+			theKeys[binders.size()] = "none";
+			theValues[binders.size()] = "none";
+
+			bindersEl = uifactory.addDropdownSingleselect("binders", "page.binders", formLayout, theKeys, theValues, null);
+			bindersEl.addActionListener(FormEvent.ONCHANGE);
+			
+			if (currentBinder == null) {
+				currentBinder = binders.get(0);
+			} else {
+				for (String key : theKeys) {
+					if (key.equals(currentBinder.getKey().toString()))
+						bindersEl.select(key, true);
+				}
+			}
 		} else {
+			
 			String[] theKeys = new String[] { currentBinder.getKey().toString() };
-			String[] theValues = new String[]{ StringHelper.escapeHtml(currentBinder.getTitle()) };
+			String[] theValues = new String[] { StringHelper.escapeHtml(currentBinder.getTitle()) };
+
 			bindersEl = uifactory.addDropdownSingleselect("binders", "page.binders", formLayout, theKeys, theValues, null);
 			bindersEl.setEnabled(false);
 		}
-		
+
 		//list of sections
 		if(chooseSection) {
-			if(chooseBinder) {
-				
-			} else {
-				List<Section> sections = portfolioService.getSections(currentBinder);
-				if(sections.isEmpty()) {
-					//wrong
-				} else {
-					int numOfSections = sections.size();
-					String selectedKey = null;
-					String[] theKeys = new String[numOfSections];
-					String[] theValues = new String[numOfSections];
-					for(int i=0; i<numOfSections; i++) {
-						Long sectionKey = sections.get(i).getKey();
-						theKeys[i] = sectionKey.toString();
-						theValues[i] = (i + 1) + ". " + StringHelper.escapeHtml(sections.get(i).getTitle());
-						if(currentSection != null && currentSection.getKey().equals(sectionKey)) {
-							selectedKey = theKeys[i];
-						}
-					}
-					sectionsEl = uifactory.addDropdownSingleselect("sections", "page.sections", formLayout, theKeys, theValues, null);
-					if(selectedKey != null) {
-						sectionsEl.select(selectedKey, true);
-					}
-				}
-			}	
-		} else {
+			retrieveSections(formLayout, true);
+		} else {// currently never used
 			String[] theKeys = new String[] { currentSection.getKey().toString() };
 			String[] theValues = new String[]{ StringHelper.escapeHtml(currentSection.getTitle()) };
 			sectionsEl = uifactory.addDropdownSingleselect("sections", "page.sections", formLayout, theKeys, theValues, null);
@@ -192,6 +201,42 @@ public class PageMetadataEditController extends FormBasicController {
 		}
 		uifactory.addFormCancelButton("cancel", buttonsCont, ureq, getWindowControl());
 	}
+	
+	protected void retrieveSections(FormItemContainer formLayout, boolean updateBox) {
+		
+		List<Section> sections = portfolioService.getSections(currentBinder);
+		if (sections.isEmpty()) {
+			// wrong
+		} else {
+			int numOfSections = sections.size();
+			String selectedKey = null;
+			String[] theKeys = new String[numOfSections];
+			String[] theValues = new String[numOfSections];
+			for (int i = 0; i < numOfSections; i++) {
+				Long sectionKey = sections.get(i).getKey();
+				theKeys[i] = sectionKey.toString();
+				theValues[i] = (i + 1) + ". " + StringHelper.escapeHtml(sections.get(i).getTitle());
+				if (currentSection != null && currentSection.getKey().equals(sectionKey)) {
+					selectedKey = theKeys[i];
+				}
+			}
+			
+			if (updateBox) {
+				sectionsEl = uifactory.addDropdownSingleselect("sections", "page.sections", formLayout, theKeys,
+						theValues, null);
+			} else {
+				sectionsEl.setKeysAndValues(theKeys, theValues, null);
+			}
+			
+			if (selectedKey != null) {
+				sectionsEl.select(selectedKey, true);
+			}
+		}
+	}
+	
+	protected void updateSections (){
+		
+	}
 
 	@Override
 	protected boolean validateFormLogic(UserRequest ureq) {
@@ -202,34 +247,37 @@ public class PageMetadataEditController extends FormBasicController {
 
 	@Override
 	protected void formOK(UserRequest ureq) {
-		if(page == null) {
+
+		if (page == null) {
 			String title = titleEl.getValue();
 			String summary = summaryEl.getValue();
 			SectionRef selectSection = null;
-			if(sectionsEl.isOneSelected() && sectionsEl.isEnabled()) {
+			if (sectionsEl.isOneSelected() && sectionsEl.isEnabled() && sectionsEl.isVisible()) {
 				String selectedKey = sectionsEl.getSelectedKey();
 				selectSection = new SectionKeyRef(new Long(selectedKey));
 			}
 			String imagePath = null;
-			if(fileUpload.getUploadFile() != null) {
-				imagePath = portfolioService.addPosterImageForPage(fileUpload.getUploadFile(), fileUpload.getUploadFileName());
+			if (fileUpload.getUploadFile() != null) {
+				imagePath = portfolioService.addPosterImageForPage(fileUpload.getUploadFile(),
+						fileUpload.getUploadFileName());
 			}
-			portfolioService.appendNewPage(title, summary, imagePath, selectSection);
+			portfolioService.appendNewPage(getIdentity(), title, summary, imagePath, selectSection);
 		} else {
 			page.setTitle(titleEl.getValue());
 			page.setSummary(summaryEl.getValue());
 
-			if(fileUpload.getUploadFile() != null) {
-				String imagePath = portfolioService.addPosterImageForPage(fileUpload.getUploadFile(), fileUpload.getUploadFileName());
+			if (fileUpload.getUploadFile() != null) {
+				String imagePath = portfolioService.addPosterImageForPage(fileUpload.getUploadFile(),
+						fileUpload.getUploadFileName());
 				page.setImagePath(imagePath);
-			} else if(fileUpload.getInitialFile() == null) {
+			} else if (fileUpload.getInitialFile() == null) {
 				page.setImagePath(null);
 				portfolioService.removePosterImage(page);
 			}
-			
+
 			page = portfolioService.updatePage(page);
 		}
-		
+
 		fireEvent(ureq, Event.DONE_EVENT);
 	}
 
@@ -252,6 +300,15 @@ public class PageMetadataEditController extends FormBasicController {
 					}
 				}
 			}
+		} else if (bindersEl == source) {
+			if (bindersEl.getSelectedKey().equals("none")) {
+				sectionsEl.setVisible(false);
+				currentBinder = null;
+			} else {
+				currentBinder = portfolioService.searchOwnedBinders(getIdentity()).get(bindersEl.getSelected());
+				sectionsEl.setVisible(true);
+				retrieveSections(flc, false);
+			}
 		}
 	}
-}
+}
\ No newline at end of file
diff --git a/src/main/java/org/olat/modules/portfolio/ui/_content/page_row.html b/src/main/java/org/olat/modules/portfolio/ui/_content/page_row.html
index afa6bb04147..3b412893385 100644
--- a/src/main/java/org/olat/modules/portfolio/ui/_content/page_row.html
+++ b/src/main/java/org/olat/modules/portfolio/ui/_content/page_row.html
@@ -1,4 +1,4 @@
-#if($row.firstPageOfSection)
+#if(${row.isSection()} && $row.firstPageOfSection)
 <h3>$r.escapeHtml($row.sectionLongTitle)</h3>
 <div class="o_portfolio_section_meta">
 	<div class="clearfix">
diff --git a/src/test/java/org/olat/modules/portfolio/manager/PortfolioServiceTest.java b/src/test/java/org/olat/modules/portfolio/manager/PortfolioServiceTest.java
index 5f0a4be86a7..38b07aeb872 100644
--- a/src/test/java/org/olat/modules/portfolio/manager/PortfolioServiceTest.java
+++ b/src/test/java/org/olat/modules/portfolio/manager/PortfolioServiceTest.java
@@ -103,7 +103,7 @@ public class PortfolioServiceTest extends OlatTestCase {
 		dbInstance.commit();
 		List<Section> sections = portfolioService.getSections(binder);
 		Section section = sections.get(0);
-		portfolioService.appendNewPage("Reviewed page", "", null, section);
+		portfolioService.appendNewPage(owner, "Reviewed page", "", null, section);
 		portfolioService.addAccessRights(section, coach, PortfolioRoles.coach);
 		
 		dbInstance.commit();
@@ -147,7 +147,7 @@ public class PortfolioServiceTest extends OlatTestCase {
 		dbInstance.commit();
 		List<Section> sections = portfolioService.getSections(binder);
 		Section section = sections.get(0);
-		portfolioService.appendNewPage("Reviewed page", "", null, section);
+		portfolioService.appendNewPage(owner, "Reviewed page", "", null, section);
 		portfolioService.addAccessRights(section, identity, PortfolioRoles.coach);
 		
 		dbInstance.commit();
-- 
GitLab