001: /*
002: * Created on 2006.4.27
003: * Milin Radivoj
004: * rmilin@gmail.com
005: */
006: package org.enhydra.snapperAdmin.business;
007:
008: import java.util.Enumeration;
009: import java.util.Hashtable;
010: import java.util.List;
011: import java.util.Vector;
012:
013: import org.enhydra.snapperAdmin.SnapperAdmin;
014: import org.enhydra.snapperAdmin.business.FileTypeImpl;
015: import org.enhydra.snapperAdmin.spec.*;
016: import org.enhydra.snapper.xml.configuration.DOCUMENTGROUPDocument.DOCUMENTGROUP;
017: import org.enhydra.snapper.xml.configuration.DOCUMENTGROUPLISTDocument.DOCUMENTGROUPLIST;
018: import org.enhydra.snapper.xml.configuration.FILETYPEDocument.FILETYPE;
019:
020: /**
021: * @author Milin Radivoj
022: */
023:
024: public class DocumentGroupImpl implements DocumentGroup {
025:
026: private Hashtable table = new Hashtable();
027:
028: private FileType[] listOfAll = null;
029:
030: private boolean isDefaultDocGroup = false;
031:
032: public List getListOfDocumentGroup() throws Exception {
033:
034: Vector result = new Vector();
035:
036: for (Enumeration e = table.keys(); e.hasMoreElements();) {
037:
038: result.add(e.nextElement());
039: }
040: return result;
041: }
042:
043: public FileType[] getListOfFILETYPES(String groupName)
044: throws Exception {
045:
046: return (FileType[]) table.get(groupName);
047: }
048:
049: public FileType[] getListOfAllFILETYPES() throws Exception {
050:
051: return listOfAll;
052: }
053:
054: public void addFileType(FileType ft) {
055: FileType[] newListOfAll = null;
056: Vector exist = new Vector();
057:
058: if (listOfAll != null) {
059: newListOfAll = new FileType[listOfAll.length + 1];
060:
061: for (int i = 0; i < listOfAll.length; i++) {
062: newListOfAll[i] = listOfAll[i];
063: exist.add(listOfAll[i].getEXTENSION().toLowerCase());
064: }
065:
066: if (!exist.contains(ft.getEXTENSION().toLowerCase()))
067: newListOfAll[newListOfAll.length - 1] = ft;
068: else
069: newListOfAll = listOfAll;
070: } else {
071: newListOfAll = new FileType[1];
072: newListOfAll[0] = ft;
073: }
074:
075: listOfAll = newListOfAll;
076:
077: FileType[] ftlist = (FileType[]) table.get("All supported");
078:
079: if (ftlist != null) {
080:
081: table.remove("All supported");
082: table.put("All supported", listOfAll);
083: }
084:
085: }
086:
087: public void removeFileType(String extension) {
088:
089: Vector all = new Vector();
090:
091: for (int i = 0; i < listOfAll.length; i++) {
092:
093: if (!listOfAll[i].getEXTENSION().equals(extension)) {
094: all.add(listOfAll[i]);
095: }
096: }
097:
098: listOfAll = new FileType[all.size()];
099:
100: for (int i = 0; i < all.size(); i++) {
101: listOfAll[i] = (FileType) all.get(i);
102: }
103:
104: for (Enumeration e = table.keys(); e.hasMoreElements();) {
105: String groupName = (String) e.nextElement();
106: FileType[] ft = (FileType[]) table.get(groupName);
107:
108: Vector allByGroups = new Vector();
109:
110: for (int i = 0; i < ft.length; i++) {
111:
112: if (!ft[i].getEXTENSION().equals(extension)) {
113: allByGroups.add(ft[i]);
114: }
115: }
116:
117: ft = new FileType[allByGroups.size()];
118:
119: for (int i = 0; i < allByGroups.size(); i++) {
120: ft[i] = (FileType) allByGroups.get(i);
121: }
122: table.remove(groupName);
123: table.put(groupName, ft);
124:
125: }
126:
127: }
128:
129: public void updateFileType(String extension, FileType f) {
130:
131: for (int i = 0; i < listOfAll.length; i++) {
132:
133: if (listOfAll[i].getEXTENSION().equals(extension)) {
134: listOfAll[i] = f;
135: }
136: }
137:
138: for (Enumeration e = table.keys(); e.hasMoreElements();) {
139: String groupName = (String) e.nextElement();
140: FileType[] ft = (FileType[]) table.get(groupName);
141:
142: for (int i = 0; i < ft.length; i++) {
143:
144: if (ft[i].getEXTENSION().equals(extension)) {
145: ft[i] = f;
146: }
147: }
148: }
149: }
150:
151: public void init(DOCUMENTGROUPLIST list) {
152:
153: try {
154: int size = list.getDOCUMENTGROUPArray().length;
155:
156: if (size < 1) {
157: SnapperAdmin
158: .logWarrning("None document group is defined !!!");
159: return;
160: }
161:
162: Vector extension = new Vector();
163: Vector all = new Vector();
164:
165: for (int i = 0; i < size; i++) {
166: DOCUMENTGROUP dg = (DOCUMENTGROUP) list
167: .getDOCUMENTGROUPArray(i);
168: String name = dg.getNAME();
169:
170: FILETYPE[] ftl = dg.getFILETYPEArray();
171:
172: FileType[] ftList = new FileType[ftl.length];
173:
174: for (int j = 0; j < ftl.length; j++) {
175: FileType ft = new FileTypeImpl();
176: FILETYPE zeusFt = (FILETYPE) ftl[j];
177:
178: ft.setNAME(zeusFt.getNAME());
179: ft.setEXTENSION(zeusFt.getEXTENSION());
180: ft.setPARSER(zeusFt.getPARSEAS());
181: ftList[j] = ft;
182:
183: if (!extension.contains(ft.getEXTENSION()
184: .toLowerCase())) {
185: all.add(ft);
186: extension.add(ft.getEXTENSION().toLowerCase());
187: }
188: }
189: table.put(name, ftList);
190: }
191:
192: listOfAll = new FileType[all.size()];
193:
194: for (int i = 0; i < all.size(); i++) {
195: listOfAll[i] = (FileType) all.get(i);
196: }
197:
198: table.put("All supported", listOfAll);
199:
200: } catch (Exception e) {
201: SnapperAdmin
202: .logWarrning("Problem ocured while initialyzing document group list : "
203: + e.getMessage());
204: }
205:
206: }
207:
208: private void update() {
209:
210: Vector all = new Vector();
211: Vector extension = new Vector();
212:
213: for (Enumeration e = table.keys(); e.hasMoreElements();) {
214:
215: String groupname = (String) e.nextElement();
216:
217: if (!groupname.equals("All supported")) {
218:
219: FileType[] groupTypes = (FileType[]) table
220: .get(groupname);
221:
222: for (int j = 0; j < groupTypes.length; j++) {
223: if (!extension.contains(groupTypes[j]
224: .getEXTENSION().toLowerCase())) {
225: all.add(groupTypes[j]);
226: extension.add(groupTypes[j].getEXTENSION()
227: .toLowerCase());
228:
229: }
230: }
231: }
232: }
233:
234: listOfAll = new FileType[all.size()];
235: for (int i = 0; i < all.size(); i++) {
236: listOfAll[i] = (FileType) all.get(i);
237: }
238:
239: table.remove("All supported");
240: table.put("All supported", listOfAll);
241:
242: }
243:
244: public boolean isDefaultDocGroup() {
245: return isDefaultDocGroup;
246: }
247:
248: public void setIsDefaultDocGroup(boolean is) {
249: isDefaultDocGroup = is;
250: }
251:
252: public void addDocumentGroup(String groupName, FileType[] ftl) {
253: if (groupName == null || groupName.equals("")
254: || table.contains(groupName) || ftl == null)
255: return;
256: else
257: table.put(groupName, ftl);
258:
259: update();
260:
261: }
262:
263: public void updateDocumentGroup(String groupName, FileType[] ftl) {
264: if (groupName != null && ftl != null) {
265: table.remove(groupName);
266: table.put(groupName, ftl);
267: update();
268: }
269:
270: }
271:
272: public void removeDocumentGroup(String groupName) {
273: if (groupName != null) {
274: table.remove(groupName);
275: update();
276: }
277: }
278:
279: }
|