View Javadoc

1   /***
2    * 
3    */
4   package org.dbe.studio.tools.accounting.metering.wizards;
5   
6   import java.io.File;
7   import java.io.FileOutputStream;
8   import java.io.IOException;
9   import java.io.OutputStream;
10  import java.util.Properties;
11  
12  import org.apache.log4j.BasicConfigurator;
13  import org.apache.log4j.Level;
14  import org.apache.log4j.Logger;
15  import org.dbe.core.dar.DarFile;
16  import org.dbe.core.dar.DarUtils;
17  import org.eclipse.core.resources.ResourcesPlugin;
18  import org.eclipse.core.runtime.Platform;
19  import org.eclipse.core.runtime.preferences.IPreferencesService;
20  import org.eclipse.jface.dialogs.MessageDialog;
21  import org.eclipse.jface.viewers.IStructuredSelection;
22  import org.eclipse.jface.wizard.Wizard;
23  import org.eclipse.ui.IWorkbench;
24  
25  /***
26   * @author andy
27   */
28  public class AccountingWizard extends Wizard {
29  
30  	private String darFile;
31  
32  	private String url;
33  
34  	private IPreferencesService service;
35  
36  	private MeteringFilterWizardPage1 wizPage1;
37  
38  	// **** new for accounting
39  
40  	private static Logger logger = Logger.getLogger(AccountingWizard.class
41  			.getName());
42  
43  	private static final String DEPLOYMENT_PROPS = "deployment.props";
44  
45  	// *****
46  
47  	/***
48  	 * 
49  	 */
50  	private AccountingWizard() {
51  		super();
52  		this.darFile = "";
53  	}
54  
55  	public AccountingWizard(String darFile) {
56  		super();
57  		logger.info("setting dar file");
58  		this.darFile = darFile;
59  		if (darFile != null)
60  			logger.info(darFile.toString());
61  		service = Platform.getPreferencesService();
62  
63  		this.url = service.getString(
64  				"org.dbe.ui.eclipse.plugins.DBEPreferencesAndConfigPlugin",
65  				"serventURL", "", null);
66  
67  		BasicConfigurator.configure();
68  		logger = Logger.getRootLogger();
69  		logger.setLevel(Level.INFO);
70  
71  	}
72  
73  	public void addPages() {
74  		super.addPages();
75  		wizPage1 = new MeteringFilterWizardPage1(darFile);
76  
77  		this.addPage(wizPage1);
78  
79  	}
80  
81  	public boolean performFinish() {
82  		try {
83  			this.updateDeploymentProperties(wizPage1
84  					.filterDeploymentProperties());
85  		} catch (Exception e) {
86  			MessageDialog.openError(getShell(), "Error", e.getMessage());
87  			logger.error("Exception: " + e.toString() + e.getMessage());
88  			e.printStackTrace();
89  			return false;
90  		}
91  		return true;
92  	}
93  
94  	private void updateDeploymentProperties(Properties filterProperties) {
95  
96  		try {
97  
98  			File deployProps = new File(DEPLOYMENT_PROPS);
99  			deployProps.deleteOnExit();
100 			if (!deployProps.exists())
101 				deployProps.createNewFile();
102 
103 			File origFile = new File(this.darFile);
104 			DarFile tempDarFile = new DarFile(origFile);
105 			deployProps = DarUtils.extract(tempDarFile, DEPLOYMENT_PROPS,
106 					origFile.getParent() + File.separator + DEPLOYMENT_PROPS);
107 
108 			PropertiesFile deployPropsFile = new PropertiesFile(deployProps
109 					.getAbsolutePath());
110 
111 			deployPropsFile.setPropertys(filterProperties);
112 
113 			File newDar = DarUtils.add(tempDarFile, deployProps);
114 			deployProps.delete();
115 			deployPropsFile.delete();
116 			tempDarFile.close();
117 
118 			origFile.delete();
119 
120 			if (!newDar.renameTo(origFile))
121 				logger
122 						.error("Could not update Deployment Properties - unable to rename Dar file.");
123 
124 		} catch (IOException e) {
125 			logger.error("Could not update Deployment Properties: " + e);
126 		}
127 
128 	}
129 
130 	public void init(IWorkbench workbench, IStructuredSelection selection) {
131 		try {
132 			wizPage1.populate();
133 		} catch (Exception e) {
134 			// TODO Auto-generated catch block
135 			e.printStackTrace();
136 		}
137 	}
138 }