KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > web > bean > CheckinCheckoutBean


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.web.bean;
18
19 import java.io.File JavaDoc;
20 import java.io.Serializable JavaDoc;
21 import java.text.MessageFormat JavaDoc;
22 import java.util.HashMap JavaDoc;
23 import java.util.Map JavaDoc;
24
25 import javax.faces.context.FacesContext;
26 import javax.faces.event.ActionEvent;
27 import javax.transaction.UserTransaction JavaDoc;
28
29 import org.alfresco.model.ContentModel;
30 import org.alfresco.repo.content.MimetypeMap;
31 import org.alfresco.repo.version.VersionModel;
32 import org.alfresco.service.cmr.coci.CheckOutCheckInService;
33 import org.alfresco.service.cmr.repository.ChildAssociationRef;
34 import org.alfresco.service.cmr.repository.ContentData;
35 import org.alfresco.service.cmr.repository.ContentReader;
36 import org.alfresco.service.cmr.repository.ContentService;
37 import org.alfresco.service.cmr.repository.ContentWriter;
38 import org.alfresco.service.cmr.repository.InvalidNodeRefException;
39 import org.alfresco.service.cmr.repository.NodeRef;
40 import org.alfresco.service.cmr.repository.NodeService;
41 import org.alfresco.service.cmr.version.Version;
42 import org.alfresco.service.cmr.version.VersionType;
43 import org.alfresco.web.app.Application;
44 import org.alfresco.web.app.context.UIContextService;
45 import org.alfresco.web.app.servlet.DownloadContentServlet;
46 import org.alfresco.web.bean.repository.Node;
47 import org.alfresco.web.bean.repository.Repository;
48 import org.alfresco.web.ui.common.Utils;
49 import org.alfresco.web.ui.common.component.UIActionLink;
50 import org.apache.commons.logging.Log;
51 import org.apache.commons.logging.LogFactory;
52
53 /**
54  * @author Kevin Roast
55  */

