View Javadoc

1   /*--------------------------------------------------------------------------
2    *  Copyright 2008 utgenome.org
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   *--------------------------------------------------------------------------*/
16  //--------------------------------------
17  // utgb-shell Project
18  //
19  // Upgrade.java
20  // Since: May 3, 2010
21  //
22  // $URL$ 
23  // $Author$
24  //--------------------------------------
25  package org.utgenome.shell;
26  
27  import java.io.BufferedReader;
28  import java.io.File;
29  import java.io.FileReader;
30  import java.io.FileWriter;
31  
32  import org.utgenome.config.OldViewXML;
33  import org.utgenome.config.TrackConfiguration;
34  import org.utgenome.config.UTGBConfig;
35  import org.utgenome.gwt.utgb.client.view.TrackView;
36  import org.utgenome.shell.Create.ScaffoldFileFilter;
37  import org.xerial.lens.XMLLens;
38  import org.xerial.silk.SilkWriter;
39  import org.xerial.util.FileType;
40  import org.xerial.util.StringUtil;
41  import org.xerial.util.log.Logger;
42  
43  public class Upgrade extends UTGBShellCommand {
44  
45  	private static Logger _logger = Logger.getLogger(Upgrade.class);
46  
47  	@Override
48  	public void execute(String[] args) throws Exception {
49  
50  		// upgrade the old track-config.xml
51  		if (!isInProjectRoot()) {
52  			// no configuration file is found
53  			File oldConfigXML = getObsolteConfigurationFile();
54  			if (oldConfigXML.exists()) {
55  				_logger.info(String.format("old-track configuration file %s is found", oldConfigXML));
56  
57  				// convert the old config file to the silk format
58  				TrackConfiguration oldConfig = XMLLens.loadXML(TrackConfiguration.class, new BufferedReader(new FileReader(oldConfigXML)));
59  				UTGBConfig newConfig = oldConfig.convert();
60  				String silk = newConfig.toSilk();
61  
62  				// output the new configuration file
63  				File newConfigFile = new File("config/common.silk");
64  				if (!newConfigFile.exists()) {
65  					_logger.info("needs upgrade");
66  					_logger.info("generating " + newConfigFile);
67  					FileWriter fout = new FileWriter(newConfigFile);
68  					fout.append(silk);
69  					fout.append(StringUtil.newline());
70  					fout.flush();
71  					fout.close();
72  				}
73  
74  				// generate the new configuration files
75  				for (String configFile : new String[] { "config/development.silk", "config/production.silk", "config/test.silk" }) {
76  					final String targetFile = configFile;
77  					Create.createScaffold(newConfig, "./", new ScaffoldFileFilter() {
78  						public boolean accept(String logicalPathName) {
79  							return logicalPathName.startsWith(targetFile);
80  						}
81  					});
82  				}
83  			}
84  		}
85  
86  		// upgrade the view XML files
87  		if (isInProjectRoot()) {
88  			_logger.info("converting view files...");
89  			File oldViewFolder = new File(getProjectRoot(), "src/main/webapp/view");
90  			File[] viewXMLFiles = oldViewFolder.listFiles();
91  			if (viewXMLFiles != null) {
92  
93  				// create view folder
94  				File viewFolder = new File(getProjectRoot(), "config/view");
95  				viewFolder.mkdirs();
96  
97  				// convert the old view file to new view Silk file
98  				for (File viewXML : viewXMLFiles) {
99  					if (FileType.getFileType(viewXML.getName()) != FileType.XML) {
100 						_logger.info(String.format("skip %s", viewXML));
101 						continue;
102 					}
103 
104 					File newViewFile = new File(viewFolder, FileType.removeFileExt(viewXML.getName()) + ".silk");
105 					if (newViewFile.exists()) {
106 						_logger.info(String.format("skip convertion from %s to %s: %s exists", viewXML, newViewFile, newViewFile));
107 						continue;
108 					}
109 
110 					_logger.info(String.format("generating %s from %s", newViewFile, viewXML));
111 					OldViewXML oldView = XMLLens.loadXML(OldViewXML.class, new FileReader(viewXML));
112 					TrackView tv = oldView.toTrackView();
113 					SilkWriter silk = new SilkWriter(new FileWriter(newViewFile));
114 					silk.preamble();
115 					silk.toSilk(tv);
116 					silk.close();
117 				}
118 			}
119 		}
120 
121 		// upgrade pom.xml
122 		if (isInProjectRoot()) {
123 			// upgrading pom.xml
124 			_logger.info("upgrade pom.xml");
125 			UTGBShell.runCommand(globalOption, "repair -y -f pom.xml");
126 		}
127 
128 		_logger.info("upgrade done.");
129 	}
130 
131 	@Override
132 	public String getOneLinerDescription() {
133 		return "upgrade your UTGB project";
134 	}
135 
136 	@Override
137 	public String name() {
138 		return "upgrade";
139 	}
140 
141 }