Initial add of defaria.com
[clearscm.git] / defaria.com / blogs / Status / archives / 000588.html
1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
2    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3 <html xmlns="http://www.w3.org/1999/xhtml" id="sixapart-standard">
4 <head>
5    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
6    <meta name="generator" content="Movable Type 5.2.3" />
7
8    <link rel="stylesheet" href="http://defaria.com/blogs/Status/styles-site.css" type="text/css" />
9    <link rel="alternate" type="application/atom+xml" title="Atom" href="http://defaria.com/blogs/Status/atom.xml" />
10    <link rel="alternate" type="application/rss+xml" title="RSS 2.0" href="http://defaria.com/blogs/Status/index.xml" />
11
12    <title>Status for Andrew DeFaria: Cloning done</title>
13
14    <link rel="start" href="http://defaria.com/blogs/Status/" title="Home" />
15    <link rel="prev" href="http://defaria.com/blogs/Status/archives/000587.html" title="cclic_report/gpdb_putDesignsync bug" />
16    <link rel="next" href="http://defaria.com/blogs/Status/archives/000589.html" title="Convertdb" />
17
18    
19
20    
21
22    <script type="text/javascript" src="http://defaria.com/blogs/Status/mt-site.js"></script>
23 </head>
24 <body class="layout-one-column" onload="individualArchivesOnLoad(commenter_name)">
25    <div id="container">
26       <div id="container-inner" class="pkg">
27
28          <div id="banner">
29             <div id="banner-inner" class="pkg">
30                <h1 id="banner-header"><a href="http://defaria.com/blogs/Status/" accesskey="1">Status for Andrew DeFaria</a></h1>
31                <h2 id="banner-description">Searchable status reports and work log</h2>
32             </div>
33          </div>
34
35          <div id="pagebody">
36             <div id="pagebody-inner" class="pkg">
37                <div id="alpha">
38                   <div id="alpha-inner" class="pkg">
39
40                      <p class="content-nav">
41                         <a href="http://defaria.com/blogs/Status/archives/000587.html">&laquo; cclic_report/gpdb_putDesignsync bug</a> |
42                         <a href="http://defaria.com/blogs/Status/">Main</a>
43                         | <a href="http://defaria.com/blogs/Status/archives/000589.html">Convertdb &raquo;</a>
44                      </p>
45
46                      <a id="a000588"></a>
47                      <div class="entry" id="entry-588">
48                         <h3 class="entry-header">Cloning done</h3>
49                         <div class="entry-content">
50                            <div class="entry-body">
51                               <ul>
52   <li>Implemented cloning procedure for DMD/CQ</li>
53 </ul>
54                            </div>
55                            <div id="more" class="entry-more">
56                               <h3>How to clone a parent CQ record to a child</h3>
57
58 <p>You would think that this would be clearly documented in the CQ manual but it isn't. The requirement is clear enough - "implement parent/child relationships for a CQ record... Oh and when a child is created could you copy everything from the parent and we'll change what's different in the child".</p>
59
60 <p>Implementing a parent/child relationship is pretty clear and documented - basically you create a reference link in the current record back to itself. CQ even has a parent/child control that handles manipulating the relationship allowing the user the controls to link in existing records, delete a parent/child relationship or add a new record as a child of this record. But there is nothing in there about copying data from the parent to the child. This you must do with hooks. But how to code the hooks?</p>
61
62 <p>I found a method for doing this and implemented the following. The trick is to add pre and post hooks to the <b>New</b> button of the parent/child control. This button is selected when the user wishes to add a new child record to this parent. The pre action hook, created as a <i>Record Script</i> set a session wide variable saying "Hey I'm adding a new child record to this parent". This variable contains the ID of the parent. The following code accomplishes this:</p>
63
64 <div class=code><pre>
65 my $session = $entity->GetSession;
66
67 $session->NameValue ("ParentID", $entity->GetFieldValue ("id")->GetValue);
68 </pre></div>
69
70 <p>After creating this record script add it as the pre-action hook for the new button. Don't forget to toggle the Enable for CQ Web (I don't really understand why you would ever not toggle that).</p>
71
72 <p>For the post-action script you are basically saying "Hey I'm no longer adding a new child record to this parent" with the following code:
73
74 <div class=code><pre>
75 my $session = $entity->GetSession;
76
77 $session->NameValue ("ParentID", "");
78 </pre></div>
79
80 <p>What this does is effectively bound the time you are in this unique situation - any other time the global session variable ParentID will be blank. Now the cloning can begin...</p>
81
82 <p>In the default value hooks for each field you want cloned place the following call:</p>
83
84 <div class=code><pre>
85 CloneField ($fieldname);
86 </pre></div>
87
88 <p>This is written as a call to a Global Script since the code will always be the same and because you'll have to do this for each field that you wish cloned.</p>
89
90 <p>Finally, create the following Global Script:</p>
91
92 <div class=code><pre>
93 sub CloneField {
94   my $fieldname = shift;
95     
96   # Check session wide global, ParentID, and if set retrieve
97   # the parent record and clone this field
98   my $session  = $entity->GetSession;
99   my $parentID = $session->GetNameValue ("ParentID");
100
101   if ($parentID ne "") {
102     # If ParentID is not blank then we are adding a subtask.
103     # Copy this field from the parent to this new child.
104
105     # Get the parent record
106     my $parent = $session->GetEntity ("ChangeRequest", $parentID);
107
108     # Set the child field
109     $entity->SetFieldValue (
110         $fieldname,
111         $parent->GetFieldValue ($fieldname)->GetValue
112     );
113   } # if
114 } # CloneField
115 </pre></div>
116
117 <p>This script checks to see if the session global "ParentID" is not blank, indicating that we are in this special mode of adding a new child to an existing parent, and if so, gets the parent record and the field value based on the passed in field name. Finally it sets the field value based on the field name to that of the value of the corresponding parent's field value.</p>
118                            </div>
119                         </div>
120                         <p class="entry-footer">
121                            <span class="post-footers">Posted by  on October 31, 2006 11:18 AM</span> <span class="separator">|</span> <a class="permalink" href="http://defaria.com/blogs/Status/archives/000588.html">Permalink</a>
122                         </p>
123                      </div>
124
125                      
126
127                      
128                   </div>
129                </div>
130             </div>
131          </div>
132       </div>
133    </div>
134 </body>
135 </html>