001    /*
002     * file ViewHelper.java
003     *
004     * Licensed Materials - Property of IBM
005     * Restricted Materials of IBM - you are allowed to copy, modify and 
006     * redistribute this file as part of any program that interfaces with 
007     * IBM Rational CM API.
008     *
009     * com.ibm.rational.stp.client.samples.CheckoutDemo
010     *
011     * © Copyright IBM Corporation 2008.  All Rights Reserved.
012     * Note to U.S. Government Users Restricted Rights:  Use, duplication or 
013     * disclosure restricted by GSA ADP  Schedule Contract with IBM Corp.
014     */
015    package com.ibm.rational.stp.client.samples.cc;
016    
017    import java.io.File;
018    import java.util.Date;
019    import java.util.Random;
020    
021    import com.ibm.rational.wvcm.stp.StpLocation;
022    import com.ibm.rational.wvcm.stp.StpProvider.Domain;
023    import com.ibm.rational.wvcm.stp.cc.CcDirectory;
024    import com.ibm.rational.wvcm.stp.cc.CcFile;
025    import com.ibm.rational.wvcm.stp.cc.CcProvider;
026    import com.ibm.rational.wvcm.stp.cc.CcView;
027    
028    public class ViewHelper {
029    
030        private CcView m_view;
031        private final CcProvider m_provider;
032        private CcDirectory m_demoDir;
033        private CcDirectory m_vobRootDir;
034        private final CcDemoProps m_props;
035    
036        public static ViewHelper getNonUcmViewHelper(CcDemoBase env) {
037            return new ViewHelper(env.getClearCaseProvider(), env.props());
038        }
039    
040        public ViewHelper(CcProvider provider, CcDemoProps props) {
041            m_provider = provider;
042            m_props = props;
043        }
044    
045        void tearDown() throws Exception {
046            // Delete demo web view
047            if (m_view != null) {
048                m_view.doUnbindAll(null);
049                m_view = null;
050            }
051        }
052    
053        /**
054         * Create a temporary ClearCase web view.
055         * This view will be deleted once the demo completes.
056         * @throws Exception 
057         */
058        protected CcView getView() throws Exception {
059            if (m_view == null) {
060                // Create a location for the web view rooted in the user's temp dir.
061                // This does not actually create the view itself.
062                File tempDir = m_props.getTempDir();
063                String viewTag = generateUniqueName(m_props.getLoginName() + ".demo.");
064                File viewRoot = new File(tempDir, viewTag);
065                StpLocation viewLoc = m_provider.filePathLocation(Domain.CLEAR_CASE, viewRoot);
066    
067                // Create the view itself
068                m_view = m_provider.ccView(viewLoc).doCreateCcWebView(null);
069            }
070            return m_view;
071        }
072    
073        /**
074         * Generate a (hopefully) unique name by adding a random integer to the
075         * specified base name.
076         */
077        static String generateUniqueName(String baseName) {
078            Random random = new Random(new Date().getTime());
079            return baseName + random.nextInt(100000);
080        }
081    
082        CcFile createDemoFile(String fileName, boolean controlled) throws Exception {
083            CcDirectory parentDir = createDemoDirectory();
084            StpLocation demoFileLoc = (StpLocation) parentDir.stpLocation().child(fileName);
085            CcFile demoFile = m_provider.ccFile(demoFileLoc);
086    
087            // Create the file in the web view's local file area.
088            demoFile = demoFile.createCcFile(null);
089    
090            if (controlled) {
091                // Version control the demo file.
092                demoFile = (CcFile) demoFile.doVersionControl(null);
093            }
094            return demoFile;
095        }
096    
097        /**
098         * Create a version-controlled directory in the demo VOB.
099         */
100        private CcDirectory createDemoDirectory() throws Exception {
101            if (m_demoDir == null) {
102                // Get the demo VOB's root directory.
103                // That's where we're going to create the demo directory.
104                CcDirectory vobRootDir = getDemoVobRootDirectory();
105    
106                // Create a location object for the demo directory.
107                // The demo directory will be a child of the demo VOB's root directory.
108                String demoDirName = generateUniqueName("demo");
109                StpLocation vobRootDirLoc = vobRootDir.stpLocation();
110                StpLocation demoDirLoc = (StpLocation) vobRootDirLoc.child(demoDirName);
111    
112                // Create a proxy object for the demo directory.
113                CcDirectory demoDir = m_provider.ccDirectory(demoDirLoc);
114    
115                // Create the directory in the web view's local file area.
116                demoDir = demoDir.createCcDirectory(null);
117    
118                // Version control the demo directory.
119                m_demoDir = (CcDirectory) demoDir.doVersionControl(null);
120            }
121            return m_demoDir;
122        }
123    
124        /**
125         * Get the root directory of the demo VOB in the context of the
126         * demo web view.
127         */
128        private CcDirectory getDemoVobRootDirectory() throws Exception {
129            if (m_vobRootDir == null) {
130                // Get demo VOB's tag.  Strip leading slash.
131                String demoVobTag = m_props.getDemoUcmProjectVob().substring(1);
132    
133                // The demo VOB's root directory is a child of the web view.
134                CcView view = getView();
135    
136                // Create a location object for the VOB root directory.
137                StpLocation viewLoc = view.stpLocation();
138                StpLocation vobRootDirLoc = (StpLocation) viewLoc.child(demoVobTag);
139    
140                // Create a proxy object for the VOB root directory.
141                m_vobRootDir = m_provider.ccDirectory(vobRootDirLoc);
142    
143                // Load the demo VOB into the demo web view.
144                m_vobRootDir.doLoad(null);
145            }
146            return m_vobRootDir;
147        }
148    }