##### Coverage Report - org.melati.admin.Copy

Classes in this File | Line Coverage | Branch Coverage | Complexity
Copy |
41% | 13/31
50% | 2/4
| 2.111111111111111;2.111
Copy$1 |
84% | 27/32
100% | 8/8
| 2.111111111111111;2.111
Copy$2 |
100% | 2/2
N/A | N/A
| 2.111111111111111;2.111

1 |  |
     /*
2 |  |
      * $Source: /usr/cvsroot/melati/melati/src/site/resources/withWebmacro/org.melati.admin.Copy.html,v $
3 |  |
      * $Revision: 1.1 $
4 |  |
      *
5 |  |
      * Copyright (C) 2007 Tim Pizey
6 |  |
      *
7 |  |
      * Part of Melati (http://melati.org), a framework for the rapid
8 |  |
      * development of clean, maintainable web applications.
9 |  |
      *
10 |  |
      * Melati is free software; Permission is granted to copy, distribute
11 |  |
      * and/or modify this software under the terms either:
12 |  |
      *
13 |  |
      * a) the GNU General Public License as published by the Free Software
14 |  |
      *    Foundation; either version 2 of the License, or (at your option)
15 |  |
      *    any later version,
16 |  |
      *
17 |  |
      *    or
18 |  |
      *
19 |  |
      * b) any version of the Melati Software License, as published
20 |  |
      *    at http://melati.org
21 |  |
      *
22 |  |
      * You should have received a copy of the GNU General Public License and
23 |  |
      * the Melati Software License along with this program;
24 |  |
      * if not, write to the Free Software Foundation, Inc.,
25 |  |
      * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA to obtain the
26 |  |
      * GNU General Public License and visit http://melati.org to obtain the
27 |  |
      * Melati Software License.
28 |  |
      *
29 |  |
      * Feel free to contact the Developers of Melati (http://melati.org),
30 |  |
      * if you would like to work out a different arrangement than the options
31 |  |
      * outlined here.  It is our intention to allow Melati to be used by as
32 |  |
      * wide an audience as possible.
33 |  |
      *
34 |  |
      * This program is distributed in the hope that it will be useful,
35 |  |
      * but WITHOUT ANY WARRANTY; without even the implied warranty of
36 |  |
      * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
37 |  |
      * GNU General Public License for more details.
38 |  |
      *
39 |  |
      * Contact details for copyright holder:
40 |  |
      *
41 |  |
      *     Tim Pizey <timp At paneris.org>
42 |  |
      *     http://paneris.org/~timp
43 |  |
      */
44 |  |
     package org.melati.admin;
45 |  |
46 |  |
     import java.util.Enumeration;
47 |  |
48 |  |
     import org.melati.LogicalDatabase;
49 |  |
     import org.melati.Melati;
50 |  |
     import org.melati.poem.Database;
51 |  |
     import org.melati.poem.Field;
52 |  |
     import org.melati.poem.Persistent;
53 |  |
     import org.melati.poem.PoemTask;
54 |  |
     import org.melati.poem.PoemThread;
55 |  |
     import org.melati.poem.Table;
56 |  |
     import org.melati.poem.util.MappedEnumeration;
57 |  |
     import org.melati.servlet.PathInfoException;
58 |  |
     import org.melati.servlet.TemplateServlet;
59 |  |
     import org.melati.template.ServletTemplateContext;
60 |  |
61 |  |
     /**
62 |  |
      *
63 |  |
      * Copy one db to another.
64 |  |
      *
65 |  |
      * @author timp
66 |  |
      * @since 3 Oct 2007
67 |  |
      *
68 |  |
      */
