001    /*
002     * file UcmHelper.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.UcmHelper
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    
016    package com.ibm.rational.stp.client.samples.cc;
017    
018    import javax.wvcm.WvcmException;
019    import javax.wvcm.PropertyRequestItem.PropertyRequest;
020    import javax.wvcm.WvcmException.ReasonCode;
021    
022    import com.ibm.rational.wvcm.stp.StpLocation;
023    import com.ibm.rational.wvcm.stp.StpLocation.Namespace;
024    import com.ibm.rational.wvcm.stp.StpProvider.Domain;
025    import com.ibm.rational.wvcm.stp.cc.CcProject;
026    import com.ibm.rational.wvcm.stp.cc.CcProjectFolder;
027    import com.ibm.rational.wvcm.stp.cc.CcProvider;
028    import com.ibm.rational.wvcm.stp.cc.CcResource;
029    import com.ibm.rational.wvcm.stp.cc.CcStream;
030    import com.ibm.rational.wvcm.stp.cc.CcVob;
031    
032    public class UcmHelper {
033    
034        private final CcProvider m_provider;
035        private CcStream m_devStream;
036        private CcStream m_intStream;
037        private CcProject m_project;
038        private CcProjectFolder m_rootFolder;
039        private final String m_pvobTag;
040        private final String m_loginName;
041        private CcDemoBase m_demo;
042        private CcVob m_pvob;
043    
044        public UcmHelper(CcDemoBase demo) throws Exception {
045            m_demo = demo;
046            m_provider = demo.getClearCaseProvider();
047            m_loginName = demo.props().getLoginName();
048            m_pvobTag = demo.props().getDemoUcmProjectVob();
049    
050            // Verify project VOB exists.
051            getUcmProjectVob();
052        }
053    
054        public CcStream getUcmDevelopmentStream() throws Exception {
055            if (m_devStream == null) {
056    
057                // Construct location object for development stream.
058                StpLocation streamLoc = m_provider.userFriendlySelector(
059                        Domain.CLEAR_CASE,
060                        Namespace.STREAM,
061                        m_loginName + ".dev.stream",
062                        m_pvobTag);
063    
064                // Use provider's factory method to construct CC stream proxy.
065                // Note: This call does not contact the server. We don't know
066                // at this point whether the stream exists or not.
067                m_devStream = m_provider.ccStream(streamLoc);
068    
069                if ( ! resourceExists(m_devStream)) {
070                    // The development stream doesn't exist yet.  Create it.
071                    CcStream intStream = getUcmIntegrationStream();
072                    m_devStream.setParentStream(intStream);
073                    trace("Creating UCM demo development stream: " + m_devStream);
074                    m_devStream = m_devStream.doCreateCcStream(null);
075                }
076            }
077            return m_devStream;
078        }
079    
080        public CcStream getUcmIntegrationStream() throws Exception {
081            if (m_intStream == null) {
082    
083                // Construct location object for integration stream.
084                StpLocation streamLoc = m_provider.userFriendlySelector(
085                        Domain.CLEAR_CASE,
086                        Namespace.STREAM,
087                        m_loginName + ".int.stream",
088                        m_pvobTag);
089    
090                // Use provider's factory method to construct CC stream proxy.
091                // Note: This call does not contact the server. We don't know
092                // at this point whether the stream exists or not.
093                m_intStream = m_provider.ccStream(streamLoc);
094    
095                if ( ! resourceExists(m_intStream)) {
096                    // The integration stream doesn't exist yet.  Create it.
097                    CcProject project = getUcmProject();
098                    m_intStream.setParentProject(project);
099                    m_intStream.setIsIntegrationStream(true);
100                    trace("Creating UCM demo integration stream: " + m_intStream);
101                    m_intStream = m_intStream.doCreateCcStream(null);
102                }
103            }
104            return m_intStream;
105        }
106    
107        public CcProject getUcmProject() throws Exception {
108            if (m_project == null) {
109    
110                // Construct location object for UCM project.
111                StpLocation projectLoc = m_provider.userFriendlySelector(
112                        Domain.CLEAR_CASE,
113                        Namespace.PROJECT,
114                        m_loginName + ".project",
115                        m_pvobTag);
116    
117                // Use provider's factory method to construct project proxy.
118                // Note: This call does not contact the server. We don't know
119                // at this point whether the project exists or not.
120                m_project = m_provider.ccProject(projectLoc);
121    
122                if ( ! resourceExists(m_project)) {
123                    // The project doesn't exist yet.  Create it.
124                    CcProjectFolder folder = getUcmRootFolder();
125                    m_project.setProjectFolder(folder);
126                    trace("Creating UCM demo project: " + m_project);
127                    m_project = m_project.doCreateCcProject(null);
128                }
129            }
130            return m_project;
131        }
132    
133        public CcProjectFolder getUcmRootFolder() throws Exception {
134            if (m_rootFolder == null) {
135    
136                // Construct location object for the root project folder.
137                StpLocation folderLoc = m_provider.userFriendlySelector(
138                        Domain.CLEAR_CASE,
139                        Namespace.FOLDER,
140                        "RootFolder",
141                        m_pvobTag);
142    
143                m_rootFolder = m_provider.ccProjectFolder(folderLoc);
144    
145                if ( ! resourceExists(m_rootFolder)) {
146                    throw new IllegalStateException("Missing root project folder: " + m_rootFolder);
147                }
148            }
149            return m_rootFolder;
150        }
151    
152        public CcVob getUcmProjectVob() throws Exception {
153            if (m_pvob == null) {
154    
155                // Construct location object for the root project folder.
156                StpLocation pvobLoc = m_provider.userFriendlySelector(
157                        Domain.CLEAR_CASE,
158                        Namespace.VOB,
159                        null,
160                        m_pvobTag);
161    
162                m_pvob = m_provider.ccVob(pvobLoc);
163                PropertyRequest wantedProps = new PropertyRequest(CcVob.IS_PROJECT_VOB);
164                m_pvob = (CcVob) m_pvob.doReadProperties(wantedProps);
165                if ( ! m_pvob.getIsProjectVob()) {
166                    throw new IllegalStateException("Not a UCM project VOB: " + m_pvob);
167                }
168            }
169            return m_pvob;
170        }
171    
172        private boolean resourceExists(CcResource res) throws Exception {
173            try {
174                res = (CcResource) res.doReadProperties(null);
175                return true;
176            } catch (WvcmException ex) {
177                if (ex.getReasonCode() == ReasonCode.NOT_FOUND) {
178                    return false;
179                } else {
180                    throw ex;
181                }
182            }
183        }
184    
185        private void trace(String msg) {
186            m_demo.trace(msg);
187        }
188    }