/** * <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.portfolio.ui.media; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import org.olat.core.gui.UserRequest; import org.olat.core.gui.components.form.flexible.FormItemContainer; import org.olat.core.gui.components.form.flexible.elements.RichTextElement; import org.olat.core.gui.components.form.flexible.elements.TextBoxListElement; import org.olat.core.gui.components.form.flexible.elements.TextElement; import org.olat.core.gui.components.form.flexible.impl.FormBasicController; import org.olat.core.gui.components.form.flexible.impl.FormLayoutContainer; 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.id.context.BusinessControlFactory; import org.olat.core.util.Formatter; import org.olat.core.util.StringHelper; import org.olat.core.util.Util; import org.olat.modules.portfolio.Category; import org.olat.modules.portfolio.Media; import org.olat.modules.portfolio.PortfolioService; import org.olat.modules.portfolio.handler.TextHandler; import org.olat.modules.portfolio.model.MediaPart; import org.olat.modules.portfolio.ui.PortfolioHomeController; import org.olat.modules.portfolio.ui.editor.AddElementInfos; import org.olat.modules.portfolio.ui.editor.PageElement; import org.olat.modules.portfolio.ui.editor.PageElementAddController; import org.springframework.beans.factory.annotation.Autowired; /** * * Initial date: 24.06.2016<br> * @author srosse, stephane.rosse@frentix.com, http://www.frentix.com * */ public class CollectTextMediaController extends FormBasicController implements PageElementAddController { private TextElement titleEl; private RichTextElement descriptionEl, textEl; private TextBoxListElement categoriesEl; private Media mediaReference; private Map<String,String> categories = new HashMap<>(); private final String businessPath; private AddElementInfos userObject; @Autowired private TextHandler fileHandler; @Autowired private PortfolioService portfolioService; public CollectTextMediaController(UserRequest ureq, WindowControl wControl) { super(ureq, wControl); setTranslator(Util.createPackageTranslator(PortfolioHomeController.class, getLocale(), getTranslator())); businessPath = "[HomeSite:" + getIdentity().getKey() + "][PortfolioV2:0][MediaCenter:0]"; initForm(ureq); } public CollectTextMediaController(UserRequest ureq, WindowControl wControl, Media media) { super(ureq, wControl); setTranslator(Util.createPackageTranslator(PortfolioHomeController.class, getLocale(), getTranslator())); businessPath = media.getBusinessPath(); mediaReference = media; List<Category> categoryList = portfolioService.getCategories(media); for(Category category:categoryList) { categories.put(category.getName(), category.getName()); } initForm(ureq); } public Media getMediaReference() { return mediaReference; } @Override public AddElementInfos getUserObject() { return userObject; } @Override public void setUserObject(AddElementInfos userObject) { this.userObject = userObject; } @Override public PageElement getPageElement() { MediaPart part = new MediaPart(); part.setMedia(mediaReference); return part; } @Override protected void initForm(FormItemContainer formLayout, Controller listener, UserRequest ureq) { String title = mediaReference == null ? null : mediaReference.getTitle(); titleEl = uifactory.addTextElement("artefact.title", "artefact.title", 255, title, formLayout); titleEl.setMandatory(true); String desc = mediaReference == null ? null : mediaReference.getDescription(); descriptionEl = uifactory.addRichTextElementForStringDataMinimalistic("artefact.descr", "artefact.descr", desc, 6, 60, formLayout, getWindowControl()); descriptionEl.getEditorConfiguration().setPathInStatusBar(false); String content = mediaReference == null ? null : mediaReference.getContent(); textEl = uifactory.addRichTextElementForStringData("artefact.text", "artefact.text", content, 10, 6, false, null, null, formLayout, ureq.getUserSession(), getWindowControl()); categoriesEl = uifactory.addTextBoxListElement("categories", "categories", "categories.hint", categories, formLayout, getTranslator()); categoriesEl.setHelpText(translate("categories.hint")); categoriesEl.setElementCssClass("o_sel_ep_tagsinput"); categoriesEl.setAllowDuplicates(false); Date collectDate = mediaReference == null ? new Date() : mediaReference.getCollectionDate(); String date = Formatter.getInstance(getLocale()).formatDate(collectDate); uifactory.addStaticTextElement("artefact.collect.date", "artefact.collect.date", date, formLayout); if(StringHelper.containsNonWhitespace(businessPath)) { String link = BusinessControlFactory.getInstance().getURLFromBusinessPathString(businessPath); uifactory.addStaticTextElement("artefact.collect.link", "artefact.collect.link", link, formLayout); } FormLayoutContainer buttonsCont = FormLayoutContainer.createButtonLayout("buttons", getTranslator()); formLayout.add(buttonsCont); uifactory.addFormSubmitButton("save", "save", buttonsCont); uifactory.addFormCancelButton("cancel", buttonsCont, ureq, getWindowControl()); } @Override protected void doDispose() { // } @Override protected boolean validateFormLogic(UserRequest ureq) { boolean allOk = true; return allOk & super.validateFormLogic(ureq); } @Override protected void formOK(UserRequest ureq) { if(mediaReference == null) { String title = titleEl.getValue(); String description = descriptionEl.getValue(); String content = textEl.getValue(); mediaReference = fileHandler.createMedia(title, description, content, businessPath, getIdentity()); } else { mediaReference.setTitle(titleEl.getValue()); mediaReference.setDescription(descriptionEl.getValue()); mediaReference.setContent(textEl.getValue()); mediaReference = portfolioService.updateMedia(mediaReference); } List<String> updatedCategories = categoriesEl.getValueList(); portfolioService.updateCategories(mediaReference, updatedCategories); fireEvent(ureq, Event.DONE_EVENT); } @Override protected void formCancelled(UserRequest ureq) { fireEvent(ureq, Event.CANCELLED_EVENT); } }