001: package org.enhydra.snapper.business;
002:
003: /**
004: * @author Milin Radivoj
005: *
006: */
007:
008: import java.io.File;
009: import java.net.URLEncoder;
010: import java.util.Hashtable;
011: import java.util.StringTokenizer;
012:
013: import org.enhydra.snapper.Snapper;
014: import org.enhydra.snapper.Log;
015: import org.enhydra.snapper.business.SiteListImpl;
016: import org.enhydra.snapper.spec.Path;
017: import org.enhydra.snapper.spec.Site;
018:
019: import com.lutris.logging.Logger;
020:
021: public class Mapper {
022:
023: Hashtable downloadProperties = new Hashtable();
024:
025: Hashtable table = new Hashtable();
026: Site[] list = null;
027:
028: public Mapper() {
029: try {
030: SiteListImpl sl = new SiteListImpl();
031: list = sl.getList();
032: if (list != null) {
033: for (int i = 0; i < list.length; i++) {
034: Site currentSite = list[i];
035: String name = currentSite.getNAME();
036:
037: boolean download = currentSite.getDOWNLOAD();
038:
039: Path[] pathArray;
040:
041: pathArray = currentSite.getPathList();
042:
043: if (pathArray != null)
044: table.put(name, pathArray);
045:
046: if (download)
047: downloadProperties.put(name, "true");
048: else
049: downloadProperties.put(name, "false");
050: }
051: }
052:
053: } catch (Exception e) {
054: Log.logException(e);
055:
056: }
057: }
058:
059: public void close() {
060: try {
061: downloadProperties.clear();
062: downloadProperties = null;
063:
064: table.clear();
065: table = null;
066:
067: list = null;
068: } catch (Exception e) {
069: Log.logException(e);
070:
071: }
072: }
073:
074: public String mapFullPathToLogical(String fullPath, String site,
075: String type) {
076:
077: String result = new String();
078: try {
079: Path[] pathArray = (Path[]) table.get(site);
080:
081: if (pathArray != null) {
082: for (int num = 0; num < pathArray.length; num++) {
083: Path currentPath = pathArray[num];
084:
085: if (currentPath == null)
086: continue;
087:
088: StringBuffer finalPath = new StringBuffer();
089:
090: String host = currentPath.getHost();
091: String pathRoot = currentPath.getRoot();
092: String mappingRoot = currentPath.getMappingRoot();
093: String pathType = currentPath.getType();
094:
095: String PathTypeString = currentPath.getType();
096:
097: if (PathTypeString.equals("FTP")) {
098: finalPath.append("ftp://");
099: }
100:
101: if (!host.equals("")) {
102: if (host.endsWith(File.separator)) {
103: if (pathRoot.startsWith(File.separator)) {
104: String temp = pathRoot.replaceFirst(
105: File.separator, "");
106: finalPath.append(host + temp);
107: } else
108: finalPath.append(host + pathRoot);
109: } else {
110: if (pathRoot.startsWith(File.separator))
111: finalPath.append(host + pathRoot);
112: else
113: finalPath.append(host + File.separator
114: + pathRoot);
115: }
116: } else {
117: finalPath.append(pathRoot);
118: }
119:
120: String finall = finalPath.toString();
121: //final je ftp + host ili je pathRoot
122:
123: if (Snapper.getRelativeIndexPath()
124: .equalsIgnoreCase("true")) {
125: if (!mappingRoot.equals("")) {
126: if (mappingRoot.endsWith(File.separator))
127: result = mappingRoot + fullPath;
128: else
129: result = mappingRoot + File.separator
130: + fullPath;
131:
132: if (Snapper.getFileSeparatorConvention() != null
133: && Snapper
134: .getFileSeparatorConvention()
135: .equalsIgnoreCase("unix")) {
136: result = result.replaceAll("\\\\", "/");
137: }
138:
139: return result;
140: } else {
141: if (pathRoot.endsWith(File.separator))
142: result = pathRoot + fullPath;
143: else
144: result = pathRoot + File.separator
145: + fullPath;
146:
147: if (Snapper.getFileSeparatorConvention() != null
148: && Snapper
149: .getFileSeparatorConvention()
150: .equalsIgnoreCase("unix")) {
151: result = result.replaceAll("\\\\", "/");
152: }
153:
154: return result;
155:
156: }
157: } else if (fullPath.startsWith(finall)) {
158:
159: if (!mappingRoot.equals("")) {
160: if (mappingRoot.endsWith(File.separator))
161: result = mappingRoot
162: + fullPath.substring(finall
163: .length() + 1);
164: else
165: result = mappingRoot
166: + File.separator
167: + fullPath.substring(finall
168: .length() + 1);
169:
170: if (Snapper.getFileSeparatorConvention() != null
171: && Snapper
172: .getFileSeparatorConvention()
173: .equalsIgnoreCase("unix")) {
174: result = result.replaceAll("\\\\", "/");
175: }
176:
177: return result;
178: } else {
179: if (Snapper.getFileSeparatorConvention() != null
180: && Snapper
181: .getFileSeparatorConvention()
182: .equalsIgnoreCase("unix")) {
183: fullPath = fullPath.replaceAll("\\\\",
184: "/");
185: }
186:
187: return fullPath;
188:
189: }
190: }
191:
192: }
193: } else {
194: // Snapper.log.write(Logger.WARNING," Mapper : mapFullPathToLogical() : no path are defined for site : "+site);
195: return "";
196: }
197: } catch (Exception e) {
198: Log.logException(e);
199: return new String();
200: }
201: if (Snapper.getFileSeparatorConvention() != null
202: && Snapper.getFileSeparatorConvention()
203: .equalsIgnoreCase("unix")) {
204: fullPath = fullPath.replaceAll("\\\\", "/");
205: }
206:
207: return fullPath;
208:
209: }
210:
211: public String getRelativeDirPath(String fullPath, String fileName,
212: String site) {
213:
214: try {
215: Path[] pathArray = (Path[]) table.get(site);
216: if (pathArray != null) {
217: for (int num = 0; num < pathArray.length; num++) {
218: Path currentPath = pathArray[num];
219:
220: if (currentPath == null)
221: continue;
222:
223: String pathRoot = currentPath.getRoot();
224:
225: if (fullPath.startsWith(pathRoot)) {
226:
227: String temp = fullPath.substring(pathRoot
228: .length(), fullPath.length());
229: if (!fullPath.endsWith("zip")
230: && !fullPath.endsWith("ZIP")) {
231: temp = temp.substring(0, temp
232: .indexOf(fileName));
233: } else {
234: temp = temp.substring(0, temp
235: .lastIndexOf(File.separator) + 1);
236: }
237: if (Snapper.getFileSeparatorConvention() != null
238: && Snapper.getFileSeparatorConvention()
239: .equalsIgnoreCase("unix")) {
240: temp = temp.replaceAll("\\\\", "/");
241: }
242:
243: return temp;
244:
245: }
246: }
247: } else {
248: //Snapper.log.write(Logger.WARNING," Mapper : getRelativeDirPath() : no path are defined for site : "+site);
249: return "";
250: }
251: } catch (Exception e) {
252: Log.logException(e);
253: return "";
254: }
255: return "";
256:
257: }
258:
259: public String getRelativeFilePath(String fullPath, String fileName,
260: String site) {
261:
262: try {
263: Path[] pathArray = (Path[]) table.get(site);
264: if (pathArray != null) {
265: for (int num = 0; num < pathArray.length; num++) {
266: Path currentPath = pathArray[num];
267:
268: if (currentPath == null)
269: continue;
270:
271: String pathRoot = currentPath.getRoot();
272:
273: if (fullPath.startsWith(pathRoot)) {
274:
275: String temp = fullPath.substring(pathRoot
276: .length(), fullPath.length());
277:
278: if (Snapper.getFileSeparatorConvention() != null
279: && Snapper.getFileSeparatorConvention()
280: .equalsIgnoreCase("unix")) {
281: temp = temp.replaceAll("\\\\", "/");
282: }
283:
284: return temp;
285: }
286: }
287: } else {
288: //Snapper.log.write(Logger.WARNING," Mapper : getRelativeFilePath() : no path are defined for site : "+site);
289: return "";
290: }
291: } catch (Exception e) {
292: Log.logException(e);
293: return "";
294: }
295: return "";
296:
297: }
298:
299: public String getInsideContainerFilePath(String fullPath) {
300: String retVal = null;
301:
302: StringTokenizer st = new StringTokenizer(fullPath, "|");
303: if (st.countTokens() > 1) {
304: st.nextToken();
305: retVal = st.nextToken();
306: while (st.hasMoreTokens()) {
307: retVal += "|" + st.nextToken();
308: }
309: }
310:
311: return retVal;
312: }
313:
314: public String getAbsulutDirPath(String fullPath, String site) {
315:
316: try {
317: Path[] pathArray = (Path[]) table.get(site);
318: if (pathArray != null) {
319: for (int num = 0; num < pathArray.length; num++) {
320: Path currentPath = pathArray[num];
321:
322: if (currentPath == null)
323: continue;
324:
325: String pathRoot = currentPath.getRoot();
326:
327: if (fullPath.startsWith(pathRoot)) {
328:
329: if (Snapper.getFileSeparatorConvention() != null
330: && Snapper.getFileSeparatorConvention()
331: .equalsIgnoreCase("unix")) {
332: pathRoot = pathRoot.replaceAll("\\\\", "/");
333: }
334:
335: return pathRoot;
336:
337: }
338: }
339: } else {
340: //Snapper.log.write(Logger.WARNING," Mapper : getAbsulutDirPath() : no path are defined for site : "+site);
341: return "";
342: }
343: } catch (Exception e) {
344: Log.logException(e);
345: return "";
346: }
347: return "";
348:
349: }
350:
351: public String manage(String managedLink, String originlink) {
352: if (managedLink.startsWith("DM_DownloadPO"))
353: return originlink;
354: else
355: return managedLink;
356: }
357:
358: public String getGrantedDownload(String site) {
359: String download = (String) downloadProperties.get(site);
360: if (download.equals("true"))
361: return "true";
362: else
363: return "false";
364: }
365:
366: public String getDownloadHref(String fullPath, String site,
367: String type) {
368: try {
369: String download = (String) downloadProperties.get(site);
370:
371: if (download.equals("true")) {
372:
373: String temp = URLEncoder.encode(fullPath, "UTF-8");
374: String result = "DM_DownloadPO.po?url=" + temp
375: + "&type=" + type + "&id=" + site;
376: return result;
377: } else {
378: return fullPath;
379: }
380:
381: } catch (Exception e) {
382: Log.logException(e);
383: return new String();
384: }
385:
386: }
387:
388: public Hashtable getDownloadProperties() {
389: return downloadProperties;
390: }
391:
392: public Hashtable getTable() {
393: return table;
394: }
395:
396: public Site[] getSites() {
397: return list;
398: }
399: }
|