=======================================================The first file 
Edit.Java====================================================================

/**
 * Article Edit page
 *
 * @author <a href="mailto:isid...@awraqpublishing.com";>Ismail O. M. Sidiya</a>
 * @version 1.0
 */
@Secured(Role.ROLE_USER)
public class ArticleEdit
{
  @Inject
  private Logger logger;

  @Inject
  private UserManager userManager;

  @Inject
  private SiteManager siteManager;

  @Inject
  private ArticleManager articleManager;

  @Inject
  private SectionManager sectionManager;

  @Inject
  private ImageManager imageManager;

  @Inject
  private ImageConverter imageConverter;

  @SuppressWarnings("unused")
  @Inject
  private PropertyAccess propertyAccess;

  @SuppressWarnings("unused")
  @Inject
  private ComponentResources componentResources;


  @Inject
  private PageRenderLinkSource pageRenderLinkSource;

  @Inject
  private Messages messages;

  @InjectPage
  private ArticleList articleList;

  @InjectComponent
  private Form form;

  @Persist()
  @Property
  private String id;

  @Persist
  @Property
  @Validate("required")
  private String name;

  @Persist
  @Property
  private Section section;

  @Persist
  @Property
  @Validate("required")
  private Date date;

  @Persist
  @Property
  @Validate("required")
  private WorkflowStatus publishStatus;

  @Persist
  @Property
  private Date publishDate;

  @Persist
  @Property
  private Date expiryDate;

  @Persist
  @Property
  private String authorName;

  @Persist
  @Property
  private String city;

  @Persist
  @Property
  private String source;

  @Persist
  @Property
  @Validate("required")
  private String title;

  @Persist
  @Property
  private String shortTitle;

  @Persist
  @Property
  private String subTitle;

  @Persist
  @Property
  private String lead;

  @Persist
  @Property
  private String body;

  @Persist
  @Property
  private String caption;

  @Persist
  @Property
  private List<Image> tempImages;


  @SuppressWarnings("unused")
  @Property
  private SelectModel sectionModel;

  @SuppressWarnings("unused")
  @Property
  private ValueEncoder<Section> sectionEncoder;

  private List<String> authorNames;
  private List<String> cities;
  private List<String> sources;

  @Persist
  private boolean showsamePage;


  //Helper variables
  @Property
  private UploadedFile file;
  @SuppressWarnings("unused")
  @Property
  private Image image;
  @SuppressWarnings("unused")
  @Property
  private int index;
  // end Helper variables

  /**
   * @return
   */
  public WorkflowStatus getPublishStatusDraft()
  {
    return WorkflowStatus.DRAFT;
  }

  /**
   * @return
   */
  public WorkflowStatus getPublishStatusPublic()
  {
    return WorkflowStatus.PUBLIC;
  }

  void onPrepare()
  {
                if(!showsamePage){
                        Article article = null;
                        try {
                                article = articleManager.get(id);
                        } catch (Exception ex) {
                                logger.warn("Invalid article id", ex);
                        }

                        if (article != null) {
                                name = article.getName();
                                Page page = article.getPage();
                                if (page instanceof Section) {
                                        section = (Section) page;
                                }
                                date = article.getDate();
                                publishStatus = article.getPublishStatus();
                                publishDate = article.getPublishDate();
                                expiryDate = article.getExpiryDate();
                                authorName = article.getAuthorName();
                                city = article.getCity();
                                source = article.getSource();
                                title = article.getTitle();
                                shortTitle = article.getShortTitle();
                                subTitle = article.getSubTitle();
                                lead = article.getLead();
                                body = article.getBody();
                                tempImages = article.getImages();
                        }
                }

                User user = userManager.getCurrentUser();
                List<Section> sections = 
sectionManager.getSiteSections(user.getSite());

                // sectionModel =
                // new PropertyAccessSelectionModel<Section>(sections, 
propertyAccess,
                // "name");
                sectionModel = new SectionSelectionModel(sections, messages
                                .get("padding.select-option-tree"));
                sectionEncoder = new 
PersistableValueEncoder<Section>(sectionManager);
  }

  void onValidateForm()
  {
    Article article = null;
    try {
      article = articleManager.get(id);
    }
    catch (Exception ex) {
      logger.warn("Invalid article id", ex);
    }
    if (article == null) {
      form.recordError("id-error");
    }
  }


