001    /*
002     * file CreateRecord.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.CreateRecord
010     *
011     * © Copyright IBM Corporation 2007, 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;
017    
018    import static com.ibm.rational.stp.client.samples.ExecuteQuery.setUserFriendlyLocation;
019    
020    import java.util.Iterator;
021    
022    import javax.swing.JFrame;
023    import javax.swing.JOptionPane;
024    import javax.wvcm.ResourceList;
025    import javax.wvcm.PropertyRequestItem.PropertyRequest;
026    
027    import com.ibm.rational.wvcm.stp.StpLocation;
028    import com.ibm.rational.wvcm.stp.cq.CqProvider;
029    import com.ibm.rational.wvcm.stp.cq.CqRecord;
030    import com.ibm.rational.wvcm.stp.cq.CqRecordType;
031    import com.ibm.rational.wvcm.stp.cq.CqUserDb;
032    
033    /**
034     */
035    public class CreateRecord extends EditRecord
036    {
037        /**
038         * A sample application for creating ClearQuest records, reusing the
039         * editRecord method of the EditRecord example. Rather than executing a
040         * query to find a record to edit, this application allows the user to
041         * select the database where the record is to be created and the type of
042         * record to create. Then the record is created and the EditRecord dialog is
043         * invoked to allow the user to fill in the fields desired in the new
044         * record.
045         */
046        public static void main(String[] args)
047        {
048            try {
049                CqProvider provider = Utilities.getProvider().cqProvider();
050                Viewer viewer = new Viewer(provider);
051                ResourceList<CqUserDb> databases = Utilities.getUserDbList(provider, null);
052                CqUserDb userDb = (CqUserDb) JOptionPane
053                    .showInputDialog(null,
054                                     "Choose a Database for the New Record",
055                                     "Create Record",
056                                     JOptionPane.INFORMATION_MESSAGE,
057                                     null,
058                                     databases.toArray(new Object[] {}),
059                                     databases.get(0));
060        
061                if (userDb == null) System.exit(0);
062                
063                userDb = (CqUserDb) userDb
064                    .doReadProperties(new PropertyRequest(CqUserDb.RECORD_TYPE_SET
065                                                    .nest(RECORD_TYPE_PROPERTIES)));
066                
067                // Read the list of all record types from the selected database and 
068                // remove from that list those record types that are not submittable.
069                ResourceList<CqRecordType> rTypes = 
070                    setUserFriendlyLocation(userDb.getRecordTypeSet());
071                Iterator<CqRecordType> types = rTypes.iterator();
072        
073                while (types.hasNext()) {
074                    if (!types.next().getIsSubmittable())
075                        types.remove();
076                }
077        
078                // Present the list of submittable record types to the user for
079                // selection
080                CqRecordType recordType = (CqRecordType) JOptionPane
081                    .showInputDialog(null,
082                                     "Choose the type of record to create",
083                                     "All Record Types in "
084                                         + userDb.location().string(),
085                                     JOptionPane.INFORMATION_MESSAGE,
086                                     null,
087                                     rTypes.toArray(new CqRecordType[] {}),
088                                     rTypes.get(0));
089        
090                if (recordType == null) System.exit(0);
091        
092                // The EditRecord dialog expects the editable record to be in
093                // its own change context, so create one for this purpose. It 
094                // will be destroyed by the EditRecord dialog
095                CqProvider context = recordType.cqProvider();
096                
097                // The actual name for the new record is determined by the
098                // schema. All that is needed here is a "suggested" location
099                // that makes the record a member of the specified record type.
100                CqRecord record = context.cqRecord((StpLocation) recordType
101                    .getUserFriendlyLocation().child("new"));
102                
103                // Create the record. Don't try to deliver it yet since mandatory
104                // fields may need to be set by the user before delivery will
105                // succeed.
106                record = record.doCreateRecord(RECORD_PROPERTIES, CqProvider.HOLD);
107                
108                /*
109                 * After delivering the created record to its database, the
110                 * EditRecord dialog will want to redisplay it in its own viewer.
111                 * We need to create this "original" proxy after the fact
112                 * because we don't have a valid location for the new record until
113                 * after it has been created. Need to use the stable location
114                 * because, in some cases, the user-friendly location can change
115                 * when field values are changed.
116                 */
117                CqRecord selected = recordType.cqProvider()
118                                        .cqRecord(record.getStableLocation());
119        
120                // With the new record created in the change context, the process
121                // proceeds in the same fashion as editing a record. Mandatory 
122                // fields must be supplied by the user and then it can be delivered.
123                viewer.editRecord("Create Record ", record, selected)
124                    .setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
125            } catch (Throwable ex) {
126                ex.printStackTrace();
127                Utilities.exception(null, "Create Record", ex);
128                System.exit(0);
129            }
130        }
131        
132        /** The record type properties read prior to creating a record */
133        final static PropertyRequest RECORD_TYPE_PROPERTIES =
134            new PropertyRequest(CqRecordType.USER_FRIENDLY_LOCATION,
135                                CqRecordType.IS_SUBMITTABLE,
136                                CqRecordType.DISPLAY_NAME);
137    }