001: package org.enhydra.dm.business;
002:
003: import java.sql.SQLException;
004: import java.util.Iterator;
005: import java.util.Map;
006:
007: import org.enhydra.dm.api.FoDocument;
008: import org.enhydra.dm.api.FoDocumentManager;
009: import org.enhydra.dm.api.FoDocumentParam;
010: import org.enhydra.dm.api.exceptions.BaseException;
011: import org.enhydra.dm.api.loggers.Log;
012: import org.enhydra.dm.business.exceptions.DatabaseException;
013: import org.enhydra.dm.data.FoDocumentDO;
014: import org.enhydra.dm.data.FoDocumentParamDO;
015: import org.enhydra.dm.data.FoDocumentParamQuery;
016: import org.enhydra.dm.data.FoDocumentQuery;
017:
018: import com.lutris.appserver.server.sql.DBRowUpdateException;
019: import com.lutris.appserver.server.sql.DatabaseManagerException;
020: import com.lutris.dods.builder.generator.query.DataObjectException;
021: import com.lutris.dods.builder.generator.query.NonUniqueQueryException;
022: import com.lutris.dods.builder.generator.query.QueryException;
023: import com.lutris.dods.builder.generator.query.RefAssertionException;
024:
025: public class FoDocumentManagerImpl implements FoDocumentManager {
026:
027: private Log logger = null;
028:
029: /**
030: * Empty constructor
031: */
032: public FoDocumentManagerImpl() {
033:
034: }
035:
036: /**
037: * @param parameters
038: * @param userPwd
039: * @param ownerPwd
040: * @param allowPrint
041: * @param allowCopyContent
042: * @param allowEditContent
043: * @param allowEditAnnotations
044: * @return foDocumentId
045: * @throws BaseException
046: */
047: public String create(Map parameters, String userPwd,
048: String ownerPwd, boolean allowPrint,
049: boolean allowCopyContent, boolean allowEditContent,
050: boolean allowEditAnnotation) throws BaseException {
051: if (null != getLogger()) {
052: getLogger().log(Log.DEBUG, "Create action for foDocument.");
053: }
054:
055: try {
056:
057: FoDocument foDocument = new FoDocumentImpl();
058: foDocument.setUserPassword(userPwd);
059: foDocument.setOwnerPassword(ownerPwd);
060: foDocument.setAllowPrint(allowPrint);
061: foDocument.setAllowCopyContent(allowCopyContent);
062: foDocument.setAllowEditContent(allowEditContent);
063: foDocument.setAllowEditAnnotation(allowEditAnnotation);
064:
065: FoDocumentDO foDocumentDO = null;
066:
067: foDocumentDO = foDocument.createDO();
068: foDocumentDO.save();
069:
070: Iterator entries = parameters.entrySet().iterator();
071:
072: while (entries.hasNext()) {
073: try {
074: Map.Entry me = (Map.Entry) entries.next();
075: String name = (String) me.getKey();
076: String value = (String) me.getValue();
077: if (value != null) {
078: FoDocumentParam foDocumentParam = new FoDocumentParamImpl();
079: foDocumentParam.setName(name);
080: foDocumentParam.setContent(value);
081: FoDocumentParamDO fdpDO = foDocumentParam
082: .createDO();
083: fdpDO.setFODOCUMENTREF(foDocumentDO);
084: fdpDO.save();
085: }
086: } catch (Exception ex) {
087: throw new DatabaseException(
088: "Failed to create FoDocument Parameter entry!");
089: }
090: }
091:
092: if (null != getLogger()) {
093: getLogger().log(Log.DEBUG, "foDocument is created.");
094: }
095: return foDocumentDO.get_OId().toString();
096:
097: } catch (DatabaseManagerException e) {
098: throw new DatabaseException(e);
099: } catch (DataObjectException e) {
100: throw new DatabaseException(e);
101: } catch (RefAssertionException e) {
102: throw new DatabaseException(e);
103: } catch (DBRowUpdateException e) {
104: throw new DatabaseException(e);
105: } catch (QueryException e) {
106: throw new DatabaseException(e);
107: } catch (SQLException e) {
108: throw new DatabaseException(e);
109: }
110: }
111:
112: /**
113: * Getter for logger
114: *
115: * @return
116: */
117: public Log getLogger() {
118: return logger;
119: }
120:
121: /**
122: * Setter for logger
123: *
124: * @param logger
125: */
126: public void setLogger(Log logger) {
127: this .logger = logger;
128: }
129:
130: /**
131: * @param id
132: * @return
133: * @throws BaseException
134: */
135: public FoDocument getFoDocumentById(String id) throws BaseException {
136: FoDocument foDocument = null;
137: foDocument = new FoDocumentImpl(getFoDocumentDO(id));
138:
139: return foDocument;
140: }
141:
142: public FoDocumentParam[] getFoDocumentParametersById(String id)
143: throws BaseException {
144: FoDocumentParamDO[] foDocumentParamDOArray = null;
145: FoDocumentParam[] foDocumentParamArray = null;
146: FoDocumentParamQuery foDocumentParamQuery = new FoDocumentParamQuery();
147: ;
148:
149: try {
150: foDocumentParamQuery
151: .setQueryFODOCUMENTREF(getFoDocumentDO(id));
152: foDocumentParamDOArray = foDocumentParamQuery.getDOArray();
153:
154: } catch (DataObjectException e) {
155: throw new DatabaseException(e);
156: } catch (QueryException e) {
157: throw new DatabaseException(e);
158: } catch (NonUniqueQueryException e) {
159: throw new DatabaseException(e);
160: }
161: if (foDocumentParamDOArray == null) {
162: throw new DatabaseException(
163: "FoDocument parameters not found (ID) - " + id);
164: }
165:
166: foDocumentParamArray = new FoDocumentParam[foDocumentParamDOArray.length];
167: for (int i = 0; i < foDocumentParamDOArray.length; i++) {
168: foDocumentParamArray[i] = new FoDocumentParamImpl(
169: foDocumentParamDOArray[i]);
170: }
171:
172: return foDocumentParamArray;
173: }
174:
175: /**
176: * @param id
177: * @return
178: * @throws BaseException
179: */
180: private FoDocumentDO getFoDocumentDO(String id)
181: throws BaseException {
182: if (null != getLogger()) {
183: getLogger().log(Log.DEBUG,
184: "Getting the foDocuments from database!");
185: }
186:
187: FoDocumentDO foDocumentDO = FoDocumentDO.createExisting(id);
188:
189: if (foDocumentDO == null) {
190: throw new DatabaseException("FoDocument not found (ID) - "
191: + id);
192: }
193:
194: return foDocumentDO;
195: }
196: }
|