        private Object cancel() {
                // clean presisted images
                tempImages = null;
                return articleList;
        }

        private Object upload() {
                // on upload make sure that the image is presiste in tempImages
                // and echo the other fields
                Article article = null;
                try {
                        article = articleManager.get(id);
                } catch (Exception ex) {
                        logger.warn("Invalid article id", ex);
                }

                if (article == null) {
                        form.recordError(messages.get("id-error"));
                        return null;
                }
                File contentFile = null;
                try {
                        File diskFile = UploadUtils.toDiskFile(file);
                        contentFile = imageConverter.convert(diskFile);
                        if (contentFile == null || !contentFile.exists()) {
                                contentFile = diskFile;
                        }
                } catch (IOException ex) {
                        logger.warn("Invalid image", ex);
                }

                Image newImage = null;
                try {
                        if (contentFile != null && contentFile.exists()) {
                                newImage = new Image(file.getFileName(), 
file.getContentType(),
                                                caption, contentFile);
                                tempImages.add(newImage);
                        }
                } catch (IOException ex) {
                        logger.warn("Invalid image", ex);
                }
                return this;
        }

        private Object submit() {
                Article article = null;
                try {
                        article = articleManager.get(id);
                } catch (Exception ex) {
                        logger.warn("Invalid article id", ex);
                }

                if (article == null) {
                        form.recordError(messages.get("id-error"));
                        return null;
                }

                // Section oldSection = article.getSection();
                // if (!ObjectUtils.equals(oldSection, section)) {
                // if (oldSection != null) {
                // oldSection.getArticles().remove(article);
                // }
                // }

                article.setName(name);
                article.setPage(section);
                article.setDate(date);
                article.setPublishStatus(publishStatus);
                article.setPublishDate(publishDate);
                article.setExpiryDate(expiryDate);
                article.setAuthorName(authorName);
                article.setCity(city);
                article.setSource(source);
                article.setTitle(title);
                article.setShortTitle(shortTitle);
                article.setSubTitle(subTitle);
                article.setLead(lead);
                article.setBody(body);

                File contentFile = null;
                try {
                        File diskFile = UploadUtils.toDiskFile(file);
                        contentFile = imageConverter.convert(diskFile);
                        if (contentFile == null || !contentFile.exists()) {
                                contentFile = diskFile;
                        }
                } catch (IOException ex) {
                        logger.warn("Invalid image", ex);
                }

                Image newImage = null;
                try {
                        if (contentFile != null && contentFile.exists()) {
                                newImage = new Image(file.getFileName(), 
file.getContentType(),
                                                caption, contentFile);
                                newImage = imageManager.save(newImage);
                        }
                } catch (IOException ex) {
                        logger.warn("Invalid image", ex);
                }

                if (newImage != null) {
                        if (!CollectionUtils.isEmpty(article.getImages())) {
                                article.getImages().remove(0);
                                article = articleManager.save(article);
                        }
                        article.addImage(newImage);
                }
                article = articleManager.save(article);
                if (!StringUtils.isEmpty(caption)) {
                        Image image = article.getFisrtImage();
                        if (image != null) {
                                image = imageManager.get(image.getId());
                                image.setCaption(caption);
                                imageManager.save(image);
                        }
                }

                updateSite();

                articleList.getLayout().writeMessage(
                                messages.format("article-updated", 
article.getName()));

                return articleList;
        }

        /**
         * Remove image by index
         *
         * @param id
         * @param articleid
         * @return
         */
        Object onActionFromRemoveImage(int index) {
                tempImages.remove(index);
                return null;
        }




  @OnEvent(component="upload")
  Object onSelectFromUpload(){
          showsamePage = true;
         return upload();
  }

  @OnEvent(component="cancel")
  Object onSelectFromCancel(){
          showsamePage = false;
         return cancel();
  }

  @OnEvent(component="submit")
  Object onSelectFromSubmit(){
          showsamePage = false;
         return submit();
  }

  @OnEvent(component="submitTop")
  Object onSelectFromSubmitTop(){
          return onSelectFromSubmit();
  }

  @OnEvent(component="cancelTop")
  Object onSelectFromCancelTop(){
          return onSelectFromCancel();
  }

