1 56 package org.objectstyle.cayenne.project; 57 58 import java.util.Collection ; 59 60 import org.objectstyle.cayenne.conf.Configuration; 61 62 66 abstract class ApplicationUpgradeHandler { 67 private static final ApplicationUpgradeHandler sharedInstance = 68 new UpgradeHandler_1_1(); 69 70 static ApplicationUpgradeHandler sharedHandler() { 71 return sharedInstance; 72 } 73 74 abstract String supportedVersion(); 75 76 abstract boolean checkForUpgrades(Configuration project, Collection appendMessages); 77 78 abstract void performUpgrade(ApplicationProject project) throws ProjectException; 79 80 int compareVersion(String version) { 81 double supported = decodeVersion(supportedVersion()); 82 double newVersion = decodeVersion(version); 83 return supported < newVersion ? -1 : (supported == newVersion) ? 0 : 1; 84 } 85 86 static double decodeVersion(String version) { 87 if (version == null || version.trim().length() == 0) { 88 return 0; 89 } 90 91 StringBuffer buffer = new StringBuffer (version.length()); 94 boolean dotProcessed = false; 95 for (int i = 0; i < version.length(); i++) { 96 char nextChar = version.charAt(i); 97 if (nextChar == '.' && !dotProcessed) { 98 dotProcessed = true; 99 buffer.append('.'); 100 } 101 else if (Character.isDigit(nextChar)) { 102 buffer.append(nextChar); 103 } 104 } 105 106 return Double.parseDouble(buffer.toString()); 107 } 108 109 static class UpgradeHandler_1_1 extends ApplicationUpgradeHandler { 110 String supportedVersion() { 111 return "1.1"; 112 } 113 114 void performUpgrade(ApplicationProject project) throws ProjectException { 115 project.setModified(true); 116 project.getConfiguration().setProjectVersion(supportedVersion()); 117 project.save(); 118 } 119 120 boolean checkForUpgrades(Configuration project, Collection appendMessages) { 121 String loadedVersion = project.getProjectVersion(); 122 int versionState = compareVersion(loadedVersion); 123 if (versionState < 0) { 124 String versionLabel = (loadedVersion != null) ? loadedVersion : "?"; 125 appendMessages.add( 126 "Newer Project Version Detected: \"" + versionLabel + "\""); 127 return true; 128 } 129 else if (versionState > 0) { 130 String versionLabel = (loadedVersion != null) ? loadedVersion : "?"; 131 appendMessages.add( 132 "Older Project Version Detected: \"" + versionLabel + "\""); 133 return true; 134 } 135 else { 136 return false; 137 } 138 } 139 } 140 } 141 | Popular Tags |