/**
* <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.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.ui.PortfolioHomeController;
import org.springframework.beans.factory.annotation.Autowired;
/**
*
* Initial date: 03.08.2016<br>
* @author srosse, stephane.rosse@frentix.com, http://www.frentix.com
*
*/
public class StandardEditMediaController extends FormBasicController {
private TextElement titleEl;
private TextElement descriptionEl;
private TextBoxListElement categoriesEl;
private Media mediaReference;
private final String businessPath;
private Map<String,String> categories = new HashMap<>();
@Autowired
private PortfolioService portfolioService;
public StandardEditMediaController(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);
}
@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.addRichTextElementForStringData("artefact.descr", "artefact.descr", desc, 8, 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 void formOK(UserRequest ureq) {
mediaReference.setTitle(titleEl.getValue());
mediaReference.setDescription(descriptionEl.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);
}
}