/**
* Copyright 2011 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.bricket.plugin.user_gallery.web;
import java.util.HashMap;
import java.util.Map;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.StringResourceModel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.apache.wicket.util.string.StringValueConversionException;
import org.bricket.plugin.user_gallery.domain.UserGallery;
import org.bricket.plugin.user_gallery.service.UserGalleryService;
import org.bricket.service.BricketServiceException;
import org.bricket.web.common.link.GenericLinkConfigPanel;
import brix.jcr.wrapper.BrixNode;
import brix.web.nodepage.BrixPageParameters;
import brix.web.nodepage.PageParametersAware;
/**
* @author Ingo Renner
* @author Henning Teek
*
*/
public class EditOrCreateUserGalleryPanel extends GenericLinkConfigPanel implements PageParametersAware {
@SpringBean
private UserGalleryService userGalleryService;
private long userGalleryId;
public EditOrCreateUserGalleryPanel(String id, IModel<BrixNode> tileNode) {
super(id, tileNode);
}
@Override
public void contributeToPageParameters(BrixPageParameters params) {
}
@Override
public void initializeFromPageParameters(BrixPageParameters params) {
try {
userGalleryId = params.getQueryParam(UserGalleryPageParameterDefinition.USER_GALLERY_PAGE_PARAMETER)
.toLong();
} catch (StringValueConversionException svce) {
userGalleryId = 0l;
}
}
@Override
protected void initGui() {
final UserGallery userGallery;
if (userGalleryId > 0l) {
userGallery = userGalleryService.loadUserGallery(userGalleryId);
} else {
userGallery = userGalleryService.createDomainObject();
}
Form<UserGallery> form = new Form<UserGallery>("form", new CompoundPropertyModel<UserGallery>(userGallery));
form.add(new AjaxLink<Void>("cancel") {
@Override
public void onClick(AjaxRequestTarget target) {
getSession().cleanupFeedbackMessages();
Map<String, Object> params = new HashMap<String, Object>();
params.put(UserGalleryPageParameterDefinition.USER_GALLERY_PAGE_PARAMETER, userGallery.getId());
handleCancel(params);
}
});
form.add(new AjaxButton("submit") {
@Override
protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
try {
UserGallery ug = userGalleryService.saveUserGallery((UserGallery) form.getModelObject());
form.info(new StringResourceModel("feedback.success", EditOrCreateUserGalleryPanel.this, null)
.getString());
Map<String, Object> params = new HashMap<String, Object>();
params.put(UserGalleryPageParameterDefinition.USER_GALLERY_PAGE_PARAMETER, ug.getId());
handleSave(params);
} catch (BricketServiceException e) {
form.error(new StringResourceModel(e.getKey(), EditOrCreateUserGalleryPanel.this, null).getString());
target.addComponent(getFeedback());
}
}
@Override
protected void onError(AjaxRequestTarget target, Form<?> form) {
target.addComponent(getFeedback());
}
});
add(form);
}
}