  public int getTempImageSize(){
          return tempImages.size()-1;
  }

}
===================================================End of 
Edit.Java======================================================================
===================================================The template 
file=====================================================================
<html xmlns:t="http://tapestry.apache.org/schema/tapestry_5_1_0.xsd";
      xmlns:p="tapestry:parameter"
      t:type="layout"
      t:id="layout"
      t:title="message:page.title">

  <div class="table">

    <t:form t:id="form"
            t:context="id">
      <t:errors/>
         <fieldset class="button">
        <input t:type="submit" value="${message:submit-label}" 
t:id="submitTop"/>
        <input t:type="submit" value="${message:cancel-label}" 
t:id="cancelTop"/>
      </fieldset>
      <fieldset class="panel">
        <fieldset class="field name required">
          <t:label t:for="name"/>
          <t:textfield t:id="name"
                       t:value="name"
                       t:validate="required"
                       size="32"
                       maxlength="128"
                       class="medium"/>
        </fieldset>
        <fieldset class="field section">
          <t:label t:for="section"/>
          <t:select t:id="section"
                    t:model="sectionModel"
                    t:encoder="sectionEncoder"
                    t:value="section"
                    class="large"/>
        </fieldset>
        <fieldset class="field date required">
          <t:label t:for="date"/>
          <input t:type="dmi/datetimefield"
                 t:id="date"
                 t:value="date"
                 t:validate="required"
                 datePattern="yyyy-MM-dd HH:mm"
                 timePicker="true"
                 timePickerAdjacent="true"
                 use24hrs="false"
                 size="16"
                 maxlength="16"
                 class="small"/>
          <!--t:datefield t:id="date"
                       t:value="date"
                       t:validate="required"
                       size="16"
                       maxlength="16"
                       class="small"/-->
        </fieldset>
      </fieldset>
     <!-- <fieldset class="panel">
        <fieldset class="field publishStatus">
          <t:label t:for="publishStatus"/>
          <t:radiogroup t:id="publishStatus"
                        t:label="message:publishStatus-label"
                        t:value="publishStatus">
            <fieldset class="radio">
              <t:radio t:id="publishStatusDraft"
                       t:label="message:publishStatus.draft-label"
                       t:value="publishStatusDraft"/>
              <t:label t:for="publishStatusDraft"/>
              <t:radio t:id="publishStatusPublic"
                       t:label="message:publishStatus.public-label"
                       t:value="publishStatusPublic"/>
              <t:label t:for="publishStatusPublic"/>
            </fieldset>
          </t:radiogroup>
        </fieldset>

        <fieldset class="field date publishDate">
          <t:label t:for="publishDate"/>
          <input t:type="dmi/datetimefield"
                 t:id="publishDate"
                 t:value="publishDate"
                 datePattern="yyyy-MM-dd HH:mm"
                 timePicker="true"
                 timePickerAdjacent="true"
                 use24hrs="false"
                 size="16"
                 maxlength="16"
                 class="small"/>
        </fieldset>

        <fieldset class="field date expiryDate">
          <t:label t:for="expiryDate"/>
          <input t:type="dmi/datetimefield"
                 t:id="expiryDate"
                 t:value="expiryDate"
                 datePattern="yyyy-MM-dd HH:mm"
                 timePicker="true"
                 timePickerAdjacent="true"
                 use24hrs="false"
                 size="16"
                 maxlength="16"
                 class="small"/>
        </fieldset>
      </fieldset> -->
      <fieldset class="panel">
        <fieldset class="field authorName">
          <t:label t:for="authorName"/>
          <t:textfield t:id="authorName"
                       t:value="authorName"
                       t:mixins="autocomplete"
                       maxlength="128"
                       class="medium"/>
        </fieldset>
        <fieldset class="field city">
          <t:label t:for="city"/>
          <t:textfield t:id="city"
                       t:value="city"
                       t:mixins="autocomplete"
                       maxlength="128"
                       class="medium"/>
        </fieldset>
        <fieldset class="field source">
          <t:label t:for="source"/>
          <t:textfield t:id="source"
                       t:value="source"
                       t:mixins="autocomplete"
                       maxlength="128"
                       class="medium"/>
        </fieldset>
      </fieldset>
      <fieldset class="panel">
        <fieldset class="field title required">
          <t:label t:for="title"/>
          <t:textfield t:id="title"
                       t:value="title"
                       t:validate="required"
                       maxlength="256"
                       class="large"/>
        </fieldset>
      </fieldset>
      <fieldset class="panel">
       <!-- <fieldset class="field shortTitle">
          <t:label t:for="shortTitle"/>
          <t:textfield t:id="shortTitle"
                       t:value="shortTitle"
                       maxlength="256"
                       class="large"/>
        </fieldset> -->
        <fieldset class="field subTitle">
          <t:label t:for="subTitle"/>
          <t:textfield t:id="subTitle"
                       t:value="subTitle"
                       maxlength="256"
                       class="large"/>
        </fieldset>
      </fieldset>
      <fieldset class="panel">
        <fieldset class="field lead">
          <t:label t:for="lead"/>
          <t:textarea t:id="lead"
                      t:value="lead"
                      rows="3"
                      class="large"/>
        </fieldset>
      </fieldset>
      <fieldset class="panel">
        <fieldset class="field editor body">
          <t:label t:for="body"/>
          <textarea t:type="ck/editor"
                    t:id="body"
                    t:value="body"
                    customConfiguration="asset:../../components/FCKConfig.js"
                    width="800px"
                    height="400px"
                    toolbarSet="Default"/>
          <!--textarea t:type="easyck/ckeditor"
                    t:id="body"
                    t:value="body"
                    height="400px"
                    toolbarSet="Basic"/-->
          <!--t:textarea t:id="body"
                      t:value="body"
                      rows="10"
                      class="large"/-->
        </fieldset>
      </fieldset>
      <fieldset class="panel image">
        <label>${message:image-label}</label>
        <fieldset class="file file">
          <t:label t:for="file"/>
          <input t:type="upload"
                 t:id="file"
                 class="large"/>
          <input t:type="submit" value="${message:upload-label}" t:id="upload"/>
          <t:if test="tempImages">
                <t:loop source="tempImages" value="image">
                <t:count start="0" end="${tempImageSize}" value="index">
                <div class="image">
                        <t:pagelink t:page="image/view" t:context="image.id" 