69 | 0 |
     public class Copy extends TemplateServlet {
70 |  |
71 |  |
       /**
72 |  |
        *
73 |  |
        */
74 |  |
       private static final long serialVersionUID = 1L;
75 | 2 |
       static Database fromDb = null;
76 | 2 |
       static Database toDb = null;
77 |  |
78 |  |
79 |  |
       /**
80 |  |
        * {@inheritDoc}
81 |  |
        * @see org.melati.servlet.TemplateServlet#prePoemSession(org.melati.Melati)
82 |  |
        */
83 |  |
       protected void prePoemSession(Melati melati) throws Exception {
84 | 0 |
         super.prePoemSession(melati);
85 | 0 |
         String[] parts = melati.getPathInfoParts();
86 | 0 |
         if (parts.length != 2)
87 | 0 |
           throw new PathInfoException("Two database names expecetd");
88 | 0 |
         String fromDbName = parts[0];
89 | 0 |
         String toDbName = parts[1];
90 |  |
91 | 0 |
         copy(fromDbName, toDbName);
92 |  |
93 | 0 |
       }
94 |  |
95 |  |
       /**
96 |  |
        * {@inheritDoc}
97 |  |
        * @see org.melati.servlet.TemplateServlet#doTemplateRequest
98 |  |
        */
99 |  |
       protected String doTemplateRequest(Melati melati,
100 |  |
               ServletTemplateContext templateContext) throws Exception {
101 | 0 |
         melati.setPassbackExceptionHandling();
102 | 0 |
         melati.setResponseContentType("text/html");
103 | 0 |
         templateContext.put("admin", new AdminUtils(melati));
104 |  |
105 | 0 |
         String[] parts = melati.getPathInfoParts();
106 | 0 |
         String fromDbName = parts[0];
107 | 0 |
         String toDbName = parts[1];
108 |  |
109 | 0 |
         templateContext.put("fromDbName",fromDbName);
110 | 0 |
         templateContext.put("toDbName",toDbName);
111 | 0 |
         return "org/melati/admin/CopyDone";
112 |  |
       }
113 |  |
114 |  |
       /**
115 |  |
        * @param from
116 |  |
        * @param to
117 |  |
        * @return the updated db
118 |  |
        */
119 |  |
       public static Database copy(String from, String to) {
120 | 2 |
         fromDb = LogicalDatabase.getDatabase(from);
121 | 2 |
         toDb =  LogicalDatabase.getDatabase(to);
122 | 2 |
         return copy();
123 |  |
       }
124 |  |
       /**
125 |  |
        * @param fromDb
126 |  |
        * @param toDb
127 |  |
        * @return the updated db
128 |  |
        */
129 |  |
       public static Database copy(Database fromDbIn, final Database toDbIn) {
130 | 4 |
         fromDb = fromDbIn;
131 | 4 |
         toDb = toDbIn;
132 | 4 |
         return copy();
133 |  |
       }
134 |  |
       /**
135 |  |
        * @return the updated db
136 |  |
        */
137 |  |
       public static Database copy() {
138 | 6 |
         if (fromDb.getClass() != toDb.getClass())
139 | 2 |
           throw new AnticipatedException("Both from(" + fromDb.getClass() + ") and " +
140 |  |
                                          "to(" + toDb.getClass() + ") databases " +
141 |  |
                                          "must be of the same class");
142 | 4 |
         toDb.inSessionAsRoot(
143 | 4 |
                 new PoemTask() {
144 |  |
                   public void run() {
145 | 4 |
                     System.err.println("PoemThread " + PoemThread.database().getDisplayName());
146 |  |
                     try {
147 | 4 |
                       Enumeration<Table> en = fromDb.getDisplayTables(null);
148 | 100 |
                       while(en.hasMoreElements()) {
149 | 96 |
                         Table fromTable = (Table)en.nextElement();
150 | 96 |
                         String fromTableName = fromTable.getName();
151 | 96 |
                         System.err.println("From " + fromDb + " table " + fromTableName);
152 | 96 |
                         Table toTable = toDb.getTable(fromTableName);
153 | 96 |
                         int count = toTable.count();
154 | 96 |
                         if (count != 0 ) {
155 | 38 |
                           System.err.println("Skipping " + toTable.getName() + " as it contains " + count + " records." );
156 |  |
                         } else {
157 | 58 |
                           System.err.println(toTable.getName() + " in both and empty in destination.");
158 | 58 |
                           Enumeration<Persistent> recs = objectsFromTroids(
159 |  |
                                   fromTable.troidSelection((String)null,
160 |  |
                                                            (String)null, false, null),
161 |  |
                                                            fromTable);
162 | 60 |
                           while (recs.hasMoreElements()) {
163 | 2 |
                             Persistent p = (Persistent)recs.nextElement();
164 | 2 |
                             Persistent p2 = toTable.newPersistent();
165 | 2 |
                             Enumeration<Field> fields = p.getFields();
166 | 8 |
                             while (fields.hasMoreElements()) {
167 | 6 |
                               Field f = fields.nextElement();
168 | 6 |
                               p2.setRaw(f.getName(), f.getRaw());
169 | 6 |
                             }
170 | 2 |
                             p2.makePersistent();
171 | 2 |
                             System.err.println("Created:" + p2.displayString());
172 | 2 |
                           }
173 |  |
                         }
174 | 96 |
                        }
175 | 0 |
                     } catch (Throwable e) {
176 | 0 |
                       e.printStackTrace();
177 | 0 |
                       e.fillInStackTrace();
178 | 0 |
                       throw new RuntimeException(e);
179 | 4 |
                     }
180 | 4 |
                   }
181 |  |
                   public String toString() {
182 | 0 |
                     return "Copying";
183 |  |
                   }
184 |  |
                 });
185 | 4 |
         return toDb;
186 |  |
187 |  |
       }
188 |  |
       static Enumeration<Persistent> objectsFromTroids(Enumeration<Integer> troids, final Table t) {
189 | 60 |
         return new MappedEnumeration<Persistent, Integer>(troids) {
190 |  |
             public Persistent mapped(Integer troid) {
191 | 2 |
               return t.getObject(troid);
192 |  |
             }
193 |  |
           };
194 |  |
       }
195 |  |
196 |  |
     }