Sorry, i am not able to provide the url since it is an internal application
and corporate email is needed to be part of it.
Here is the code:
public class PostPanel extends Panel {
private static final Logger log =
LoggerFactory.getLogger(PostPanel.class);
private static final Long MAX_FILE_LENGTH = new Long(1048576);
private static final long serialVersionUID = 4297044401384008810L;
private static final IValidator<Integer> MINIMUM_VALIDATOR = new
RangeValidator<Integer>(
new Integer(0), new Integer(99999999));
private static final IValidator<Short> MINIMUM_VALIDATOR_SHORT = new
RangeValidator<Short>(
new Short((short) 0), new Short((short) 99));
private static final StringValidator TITLE_VALIDATOR = StringValidator
.lengthBetween(3, 100);
private static final StringValidator DESCRIPTION_VALIDATOR =
StringValidator
.lengthBetween(3, 500);
@Inject
private PostService postService;
@Inject
private CategoryService categoryService;
private List<FileUpload> photos;
private Post post;
private boolean editing = false;
private final Component feedback;
private String fileFeedbackMessage;
/**
* Constructor.
*
* @param id
* @param post
*/
public PostPanel(String id, final Post post) {
super(id);
// if not editing create a new empty post
if (post == null) {
MyAuthSession session = (MyAuthSession) WebSession.get();
this.post =
this.postService.createPostInstance(session.getCurrentUser());
} else {
this.post = post;
editing = true;
}
add(this.createFormTitle());
feedback = new FeedbackPanel("feedback");
feedback.setOutputMarkupPlaceholderTag(true);
final StatelessForm<Post> form = new
StatelessForm<Post>("createPostForm",
new CompoundPropertyModel<Post>(this.post));
form.add(feedback);
form.setMultiPart(true);
form.add(this.createTitleTextField());
form.add(this.createPriceTextField());
form.add(this.createCurrencySelect());
form.add(this.createPhotoPanel());
form.add(this.createFileUploader());
form.add(this.createDescriptionTextArea());
form.add(this.createPostButton());
form.add(new ExternalLink("cancelLink", "/my-posts?operation=ALL"));
form.add(createCategoryDropDownChoice());
form.add(createConditionSelectOption());
this.add(form);
}
public String getFileFeedbackMessage() {
return fileFeedbackMessage;
}
public void setFileFeedbackMessage(String fileFeedbackMessage) {
this.fileFeedbackMessage = fileFeedbackMessage;
}
public void copyPost(Post post) {
this.post.setPrice(post.getPrice());
this.post.setCurrency(post.getCurrency());
this.post.setPriceNatural(post.getPrice().intValue());
this.post.setPriceDecimal(post.getPrice().shortValue());
this.post.setTitle(post.getTitle());
this.post.setQuantity(post.getQuantity());
this.post.setDescription(post.getDescriptionSaved());
this.post.setDescriptionSaved(post.getDescriptionSaved());
this.post.setCategory(post.getCategory());
this.post.setCondition(post.getCondition());
BinaryInformation photo = post.getPhoto();
this.post.addPhoto(new BinaryInformation(photo.getContent(), photo
.getName(), photo.getContentType(), photo.getContent().length));
this.post.addThumbnail(new BinaryInformation(photo.getContent(),
"kea_thumbnail_" + photo.getName(), photo.getContentType(), photo
.getContent().length));
}
private Select<String> createConditionSelectOption() {
// Condition Drop Down Choice
Select<String> condition = new Select<String>("condition");
condition.add(new SelectOption<String>("new", new
Model<String>("new")));
condition.add(new SelectOption<String>("used", new
Model<String>("used")));
return condition;
}
private DropDownChoice<Void> createCategoryDropDownChoice() {
final List<Category> categoryList = categoryService.getCategories();
List<Long> catIds = new ArrayList<Long>();
// Categories Drop Down Choice
for (Category cat : categoryList) {
catIds.add(cat.getId());
}
@SuppressWarnings("unchecked")
DropDownChoice<Void> catDropDownChoice = new DropDownChoice(
"categoriesDropDown", new PropertyModel(this.post, "categoryId"),
catIds, new ChoiceRenderer<Long>() {
@Override
public String getDisplayValue(Long catId) {
for (Category cat : categoryList) {
if (cat.getId().equals(catId)) {
return cat.getName();
}
}
return "Select one!";
}
@Override
public String getIdValue(Long catId, int index) {
return catId.toString();
}
}) {
};
catDropDownChoice.setRequired(true).setEscapeModelStrings(false);
return catDropDownChoice;
}
private Select<String> createCurrencySelect() {
// TODO get the currency from a property file
Select<String> currency = new Select<String>("currency");
currency.add(new SelectOption<String>("pesos", new
Model<String>("ARS$")));
currency.add(new SelectOption<String>("dolares", new
Model<String>("US$")));
currency.add(new SelectOption<String>("pesosUruguayos", new
Model<String>(
"UYU$")));
currency.add(new SelectOption<String>("pesosColombianos",
new Model<String>("COP$")));
return currency;
}
private TextArea<String> createDescriptionTextArea() {
if (editing) {
post.setDescription(post.getDescriptionSaved());
}
TextArea<String> description = new TextArea<String>("description");
description.setRequired(true);
description.add(DESCRIPTION_VALIDATOR);
return description;
}
private TextField<Integer> createPriceTextField() {
if (editing) {
String strPrice = post.getPrice().toPlainString();
int idx = strPrice.indexOf(".");
strPrice = (idx >= 0) ? strPrice.substring(0, idx) : strPrice;
post.setPriceNatural(Integer.valueOf(strPrice));
}
TextField<Integer> price = new TextField<Integer>("priceNatural");
price.setType(Integer.class);
price.setRequired(true);
price.add(MINIMUM_VALIDATOR);
return price;
}
private TextField<Short> createPriceDecimalTextField() {
if (editing) {
String strPrice = post.getPrice().toPlainString();
int idx = strPrice.indexOf(".");
strPrice = (idx >= 0) ? strPrice.substring(idx + 1) : "00";
if (strPrice.length() > 2) {
strPrice = strPrice.substring(0, 2);
}
post.setPriceDecimal(Short.valueOf(strPrice));
}
TextField<Short> priceDecimal = new TextField<Short>("priceDecimal");
priceDecimal.setType(Short.class);
priceDecimal.setRequired(true);
priceDecimal.add(MINIMUM_VALIDATOR_SHORT);
return priceDecimal;
}
@SuppressWarnings("serial")
private AjaxButton createPostButton() {
AjaxButton but = new AjaxButton("createPostButton") {
@Inject
private ImageService imageService;
@Override
public void onError(AjaxRequestTarget target, Form<?> form) {
log.debug("....... onError");
feedback.add(new AttributeAppender("class", "alert alert-error"));
target.add(feedback);
}
@Override
public void onSubmit(AjaxRequestTarget target, Form<?> form) {
try {
log.debug("....... SUBMITTING");
Post postNew = (Post) getForm().getModel().getObject();
// Only for compatibility purpose
postNew.setPriceDecimal((short) 0);
if (photos == null) { // can be editing
postNew.addPhoto(post.getPhoto());
} else {
FileUpload photo = photos.get(0);
if (photo.getSize() >= MAX_FILE_LENGTH) {
error("La imagen del producto debe ser menor a 1MB.");
log.debug("PHOTO SIZE >= MAX_FILE_LENGTH");
feedback.add(new AttributeAppender("class", "alert
alert-error"));
target.add(feedback);
return;
} else {
log.debug("PHOTO NOT NULL");
String contentType = photo.getContentType();
// Verifies if there is a file uploaded, if it is checks
// thats its an image and the format
if (!contentType.contains("image")) {
error("Tipo de archivo invalido, por favor seleccionar una
imagen para subir.");
feedback
.add(new AttributeAppender("class", "alert
alert-error"));
target.add(feedback);
return;
} else {
log.debug(".........PHOTO UPLOADING");
log.debug("...... ON SUBMIT: Creating POST");
BinaryInformation bi = new
BinaryInformation(photo.getBytes(),
photo.getClientFileName(), photo.getContentType(),
photo.getSize());
postNew
.addPhoto(imageService.resizeImage(bi,
ImageSize.NORMAL));
postNew.addThumbnail(imageService.resizeImage(bi,
ImageSize.THUMBNAIL));
}
}
}
createPost(postNew);
} catch (Throwable e) {
log.error("SUBMIT ERROR:" + e);
}
}
};
String message = (editing) ? getDefaultString("postPanel.post")
: getDefaultString("postPanel.publish");
but.add(new AttributeModifier("value", message));
return but;
}
/**
* Saves posts in db
*
* @param post to save
* @author Nelson.Rey
*/
private void createPost(Post post) {
post.setQuantity(1);
log.debug("...... PANEL: Creating POST");
if (!editing) {
postService.create(post);
} else {
postService.update(post);
}
PageParameters parameters;
parameters = new PageParameters();
parameters.add("operation", "ALL");
setResponsePage(MyPostsPage.class, parameters);
}
private TextField<String> createTitleTextField() {
TextField<String> title = new TextField<String>("title");
title.setRequired(true);
title.add(TITLE_VALIDATOR);
return title;
}
private Label createFormTitle() {
return editing ? new Label("postProductTitle", "Editar Producto")
: new Label("postProductTitle", "");
}
private ViewPhotoPanel createPhotoPanel() {
ViewPhotoPanel photoPanel = null;
if (!editing)
photoPanel = new ViewPhotoPanel("viewPhotoPanel", new Long(0));
else
photoPanel = new ViewPhotoPanel("viewPhotoPanel",
post.getKey().getId());
photoPanel.setVisible(editing);
return photoPanel;
}
private FileUploaderPanel createFileUploader() {
FileUploaderPanel fileUploader = new
FileUploaderPanel("fileUploaderPanel",
new PropertyModel<List<FileUpload>>(this, "photos"));
fileUploader.setRequiredFile(!editing);
return fileUploader;
}
private String getDefaultString(String key) {
// String string =
//
componentStringResourceLoader.loadStringResource(SearchFilterPanel.class,
// key, getLocale(), getSession().getStyle(), getVariation());
ComponentStringResourceLoader loader = new
ComponentStringResourceLoader();
String string = loader.loadStringResource(this, key, getLocale(),
getSession().getStyle(), getVariation());
return string;
}
}
In the html:
<div class="control-group">
<label class="control-label"> </label>
<div class="form_buttons">
<input type="submit" class="btn btn-success post-submit"
wicket:id="createPostButton" ></input>
<wicket:message
key="postPanel.cancelLink">Cancel</wicket:message>
</div>
</div>
--
View this message in context:
http://apache-wicket.1842946.n4.nabble.com/AjaxButton-submit-issue-in-GAE-tp4662084p4662108.html
Sent from the Users forum mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]