001: /*********************************************************************************
002: * The contents of this file are subject to the OpenI Public License Version 1.0
003: * ("License"); You may not use this file except in compliance with the
004: * License. You may obtain a copy of the License at
005: * http://www.openi.org/docs/LICENSE.txt
006: *
007: * Software distributed under the License is distributed on an "AS IS" basis,
008: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
009: * the specific language governing rights and limitations under the License.
010: *
011: * The Original Code is: OpenI Open Source
012: *
013: * The Initial Developer of the Original Code is Loyalty Matrix, Inc.
014: * Portions created by Loyalty Matrix, Inc. are
015: * Copyright (C) 2005 Loyalty Matrix, Inc.; All Rights Reserved.
016: *
017: * Contributor(s): i18n : Pedro Casals Fradera (2006/05/16)
018: *
019: ********************************************************************************/package org.openi.feeds.client;
020:
021: import java.io.File;
022: import java.net.URL;
023: import java.rmi.RemoteException;
024: import java.text.DateFormat;
025: import java.text.SimpleDateFormat;
026: import java.util.ArrayList;
027: import java.util.Arrays;
028: import java.util.Collection;
029: import java.util.Date;
030: import java.util.List;
031: import java.util.Map;
032: import java.util.TreeMap;
033:
034: import javax.xml.rpc.ServiceException;
035:
036: import org.openi.feeds.FeedsMetaData;
037: import org.openi.util.FileItem;
038: import org.openi.util.Folder;
039:
040: /**
041: * @author Dipendra Pokhrel <br>
042: *
043: * FeedsMetaData web service client facade
044: *
045: */
046: class FeedsServiceWSClient implements FeedsService {
047: FeedsMetaDataService svcInterface;
048: String remoteDir;
049:
050: FeedsServiceWSClient(URL url, String remoteDir)
051: throws FeedsServiceException {
052: FeedsMetaDataServiceServiceLocator locator = new FeedsMetaDataServiceServiceLocator();
053: try {
054: svcInterface = locator.getFeedsMetaDataService(url);
055: if (remoteDir == null || "".equals(remoteDir.trim())) {
056: this .remoteDir = null;
057: } else {
058: this .remoteDir = remoteDir.trim().replace('\\', '/');
059: if (!this .remoteDir.startsWith("/"))
060: this .remoteDir = "/" + remoteDir;
061: }
062:
063: } catch (ServiceException e) {
064: throw new FeedsServiceException(e);
065: }
066: }
067:
068: public Folder listFolders(String extensions)
069: throws FeedsServiceException {
070: try {
071: return getRemoteFolderList(extensions);
072: } catch (Exception e) {
073: throw new FeedsServiceException(e);
074: }
075: }
076:
077: public Folder listFolders(String dir, boolean foldersOnly,
078: String pattern) throws FeedsServiceException {
079: Map treemap;
080: try {
081: treemap = svcInterface.listDir(dir, foldersOnly, pattern);
082: } catch (RemoteException e) {
083: throw new FeedsServiceException(e);
084: }
085: Folder folder = new Folder();
086: folder.setDisplayName("root");
087: buildFolderListFromMap(folder, treemap);
088: return folder;
089: }
090:
091: private Folder getRemoteFolderList(String extensions)
092: throws Exception {
093: Map treemap = svcInterface.listAll(remoteDir, extensions);
094: Folder folder = new Folder();
095: folder.setDisplayName("root");
096: buildFolderListFromMap(folder, treemap);
097: return folder;
098: }
099:
100: private void buildFolderListFromMap(Folder folder, Map treemap) {
101: if (treemap == null)
102: return;
103: String keys[] = (String[]) treemap.keySet().toArray(
104: new String[treemap.keySet().size()]);
105: Arrays.sort(keys, String.CASE_INSENSITIVE_ORDER);
106: for (int i = 0; i < keys.length; i++) {
107: String key = keys[i];
108: if ("type".equals(key))
109: continue;
110: Map value = (Map) treemap.get(key);
111: if ("file".equals(value.get("type"))) {
112: Collection children = folder.getChildren();
113: if (children == null) {
114: children = new ArrayList();
115: folder.setChildren(children);
116: }
117: FileItem fileItem = new FileItem();
118: children.add(fileItem);
119: fileItem.setPath(key);
120: String size = (String) value.get("size");
121: String modifiedDate = (String) value.get("modified");
122: if (size != null) {
123: try {
124: fileItem
125: .setSize(Long.valueOf(size).longValue());
126: } catch (Throwable t) {
127: }
128: }
129: if (modifiedDate != null) {
130: try {
131: long date = Long.valueOf(modifiedDate)
132: .longValue();
133: DateFormat format = SimpleDateFormat
134: .getDateInstance(SimpleDateFormat.SHORT);
135: fileItem.setDateModified(format
136: .format(new Date(date)));
137: } catch (Throwable t) {
138: }
139: }
140: fileItem.setDisplayName(new File(key).getName());
141: } else if ("dir".equals(value.get("type"))) {
142: Collection children = folder.getChildren();
143: if (children == null) {
144: children = new ArrayList();
145: folder.setChildren(children);
146: }
147: Folder subfolder = new Folder();
148: children.add(subfolder);
149: subfolder.setDisplayName(new File(key).getName());
150: subfolder.setPath(key);
151: buildFolderListFromMap(subfolder, value);
152: }
153: }
154: }
155:
156: private String resolvePath(String path) {
157: if (remoteDir == null)
158: return path;
159: path = path.replace('\\', '/');
160: if (path.startsWith(remoteDir))
161: return path;
162:
163: if (path.startsWith("/"))
164: path = remoteDir + path;
165: else
166: path = remoteDir + "/" + path;
167: return path;
168: }
169:
170: public List listMetaData(String path) throws FeedsServiceException {
171: try {
172: return fromMetaDataArrayToList(svcInterface
173: .listMetaData(resolvePath(path)));
174: } catch (Exception e) {
175: throw new FeedsServiceException(e);
176: }
177: }
178:
179: private List fromMetaDataArrayToList(FeedsMetaData[] metaDatas)
180: throws Exception {
181: List metaList = new ArrayList();
182: for (int i = 0; i < metaDatas.length; i++) {
183: if (metaDatas[i].getAttributes() != null) {
184: Map sorted = sortMap((Map) metaDatas[i].getAttributes()
185: .get("lineDetails"));
186: metaDatas[i].getAttributes().put("lineDetails", sorted);
187: }
188: metaList.add(metaDatas[i]);
189: }
190: return metaList;
191: }
192:
193: private Map sortMap(Map map) {
194: if (map == null)
195: return null;
196: Map sorted = new TreeMap(map);
197: return sorted;
198: }
199:
200: public FeedsMetaData parse(String path, FeedsMetaData metaData)
201: throws FeedsServiceException {
202: try {
203: FeedsMetaData metadata = svcInterface.parse(
204: resolvePath(path), metaData);
205: if (metadata.getAttributes() != null) {
206: metadata.getAttributes().put(
207: "lineDetails",
208: sortMap((Map) metadata.getAttributes().get(
209: "lineDetails")));
210: }
211: return metadata;
212: } catch (RemoteException e) {
213: throw new FeedsServiceException(e);
214: }
215: }
216:
217: public FeedsMetaData reparse(String path)
218: throws FeedsServiceException {
219: try {
220: FeedsMetaData metadata = svcInterface
221: .reparse(resolvePath(path));
222: if (metadata.getAttributes() != null) {
223: metadata.getAttributes().put(
224: "lineDetails",
225: sortMap((Map) metadata.getAttributes().get(
226: "lineDetails")));
227: }
228: return metadata;
229: } catch (RemoteException e) {
230: throw new FeedsServiceException(e);
231: }
232: }
233:
234: }
|