--- /dev/null
+/*****************************************************************************
+ * Copyright Igor Barma, Eric Brun, Alexandre Desoubeaux, Christian Martel,
+ * (2 décembre 2008)
+ *
+ * Ce logiciel est un programme informatique servant à l'évaluation des
+ * compétences.
+ *
+ * Ce logiciel est régi par la licence CeCILL soumise au droit français et
+ * respectant les principes de diffusion des logiciels libres. Vous pouvez
+ * utiliser, modifier et/ou redistribuer ce programme sous les conditions
+ * de la licence CeCILL telle que diffusée par le CEA, le CNRS et l'INRIA
+ * sur le site "http://www.cecill.info".
+ *
+ * En contrepartie de l'accessibilité au code source et des droits de copie,
+ * de modification et de redistribution accordés par cette licence, il n'est
+ * offert aux utilisateurs qu'une garantie limitée. Pour les mêmes raisons,
+ * seule une responsabilité restreinte pèse sur l'auteur du programme, le
+ * titulaire des droits patrimoniaux et les concédants successifs.
+ *
+ * A cet égard l'attention de l'utilisateur est attirée sur les risques
+ * associés au chargement, à l'utilisation, à la modification et/ou au
+ * développement et à la reproduction du logiciel par l'utilisateur étant
+ * donné sa spécificité de logiciel libre, qui peut le rendre complexe à
+ * manipuler et qui le réserve donc à des développeurs et des professionnels
+ * avertis possédant des connaissances informatiques approfondies. Les
+ * utilisateurs sont donc invités à charger et tester l'adéquation du
+ * logiciel à leurs besoins dans des conditions permettant d'assurer la
+ * sécurité de leurs systèmes et ou de leurs données et, plus généralement,
+ * à l'utiliser et l'exploiter dans les mêmes conditions de sécurité.
+ *
+ * Le fait que vous puissiez accéder à cet en-tête signifie que vous avez
+ * pris connaissance de la licence CeCILL, et que vous en avez accepté les
+ * termes.
+ *******************************************************************************/
+package com.pentila.evalcomp.manager;
+
+import java.io.BufferedInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.sql.SQLException;
+import java.util.HashSet;
+import java.util.Set;
+
+import com.pentila.evalcomp.dao.IDataStoreDAO;
+import com.pentila.evalcomp.domain.server.DataStore;
+import com.pentila.evalcomp.domain.transit.TDataStore;
+import com.pentila.evalcomp.manager.interfacedef.IDataStoreBase;
+import com.pentila.evalcomp.utilities.Utils;
+
+/**
+ * The Class DataStoreManager.
+ */
+public class DataStoreManager implements IDataStoreBase {
+
+ /** The data store dao. */
+ IDataStoreDAO dataStoreDAO;
+
+ /**
+ * Gets the data store dao.
+ *
+ * @return the data store dao
+ */
+ public IDataStoreDAO getDataStoreDAO() {
+ return dataStoreDAO;
+ }
+
+ /**
+ * Sets the data store dao.
+ *
+ * @param dataStoreDAO the new data store dao
+ */
+ public void setDataStoreDAO(IDataStoreDAO dataStoreDAO) {
+ this.dataStoreDAO = dataStoreDAO;
+ }
+
+ /* (non-Javadoc)
+ * @see com.pentila.evalcomp.manager.interfacedef.IDataStoreBase#getData(java.lang.Object, java.lang.String)
+ */
+ public byte[] getData(Object o, String filename) {
+
+ byte[] array = null;
+ DataStore ds = dataStoreDAO.getFile(o.getClass().getCanonicalName(),
+ (Long) Utils.getAttr(o, "id", null), filename);
+
+ if (ds != null) {
+ try {
+ array = ds.getData().getBytes(1, (int) ds.getData().length());
+ } catch (SQLException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ }
+
+ return array;
+
+ }
+
+ /* (non-Javadoc)
+ * @see com.pentila.evalcomp.manager.interfacedef.IDataStoreBase#putData(java.lang.Object, java.lang.String, java.io.InputStream)
+ */
+ public boolean putData(Object o, String filename, InputStream file) {
+
+ Long objectid = (Long) Utils.getAttr(o, "id", null);
+ String className = o.getClass().getCanonicalName();
+ return putData(objectid, className, filename, file);
+
+ }
+
+ /* (non-Javadoc)
+ * @see com.pentila.evalcomp.manager.interfacedef.IDataStoreBase#cleanDatas(java.lang.Object)
+ */
+ public boolean cleanDatas(Object o) {
+
+ Long objectId = (Long) Utils.getAttr(o, "id", null);
+ String className = o.getClass().getCanonicalName();
+
+ Set<DataStore> stds = dataStoreDAO.getFiles(className, objectId);
+ for (DataStore ds : stds) {
+
+
+
+
+
+ dataStoreDAO.delete(ds);
+ }
+
+ return true;
+ }
+
+ /* (non-Javadoc)
+ * @see com.pentila.evalcomp.manager.interfacedef.IDataStoreBase#getFiles(java.lang.Long, java.lang.String)
+ */
+ public Set<TDataStore> getFiles(Long objectId, String className) {
+
+ Set<TDataStore> stds = new HashSet<TDataStore>();
+
+ Set<DataStore> sds = dataStoreDAO.getFiles(className, objectId);
+
+ for (DataStore ds : sds){
+ TDataStore tds = new TDataStore();
+
+
+ tds.setId(ds.getId());
+ tds.setName(ds.getName());
+ tds.setClassName(ds.getClassName());
+
+ stds.add(tds);
+ }
+
+ return stds;
+
+ }
+
+ /* (non-Javadoc)
+ * @see com.pentila.evalcomp.manager.interfacedef.IDataStoreBase#putData(java.lang.Long, java.lang.String, java.lang.String, java.io.InputStream)
+ */
+ public boolean putData(Long objectId, String className, String filename,
+ InputStream file) {
+ try {
+
+ BufferedInputStream bis = new BufferedInputStream(file);
+ ByteArrayOutputStream buf = new ByteArrayOutputStream();
+ int result = bis.read();
+ while (result != -1) {
+ byte b = (byte) result;
+ buf.write(b);
+ result = bis.read();
+ }
+
+ dataStoreDAO.add(className, objectId, filename, buf.toByteArray());
+
+ } catch (IOException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+
+ return true;
+ }
+
+ /* (non-Javadoc)
+ * @see com.pentila.evalcomp.manager.interfacedef.IDataStoreBase#deleteFiles(java.util.Set)
+ */
+ public boolean deleteFiles(Set<Long> idsDataStore) {
+
+ for (Long id : idsDataStore) {
+
+ DataStore ds = dataStoreDAO.get(id);
+
+ dataStoreDAO.delete(ds);
+ }
+
+ return true;
+ }
+
+ /* (non-Javadoc)
+ * @see com.pentila.evalcomp.manager.interfacedef.IDataStoreBase#getFileData(java.lang.Long, java.lang.String, java.lang.String)
+ */
+ public byte[] getFileData(Long sicId, String className, String filename) {
+ byte[] array = null;
+ DataStore ds = dataStoreDAO.getFile(className, sicId, filename);
+
+ if (ds != null) {
+ try {
+ array = ds.getData().getBytes(1, (int) ds.getData().length());
+ } catch (SQLException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ }
+
+ return array;
+ }
+
+}