title="${image.caption}" rel="lightbox"><t:imageproxy 
image="image"/></t:pagelink>
                        <span class="caption">${image.caption}</span>
                </div>
                                <t:actionlink t:id="removeImage" 
t:context="[index]" class="remove">${message:removeImage-link}</t:actionlink>
                        </t:count>
            </t:loop>
          </t:if>
        </fieldset>
        <fieldset class="field caption">
          <t:label t:for="caption"/>
          <t:textfield t:id="caption"
                       t:value="caption"
                       maxlength="256"
                       class="large"/>
        </fieldset>
      </fieldset>

      <fieldset class="button">
        <input t:type="submit" value="${message:submit-label}" t:id="submit"/>
        <input t:type="submit" value="${message:cancel-label}" t:id="cancel"/>
      </fieldset>
    </t:form>

  </div>

</html>
-----Original Message-----
From: Taha Hafeez [mailto:tawus.tapes...@gmail.com]
Sent: 05 أبريل, 2011 05:30 م
To: Tapestry users
Subject: Re: a form that have an upload, submit button

 attachment, where ?

Just paste the template/class inline...

regards
Taha


2011/4/5 Amr Mohamed Mahmoud Hassanien <amr.hassan...@dmi.ae>

> Hi again Taha,
>
> I spent more time as I told you to see if I did something wrong, and I
> tried all the options on
> http://mail-archives.apache.org/mod_mbox/tapestry-users/200703.mbox/browseralso,
>  and nothing worked.
>
> Please find the files attached... your help is much appericated
>
> Thanks
> Amr
>
> -----Original Message-----
> From: Taha Hafeez [mailto:tawus.tapes...@gmail.com]
> Sent: 05 أبريل, 2011 11:09 ص
> To: Tapestry users
> Subject: Re: a form that have an upload, submit button
>
> Can you share the full code/template of the page ?
>
> regards
> Taha
>
>
> On Tue, Apr 5, 2011 at 12:30 PM, Amr Mohamed Mahmoud Hassanien <
> amr.hassan...@dmi.ae> wrote:
>
> > Hello All,
> >
> >                   I have followed the example in
> > http://wiki.apache.org/tapestry/Tapestry5HowToUseForms to make
> > multiple submit buttons in a page and it doesn't work very well.
> >                 Actually my case is a little bit different where in
> > the same form I have an upload button which looks like that:
> >
> > <input t:type="submit" value="${message:upload-label}"
> > t:id="upload"/>
> >
> > And handlers as follows:
> >
> > void onSelectedFromUpload(){
> >        this.submissionType = SubmissionType.UPLOAD;  }
> >
> > public Object onSuccess() {
> >            switch (submissionType) {
> >            case SUBMIT:
> >                  return submit();
> >            case UPLOAD:
> >                  return upload();
> >            case CANCEL:
> >                  return cancel();
> >            default:
> >                  return submit();
> >            }
> >
> >      }
> >
> > I put breakpoints in both methods and start debugging and no
> > breakpoint got hit.
> >
> >
> > I am expecting that on clicking the Upload button, the form will be
> > Submitted in a "multipart/form-data" post. Please not that I have
> > two other submit buttons as follows:
> >
> >
> >        <input t:type="submit" value="${message:submit-label}"
> > t:id="submit"/>
> >
> >        <input t:type="submit" value="${message:cancel-label}"
> > t:id="cancel"/>
> >
> >
> >
> > What I am doing is that on clicking Upload, I get the file uploaded
> > and save it temporary in a persistent List(in the session) ,and on
> > Submit I permanently save it and on cancel I just discard the
> > uploaded
> files.
> >
> >
> >
> > Any idea why the breakpoints have not got hit?
> >
> > Do you think that this scenario is possible to be implemented? I
> > just suspect that having more that submit button , will send
> > "multipart/form-data" post on clicking at any of them.
> >
> >
> >
> > Regards,
> >
> > Amr
> >
> >
> >
> >
> >
> >
> >
> > ####################################################################
> > ## ##########################################
> > DISCLAIMER:
> > This message is for the named person's use only. It may contain
> > confidential information, proprietary in nature or legally
> > privileged information. All trade secret, know how, formulas,
> > researches, database, software, codes diagrams, documentations,
> > attachments, voice, concepts and visual content are strictly
> > protected by United Arab Emirates Laws and Dubai Media Incorporated
> > codes which will have the right to take any legal action if you fail
> > in doing the hereunder steps. If you receive this message in error,
> > please immediately DELETE it and all copies of it from your system,
> > DESTROY any hard copies of it and destroy any soft and backup copy
> > of it saved in any kind of form under you possession and NOTIFY the sender.
> > You must not, directly or indirectly, use, disclose, distribute,
> > print, or copy any part of this message (email) if you are not the
> > intended recipient.
> >
> > ####################################################################
> > ## ##########################################
> >
>
>
> ######################################################################
> ##########################################
> DISCLAIMER:
> This message is for the named person's use only. It may contain
> confidential information, proprietary in nature or legally privileged
> information. All trade secret, know how, formulas, researches,
> database, software, codes diagrams, documentations, attachments,
> voice, concepts and visual content are strictly protected by United
> Arab Emirates Laws and Dubai Media Incorporated codes which will have
> the right to take any legal action if you fail in doing the hereunder
> steps. If you receive this message in error, please immediately DELETE
> it and all copies of it from your system, DESTROY any hard copies of
> it and destroy any soft and backup copy of it saved in any kind of
> form under you possession and NOTIFY the sender.
> You must not, directly or indirectly, use, disclose, distribute,
> print, or copy any part of this message (email) if you are not the
> intended recipient.
>
> ######################################################################
> ##########################################
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscr...@tapestry.apache.org
> For additional commands, e-mail: users-h...@tapestry.apache.org
>

################################################################################################################
DISCLAIMER:
This message is for the named person's use only. It may contain confidential 
information, proprietary 
in nature or legally privileged information. All trade secret, know how, 
formulas, researches, database, 
software, codes diagrams, documentations, attachments, voice, concepts and 
visual content are strictly 
protected by United Arab Emirates Laws and Dubai Media Incorporated codes which 
will have the right to 
take any legal action if you fail in doing the hereunder steps. If you receive 
this message in error, 
please immediately DELETE it and all copies of it from your system, DESTROY any 
hard copies of it and 
destroy any soft and backup copy of it saved in any kind of form under you 
possession and NOTIFY the sender.  
You must not, directly or indirectly, use, disclose, distribute, print, or copy 
any part of this message (email) 
if you are not the intended recipient.
################################################################################################################

Reply via email to