56 public class CheckinCheckoutBean
57 {
58    // ------------------------------------------------------------------------------
59
// Bean property getters and setters
60

61    /**
62     * @return Returns the BrowseBean.
63     */

64    public BrowseBean getBrowseBean()
65    {
66       return this.browseBean;
67    }
68    
69    /**
70     * @param browseBean The BrowseBean to set.
71     */

72    public void setBrowseBean(BrowseBean browseBean)
73    {
74       this.browseBean = browseBean;
75    }
76    
77    /**
78     * @return Returns the NodeService.
79     */

80    public NodeService getNodeService()
81    {
82       return this.nodeService;
83    }
84
85    /**
86     * @param nodeService The NodeService to set.
87     */

88    public void setNodeService(NodeService nodeService)
89    {
90       this.nodeService = nodeService;
91    }
92    
93    /**
94     * @return Returns the VersionOperationsService.
95     */

96    public CheckOutCheckInService getVersionOperationsService()
97    {
98       return this.versionOperationsService;
99    }
100    
101    /**
102     * @param versionOperationsService The VersionOperationsService to set.
103     */

104    public void setVersionOperationsService(CheckOutCheckInService versionOperationsService)
105    {
106       this.versionOperationsService = versionOperationsService;
107    }
108    
109    /**
110     * @return Returns the ContentService.
111     */

112    public ContentService getContentService()
113    {
114       return this.contentService;
115    }
116
117    /**
118     * @param contentService The ContentService to set.
119     */

120    public void setContentService(ContentService contentService)
121    {
122       this.contentService = contentService;
123    }
124    
125    /**
126     * @return The document node being used for the current operation
127     */

128    public Node getDocument()
129    {
130       return this.document;
131    }
132
133    /**
134     * @param document The document node to be used for the current operation
135     */

136    public void setDocument(Node document)
137    {
138       this.document = document;
139    }
140    
141    /**
142     * @return Returns the working copy Document.
143     */

144    public Node getWorkingDocument()
145    {
146       return this.workingDocument;
147    }
148    
149    /**
150     * @param workingDocument The working copy Document to set.
151     */

152    public void setWorkingDocument(Node workingDocument)
153    {
154       this.workingDocument = workingDocument;
155    }
156    
157    /**
158     * Determines whether the document being checked in has
159     * the versionable aspect applied
160     *
161     * @return true if the versionable aspect is applied
162     */

163    public boolean isVersionable()
164    {
165       return getDocument().hasAspect(ContentModel.ASPECT_VERSIONABLE);
166    }
167    
168    /**
169     * @param keepCheckedOut The keepCheckedOut to set.
170     */

171    public void setKeepCheckedOut(boolean keepCheckedOut)
172    {
173       this.keepCheckedOut = keepCheckedOut;
174    }
175    
176    /**
177     * @return Returns the keepCheckedOut.
178     */

179    public boolean getKeepCheckedOut()
180    {
181       return this.keepCheckedOut;
182    }
183    
184    /**
185     * @param minorChange The minorChange to set.
186     */

187    public void setMinorChange(boolean minorChange)
188    {
189       this.minorChange = minorChange;
190    }
191    
192    /**
193     * @return Returns the minorChange flag.
194     */

195    public boolean getMinorChange()
196    {
197       return this.minorChange;
198    }
199    
200    /**
201     * @return Returns the version history notes.
202     */

203    public String JavaDoc getVersionNotes()
204    {
205       return this.versionNotes;
206    }
207
208    /**
209     * @param versionNotes The version history notes to set.
210     */

211    public void setVersionNotes(String JavaDoc versionNotes)
212    {
213       this.versionNotes = versionNotes;
214    }
215    
216    /**
217     * @return Returns the selected Space Id.
218     */

219    public NodeRef getSelectedSpaceId()
220    {
221       return this.selectedSpaceId;
222    }
223    
224    /**
225     * @param selectedSpaceId The selected Space Id to set.
226     */

227    public void setSelectedSpaceId(NodeRef selectedSpaceId)
228    {
229       this.selectedSpaceId = selectedSpaceId;
230    }
231    
232    /**
233     * @return Returns the copy location. Either the current or other space.
234     */

235    public String JavaDoc getCopyLocation()
236    {
237       if (this.fileName != null)
238       {
239          return CheckinCheckoutBean.COPYLOCATION_OTHER;
240       }
241       else
242       {
243          return this.copyLocation;
244       }
245    }
246    
247    /**
248     * @param copyLocation The copy location. Either the current or other space.
249     */

250    public void setCopyLocation(String JavaDoc copyLocation)
251    {
252       this.copyLocation = copyLocation;
253    }
254    
255    /**
256     * @return Returns the message to display when a file has been uploaded
257     */

258    public String JavaDoc getFileUploadSuccessMsg()
259    {
260       String JavaDoc msg = Application.getMessage(FacesContext.getCurrentInstance(), "file_upload_success");
261       return MessageFormat.format(msg, new Object JavaDoc[] {getFileName()});
262    }
263    
264    /**
265     * @return Returns the name of the file
266     */

267    public String JavaDoc getFileName()
268    {
269       // try and retrieve the file and filename from the file upload bean
270
// representing the file we previously uploaded.
271
FacesContext ctx = FacesContext.getCurrentInstance();
272       FileUploadBean fileBean = (FileUploadBean)ctx.getExternalContext().getSessionMap().
273          get(FileUploadBean.FILE_UPLOAD_BEAN_NAME);
274       if (fileBean != null)
275       {
276          this.file = fileBean.getFile();
277          this.fileName = fileBean.getFileName();
278       }
279       
280       return this.fileName;
281    }
282
283    /**
284     * @param fileName The name of the file
285     */

286    public void setFileName(String JavaDoc fileName)
287    {
288       this.fileName = fileName;
289       
290       // we also need to keep the file upload bean in sync
291
FacesContext ctx = FacesContext.getCurrentInstance();
292       FileUploadBean fileBean = (FileUploadBean)ctx.getExternalContext().getSessionMap().
293          get(FileUploadBean.FILE_UPLOAD_BEAN_NAME);
294       if (fileBean != null)
295       {
296          fileBean.setFileName(this.fileName);
297       }
298    }
299    
300    /**
301     * @return Returns the document content used for HTML in-line editing.
302     */

303    public String JavaDoc getDocumentContent()
304    {
305       return this.documentContent;
306    }
307    
308    /**
309     * @param documentContent The document content for HTML in-line editing.
310     */

311    public void setDocumentContent(String JavaDoc documentContent)
312    {
313       this.documentContent = documentContent;
314    }
315    
316    /**
317     * @return Returns output from the in-line editor page.
318     */

319    public String JavaDoc getEditorOutput()
320    {
321       return this.editorOutput;
322    }
323
324    /**
325     * @param editorOutput The output from the in-line editor page
326     */

327    public void setEditorOutput(String JavaDoc editorOutput)
328    {
329       this.editorOutput = editorOutput;
330    }
331    
332    
333    // ------------------------------------------------------------------------------
334
// Navigation action event handlers
335

336    /**
337     * Action event called by all actions that need to setup a Content Document context on the
338     * CheckinCheckoutBean before an action page/wizard is called. The context will be a Node in
339     * setDocument() which can be retrieved on action pages via getDocument().
340     *
341     * @param event ActionEvent
342     */

343    public void setupContentAction(ActionEvent event)
344    {
345       UIActionLink link = (UIActionLink)event.getComponent();
346       Map JavaDoc<String JavaDoc, String JavaDoc> params = link.getParameterMap();
347       String JavaDoc id = params.get("id");
348       if (id != null && id.length() != 0)
349       {
350          setupContentDocument(id);
351       }
352       else
353       {
354          setDocument(null);
355       }
356       
357       clearUpload();
358    }
359    
360    /**
361     * Setup a content document node context
362     *
363     * @param id GUID of the node to setup as the content document context
364     *
365     * @return The Node
366     */

367    private Node setupContentDocument(String JavaDoc id)
368    {
369       if (logger.isDebugEnabled())
370          logger.debug("Setup for action, setting current document to: " + id);
371
372       Node node = null;
373       
374       try
375       {
376          // create the node ref, then our node representation
377
NodeRef ref = new NodeRef(Repository.getStoreRef(), id);
378          node = new Node(ref);
379          
380          // create content URL to the content download servlet with ID and expected filename
381
// the myfile part will be ignored by the servlet but gives the browser a hint
382
String JavaDoc url = DownloadContentServlet.generateDownloadURL(ref, node.getName());
383          node.getProperties().put("url", url);
384          node.getProperties().put("workingCopy", node.hasAspect(ContentModel.ASPECT_WORKING_COPY));
385          node.getProperties().put("fileType32", Utils.getFileTypeImage(node.getName(), false));
386          
387          // remember the document
388
setDocument(node);
389          
390          // refresh the UI, calling this method now is fine as it basically makes sure certain
391
// beans clear the state - so when we finish here other beans will have been reset
392
UIContextService.getInstance(FacesContext.getCurrentInstance()).notifyBeans();
393       }
394       catch (InvalidNodeRefException refErr)
395       {
396          Utils.addErrorMessage(MessageFormat.format(Application.getMessage(
397                FacesContext.getCurrentInstance(), Repository.ERROR_NODEREF), new Object JavaDoc[] {id}) );
398       }
399       
400       return node;
401    }
402    
403    /**
404     * Action called upon completion of the Check Out file page
405     */

406    public String JavaDoc checkoutFile()
407    {
408       String JavaDoc outcome = null;
409       
410       UserTransaction JavaDoc tx = null;
411       
412       Node node = getDocument();
413       if (node != null)
414       {
415          try
416          {
417             tx = Repository.getUserTransaction(FacesContext.getCurrentInstance());
418             tx.begin();
419             
420             if (logger.isDebugEnabled())
421                logger.debug("Trying to checkout content node Id: " + node.getId());
422             
423             // checkout the node content to create a working copy
424
if (logger.isDebugEnabled())
425             {
426                logger.debug("Checkout copy location: " + getCopyLocation());
427                logger.debug("Selected Space Id: " + this.selectedSpaceId);
428             }
429             NodeRef workingCopyRef;
430             if (getCopyLocation().equals(COPYLOCATION_OTHER) && this.selectedSpaceId != null)
431             {
432                // checkout to a arbituary parent Space
433
NodeRef destRef = this.selectedSpaceId;
434                
435                ChildAssociationRef childAssocRef = this.nodeService.getPrimaryParent(destRef);
436                workingCopyRef = this.versionOperationsService.checkout(node.getNodeRef(),
437                      destRef, ContentModel.ASSOC_CONTAINS, childAssocRef.getQName());
438             }
439             else
440             {
441                workingCopyRef = this.versionOperationsService.checkout(node.getNodeRef());
442             }
443             
444             // set the working copy Node instance
445
Node workingCopy = new Node(workingCopyRef);
446             setWorkingDocument(workingCopy);
447             
448             // create content URL to the content download servlet with ID and expected filename
449
// the myfile part will be ignored by the servlet but gives the browser a hint
450
String JavaDoc url = DownloadContentServlet.generateDownloadURL(workingCopyRef, workingCopy.getName());
451             
452             workingCopy.getProperties().put("url", url);
453             workingCopy.getProperties().put("fileType32", Utils.getFileTypeImage(workingCopy.getName(), false));
454             
455             // commit the transaction
456
tx.commit();
457             
458             // show the page that display the checkout link
459
outcome = "checkoutFileLink";
460          }
461          catch (Throwable JavaDoc err)
462          {
463             // rollback the transaction
464
try { if (tx != null) {tx.rollback();} } catch (Exception JavaDoc tex) {}
465             Utils.addErrorMessage(Application.getMessage(
466                   FacesContext.getCurrentInstance(), MSG_ERROR_CHECKOUT) + err.getMessage(), err);
467          }
468       }
469       else
470       {
471          logger.warn("WARNING: checkoutFile called without a current Document!");
472       }
473       
474       return outcome;
475    }
476    
477    /**
478     * Action called upon completion of the Check Out file Link download page
479     */

480    public String JavaDoc checkoutFileOK()
481    {
482       String JavaDoc outcome = null;
483       
484       Node node = getWorkingDocument();
485       if (node != null)
486       {
487          // clean up and clear action context
488
clearUpload();
489          setDocument(null);
490          setWorkingDocument(null);
491          
492          outcome = "browse";
493       }
494       else
495       {
496          logger.warn("WARNING: checkoutFileOK called without a current WorkingDocument!");
497       }
498       
499       return outcome;
500    }
501    
502    /**
503     * Action called upon completion of the Edit File download page
504     */

505    public String JavaDoc editFileOK()
506    {
507       String JavaDoc outcome = null;
508       
509       Node node = getDocument();
510       if (node != null)
511       {
512          // clean up and clear action context
513
clearUpload();
514          setDocument(null);
515          setWorkingDocument(null);
516          
517          outcome = "browse";
518       }
519       else
520       {
521          logger.warn("WARNING: editFileOK called without a current Document!");
522       }
523       
524       return outcome;
525    }
526    
527    /**
528     * Action handler called to calculate which editing screen to display based on the mimetype
529     * of a document. If appropriate, the in-line editing screen will be shown.
530     */

531    public void editFile(ActionEvent event)
532    {
533       UIActionLink link = (UIActionLink)event.getComponent();
534       Map JavaDoc<String JavaDoc, String JavaDoc> params = link.getParameterMap();
535       String JavaDoc id = params.get("id");
536       if (id != null && id.length() != 0)
537       {
538          Node node = setupContentDocument(id);
539          
540          // detect the inline editing aspect to see which edit mode to use
541
if (node.hasAspect(ContentModel.ASPECT_INLINEEDITABLE) &&
542              node.getProperties().get(ContentModel.PROP_EDITINLINE) != null &&
543              ((Boolean JavaDoc)node.getProperties().get(ContentModel.PROP_EDITINLINE)).booleanValue() == true)
544          {
545             // retrieve the content reader for this node
546
ContentReader reader = getContentService().getReader(node.getNodeRef(), ContentModel.PROP_CONTENT);
547             String JavaDoc mimetype = reader.getMimetype();
548             
549             // calculate which editor screen to display
550
if (MimetypeMap.MIMETYPE_TEXT_PLAIN.equals(mimetype) ||
551                 MimetypeMap.MIMETYPE_XML.equals(mimetype) ||
552                 MimetypeMap.MIMETYPE_TEXT_CSS.equals(mimetype))
553             {
554                // make content available to the editing screen
555
if (reader != null)
556                {
557                   setEditorOutput(reader.getContentString());
558                }
559                else
560                {
561                   setEditorOutput("");
562                }
563                
564                // navigate to appropriate screen
565
FacesContext fc = FacesContext.getCurrentInstance();
566                fc.getApplication().getNavigationHandler().handleNavigation(fc, null, "editTextInline");
567             }
568             else
569             {
570                // make content available to the editing screen
571
if (reader != null)
572                {
573                   setDocumentContent(reader.getContentString());
574                }
575                else
576                {
577                   setDocumentContent("");
578                }
579                setEditorOutput(null);
580                
581                // navigate to appropriate screen
582
FacesContext fc = FacesContext.getCurrentInstance();
583                fc.getApplication().getNavigationHandler().handleNavigation(fc, null, "editHtmlInline");
584             }
585          }
586          else
587          {
588             // normal downloadable document
589
FacesContext fc = FacesContext.getCurrentInstance();
590             fc.getApplication().getNavigationHandler().handleNavigation(fc, null, "editFile");
591          }
592       }
593    }
594    
595    /**
596     * Action handler called to set the content of a node from an inline editing page.
597     */

598    public String JavaDoc editInlineOK()
599    {
600       String JavaDoc outcome = null;
601       
602       UserTransaction JavaDoc tx = null;
603       
604       Node node = getDocument();
605       if (node != null)
606       {
607          try
608          {
609             tx = Repository.getUserTransaction(FacesContext.getCurrentInstance());
610             tx.begin();
611             
612             if (logger.isDebugEnabled())
613                logger.debug("Trying to update content node Id: " + node.getId());
614             
615             // get an updating writer that we can use to modify the content on the current node
616
ContentWriter writer = this.contentService.getWriter(node.getNodeRef(), ContentModel.PROP_CONTENT, true);
617             writer.putContent(this.editorOutput);
618             
619             // commit the transaction
620
tx.commit();
621             
622             // clean up and clear action context
623
clearUpload();
624             setDocument(null);
625             setDocumentContent(null);
626             setEditorOutput(null);
627             
628             outcome = "browse";
629          }
630          catch (Throwable JavaDoc err)
631          {
632             // rollback the transaction
633
try { if (tx != null) {tx.rollback();} } catch (Exception JavaDoc tex) {}
634             Utils.addErrorMessage(Application.getMessage(
635                   FacesContext.getCurrentInstance(), MSG_ERROR_UPDATE) + err.getMessage());
636          }
637       }
638       else
639       {
640          logger.warn("WARNING: editInlineOK called without a current Document!");
641       }
642       
643       return outcome;
644    }
645    
646    /**
647     * Action to undo the checkout of a document just checked out from the checkout screen.
648     */

649    public String JavaDoc undoCheckout()
650    {
651       String JavaDoc outcome = null;
652       
653       Node node = getWorkingDocument();
654       if (node != null)
655       {
656          try
657          {
658             // try to cancel checkout of the working copy
659
this.versionOperationsService.cancelCheckout(node.getNodeRef());
660             
661             clearUpload();
662             
663             outcome = "browse";
664          }
665          catch (Throwable JavaDoc err)
666          {
667             Utils.addErrorMessage(Application.getMessage(
668                   FacesContext.getCurrentInstance(), MSG_ERROR_CANCELCHECKOUT) + err.getMessage(), err);
669          }
670       }
671       else
672       {
673          logger.warn("WARNING: undoCheckout called without a current WorkingDocument!");
674       }
675       
676       return outcome;
677    }
678    
679    /**
680     * Action to undo the checkout of a locked document. This document may either by the original copy
681     * or the working copy node. Therefore calculate which it is, if the working copy is found then
682     * we simply cancel checkout on that document. If the original copy is found then we need to find
683     * the appropriate working copy and perform the action on that node.
684     */

685    public String JavaDoc undoCheckoutFile()
686    {
687       String JavaDoc outcome = null;
688       
689       Node node = getDocument();
690       if (node != null)
691       {
692          try
693          {
694             if (node.hasAspect(ContentModel.ASPECT_WORKING_COPY))
695             {
696                this.versionOperationsService.cancelCheckout(node.getNodeRef());
697             }
698             else if (node.hasAspect(ContentModel.ASPECT_LOCKABLE))
699             {
700                // TODO: find the working copy for this document and cancel the checkout on it
701
// is this possible? as currently only the workingcopy aspect has the copyReference
702
// attribute - this means we cannot find out where the copy is to cancel it!
703
// can we construct an XPath node lookup?
704
throw new RuntimeException JavaDoc("NOT IMPLEMENTED");
705             }
706             else
707             {
708                throw new IllegalStateException JavaDoc("Node supplied for undo checkout has neither Working Copy or Locked aspect!");
709             }
710             
711             clearUpload();
712             
713             outcome = "browse";
714          }
715          catch (Throwable JavaDoc err)
716          {
717             Utils.addErrorMessage(MSG_ERROR_CANCELCHECKOUT + err.getMessage(), err);
718          }
719       }
720       else
721       {
722          logger.warn("WARNING: undoCheckout called without a current WorkingDocument!");
723       }
724       
725       return outcome;
726    }
727    
728    /**
729     * Action called upon completion of the Check In file page
730     */

731    public String JavaDoc checkinFileOK()
732    {
733       String JavaDoc outcome = null;
734       
735       UserTransaction JavaDoc tx = null;
736       
737       // NOTE: for checkin the document node _is_ the working document!
738
Node node = getDocument();
739       if (node != null && (getCopyLocation().equals(COPYLOCATION_CURRENT) || this.getFileName() != null))
740       {
741          try
742          {
743             tx = Repository.getUserTransaction(FacesContext.getCurrentInstance());
744             tx.begin();
745             
746             if (logger.isDebugEnabled())
747                logger.debug("Trying to checkin content node Id: " + node.getId());
748             
749             // we can either checkin the content from the current working copy node
750
// which would have been previously updated by the user
751
String JavaDoc contentUrl;
752             String JavaDoc mimetype;
753             if (getCopyLocation().equals(COPYLOCATION_CURRENT))
754             {
755                ContentData contentData = (ContentData) node.getProperties().get(ContentModel.PROP_CONTENT);
756                contentUrl = (contentData == null ? null : contentData.getContentUrl());
757             }
758             // or specify a specific file as the content instead
759
else
760             {
761                // add the content to an anonymous but permanent writer location
762
// we can then retrieve the URL to the content to to be set on the node during checkin
763
ContentWriter writer = this.contentService.getWriter(node.getNodeRef(), ContentModel.PROP_CONTENT, false);
764                // TODO: Adjust the mimetype
765
writer.putContent(this.file);
766                contentUrl = writer.getContentUrl();
767             }
768             
769             if (contentUrl == null || contentUrl.length() == 0)
770             {
771                throw new IllegalStateException JavaDoc("Content URL is empty for specified working copy content node!");
772             }
773             
774             // add version history text to props
775
Map JavaDoc<String JavaDoc, Serializable JavaDoc> props = new HashMap JavaDoc<String JavaDoc, Serializable JavaDoc>(1, 1.0f);
776             props.put(Version.PROP_DESCRIPTION, this.versionNotes);
777             // set the flag for minor or major change
778
if (this.minorChange)
779             {
780                props.put(VersionModel.PROP_VERSION_TYPE, VersionType.MINOR);
781             }
782             else
783             {
784                props.put(VersionModel.PROP_VERSION_TYPE, VersionType.MAJOR);
785             }
786             
787             NodeRef originalDoc = this.versionOperationsService.checkin(
788                   node.getNodeRef(),
789                   props,
790                   contentUrl,
791                   this.keepCheckedOut);
792             
793             // commit the transaction
794
tx.commit();
795             
796             // clear action context
797
setDocument(null);
798             clearUpload();
799             
800             outcome = "browse";
801          }
802          catch (Throwable JavaDoc err)
803          {
804             // rollback the transaction
805
try { if (tx != null) {tx.rollback();} } catch (Exception JavaDoc tex) {}
806             Utils.addErrorMessage(Application.getMessage(
807                   FacesContext.getCurrentInstance(), MSG_ERROR_CHECKIN) + err.getMessage(), err);
808          }
809       }
810       else
811       {
812          logger.warn("WARNING: checkinFileOK called without a current Document!");
813       }
814       
815       return outcome;
816    }
817    
818    /**
819     * Action called upon completion of the Update File page
820     */

821    public String JavaDoc updateFileOK()
822    {
823       String JavaDoc outcome = null;
824       
825       UserTransaction JavaDoc tx = null;
826       
827       // NOTE: for update the document node _is_ the working document!
828
Node node = getDocument();
829       if (node != null && this.getFileName() != null)
830       {
831          try
832          {
833             tx = Repository.getUserTransaction(FacesContext.getCurrentInstance());
834             tx.begin();
835             
836             if (logger.isDebugEnabled())
837                logger.debug("Trying to update content node Id: " + node.getId());
838             
839             // get an updating writer that we can use to modify the content on the current node
840
ContentWriter writer = this.contentService.getWriter(node.getNodeRef(), ContentModel.PROP_CONTENT, true);
841             writer.putContent(this.file);
842             
843             // commit the transaction
844
tx.commit();
845             
846             // clear action context
847
setDocument(null);
848             clearUpload();
849             
850             outcome = "browse";
851          }
852          catch (Throwable JavaDoc err)
853          {
854             // rollback the transaction
855
try { if (tx != null) {tx.rollback();} } catch (Exception JavaDoc tex) {}
856             Utils.addErrorMessage(Application.getMessage(
857                   FacesContext.getCurrentInstance(), MSG_ERROR_UPDATE) + err.getMessage(), err);
858          }
859       }
860       else
861       {
862          logger.warn("WARNING: updateFileOK called without a current Document!");
863       }
864       
865       return outcome;
866    }
867    
868    /**
869     * Deals with the cancel button being pressed on the check in file page
870     */

871    public String JavaDoc cancel()
872    {
873       // reset the state
874
clearUpload();
875       
876       return "browse";
877    }
878    
879    /**
880     * Clear form state and upload file bean
881     */

882    private void clearUpload()
883    {
884       // delete the temporary file we uploaded earlier
885
if (this.file != null)
886       {
887          this.file.delete();
888       }
889       
890       this.file = null;
891       this.fileName = null;
892       this.keepCheckedOut = false;
893       this.minorChange = true;
894       this.copyLocation = COPYLOCATION_CURRENT;
895       this.versionNotes = "";
896       this.selectedSpaceId = null;
897       
898       // remove the file upload bean from the session
899
FacesContext ctx = FacesContext.getCurrentInstance();
900       ctx.getExternalContext().getSessionMap().remove(FileUploadBean.FILE_UPLOAD_BEAN_NAME);
901    }
902    
903    
904    // ------------------------------------------------------------------------------
905
// Private data
906

907    private static Log logger = LogFactory.getLog(CheckinCheckoutBean.class);
908    
909    /** I18N messages */
910    private static final String JavaDoc MSG_ERROR_CHECKIN = "error_checkin";
911    private static final String JavaDoc MSG_ERROR_CANCELCHECKOUT = "error_cancel_checkout";
912    private static final String JavaDoc MSG_ERROR_UPDATE = "error_update";
913    private static final String JavaDoc MSG_ERROR_CHECKOUT = "error_checkout";
914
915    /** constants for copy location selection */
916    private static final String JavaDoc COPYLOCATION_CURRENT = "current";
917    private static final String JavaDoc COPYLOCATION_OTHER = "other";
918    
919    /** The current document */
920    private Node document;
921    
922    /** The working copy of the document we are checking out */
923    private Node workingDocument;
924    
925    /** Content of the document used for HTML in-line editing */
926    private String JavaDoc documentContent;
927    
928    /** Content of the document returned from in-line editing */
929    private String JavaDoc editorOutput;
930    
931    /** transient form and upload properties */
932    private File JavaDoc file;
933    private String JavaDoc fileName;
934    private boolean keepCheckedOut = false;
935    private boolean minorChange = true;
936    private String JavaDoc copyLocation = COPYLOCATION_CURRENT;
937    private String JavaDoc versionNotes = "";
938    private NodeRef selectedSpaceId = null;
939    
940    /** The BrowseBean to be used by the bean */
941    protected BrowseBean browseBean;
942    
943    /** The NodeService to be used by the bean */
944    protected NodeService nodeService;
945    
946    /** The VersionOperationsService to be used by the bean */
947    protected CheckOutCheckInService versionOperationsService;
948    
949    /** The ContentService to be used by the bean */
950    protected ContentService contentService;
951 }
952
Popular Tags