001: /*
002: * SalomeTMF is a Test Management Framework
003: * Copyright (C) 2005 France Telecom R&D
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: *
019: * @author Marche Mikael
020: *
021: * Contact: mikael.marche@rd.francetelecom.com
022: */
023:
024: package salomeTMF_plug.requirements.data;
025:
026: import java.util.Hashtable;
027: import java.util.Vector;
028:
029: import org.objectweb.salome_tmf.api.Util;
030:
031: import salomeTMF_plug.requirements.ReqPlugin;
032: import salomeTMF_plug.requirements.ihm.RequirementTree;
033: import salomeTMF_plug.requirements.sqlWrapper.ISQLRequirement;
034: import salomeTMF_plug.requirements.sqlWrapper.ReqWrapper;
035: import salomeTMF_plug.requirements.sqlWrapper.SQLWrapper;
036:
037: public class DataLoader {
038:
039: static boolean recurMode = true;
040:
041: static public Requirement loadData(RequirementTree reqTree)
042: throws Exception {
043: int transNumber = -1;
044: Requirement rootReq;
045: Hashtable<Integer, ReqWrapper> reqProjet = new Hashtable<Integer, ReqWrapper>();
046: try {
047: transNumber = SQLWrapper.beginTransaction();
048: Hashtable<Integer, Requirement> reqInserted = new Hashtable<Integer, Requirement>();
049: rootReq = reqTree.getRootRequirement();
050: rootReq.deleteInModel();
051: if (rootReq == null) {
052: throw new Exception(
053: "[DataLoader->loadData] no parent in requirement tree");
054: }
055: reqInserted.put(0, rootReq);
056: ISQLRequirement pSQLRequirement = SQLWrapper
057: .getSQLRequirement();
058: //Vector allReq = new Vector();
059: ReqWrapper[] tmpArray = pSQLRequirement
060: .getProjectRequirements(ReqPlugin.getProjectRef()
061: .getIdBdd());
062: for (int tmpI = 0; tmpI < tmpArray.length; tmpI++) {
063: ReqWrapper pReqWrapper = tmpArray[tmpI];
064: reqProjet.put(pReqWrapper.getIdBDD(), pReqWrapper);
065: }
066:
067: if (recurMode) {
068: for (int i = 0; i < tmpArray.length; i++) {
069: insertRequimentFunc(reqTree, tmpArray[i],
070: reqInserted, reqProjet);
071: }
072: } else {
073: int previousSize = tmpArray.length;
074: boolean continu = true;
075: Vector<ReqWrapper> resteAInserer = new Vector<ReqWrapper>();
076: while (continu) {
077: resteAInserer.clear();
078: for (int i = 0; i < tmpArray.length; i++) {
079: boolean inserted = insertRequimentIT(reqTree,
080: tmpArray[i], reqInserted, reqProjet);
081: if (!inserted) {
082: resteAInserer.add(tmpArray[i]);
083: }
084: }
085: int size = resteAInserer.size();
086: Util
087: .log("[DataLoader->insertRequiment] Requirments not insterd number "
088: + size);
089: Util
090: .log("[DataLoader->insertRequiment] Nb Req to insert"
091: + tmpArray.length);
092: Util
093: .log("[DataLoader->insertRequiment] Nb Req inserted"
094: + reqInserted.size());
095: Util
096: .log("[DataLoader->insertRequiment] Nb Req in Projet"
097: + reqProjet.size());
098: if (size == previousSize) {
099: Util
100: .log("[DataLoader->insertRequiment] Data are corrupted for requirments ");
101: continu = false;
102: } else {
103: if (size > 0) {
104: tmpArray = new ReqWrapper[size];
105: for (int j = 0; j < size; j++) {
106: tmpArray[j] = resteAInserer
107: .elementAt(j);
108: }
109: } else {
110: continu = false;
111: }
112: }
113: }
114: }
115:
116: SQLWrapper.commitTrans(transNumber);
117: } catch (Exception e) {
118: SQLWrapper.rollBackTrans(transNumber);
119: throw e;
120: }
121: return rootReq;
122: // Construction de l'arbre
123: }
124:
125: static void insertRequimentFunc(RequirementTree reqTree,
126: ReqWrapper pReqWrapper,
127: Hashtable<Integer, Requirement> reqInserted,
128: Hashtable<Integer, ReqWrapper> reqProjet) throws Exception {
129: int idP = pReqWrapper.getParent_id();
130: int idR = pReqWrapper.getIdBDD();
131: if (idP == idR) {
132: Util
133: .log("[DataLoader->insertRequiment] Data are corrupted for requirments "
134: + pReqWrapper);
135: Util
136: .log("[DataLoader->insertRequiment] restore the requiement "
137: + pReqWrapper + " at root");
138: String newName = "Restore1_" + pReqWrapper.getName();
139: pReqWrapper.setName(newName);
140: pReqWrapper.setParent_id(0);
141: try {
142: ISQLRequirement pSQLRequirement = SQLWrapper
143: .getSQLRequirement();
144: pSQLRequirement.updateParent(idR, 0);
145: pSQLRequirement.updateName(idR, newName, 0, ReqPlugin
146: .getProjectRef().getIdBdd());
147: } catch (Exception e) {
148: e.printStackTrace();
149: }
150: idP = 0;
151: }
152: int type = pReqWrapper.getType();
153: if (reqInserted.get(idR) != null) {
154: /* Exigences déja insérée */
155: return;
156: }
157: Requirement pReq;
158: ReqFamily pParent = (ReqFamily) reqInserted.get(idP);
159: if (pParent != null) { // Le parent est déja insérée
160: if (type == 1) {
161: //feuille
162: pReq = new ReqLeaf(pReqWrapper, pParent);
163: } else {
164: //famille
165: pReq = new ReqFamily(pReqWrapper, pParent);
166: }
167: pParent.addRequirement(pReq);
168: reqTree.addRequirementToReqNode(pReq, pParent, false);
169: reqInserted.put(idR, pReq);
170: } else { // Sinon, il faut l'insérer avant
171: ReqWrapper pReqParentWrapper = reqProjet.get(idP);
172: if (pReqParentWrapper == null) { //Il n'y a pas de parent dans la base de données !!!!!!
173: /* Les données sont corompues
174: * mais on insert quand meme a la racine
175: * throw new Exception ("[DataLoader->loadData] no parent in requirement tree");
176: */
177: Util
178: .log("[DataLoader->insertRequiment] Data are corrupted for requirments "
179: + pReqWrapper);
180: pReqWrapper.setParent_id(0);
181: pReqWrapper
182: .setName("Restore2_" + pReqWrapper.getName());
183:
184: } else {
185: insertRequimentFunc(reqTree, pReqParentWrapper,
186: reqInserted, reqProjet);
187:
188: }
189: insertRequimentFunc(reqTree, pReqWrapper, reqInserted,
190: reqProjet);
191: }
192: }
193:
194: static boolean insertRequimentIT(RequirementTree reqTree,
195: ReqWrapper pReqWrapper,
196: Hashtable<Integer, Requirement> reqInserted,
197: Hashtable<Integer, ReqWrapper> reqProjet) throws Exception {
198: int idP = pReqWrapper.getParent_id();
199: int idR = pReqWrapper.getIdBDD();
200: if (idP == idR) {
201: Util
202: .log("[DataLoader->insertRequiment] Data are corrupted for requirments "
203: + pReqWrapper);
204: Util
205: .log("[DataLoader->insertRequiment] restore the requiement "
206: + pReqWrapper + " at root");
207: String newName = "Restore1_" + pReqWrapper.getName();
208: pReqWrapper.setName(newName);
209: pReqWrapper.setParent_id(0);
210: try {
211: ISQLRequirement pSQLRequirement = SQLWrapper
212: .getSQLRequirement();
213: pSQLRequirement.updateParent(idR, 0);
214: pSQLRequirement.updateName(idR, newName, 0, ReqPlugin
215: .getProjectRef().getIdBdd());
216: } catch (Exception e) {
217: e.printStackTrace();
218: }
219: idP = 0;
220: }
221: int type = pReqWrapper.getType();
222: if (reqInserted.get(idR) != null) {
223: /* Exigences déja insérée */
224: return true;
225: }
226: Requirement pReq;
227: ReqFamily pParent = (ReqFamily) reqInserted.get(idP);
228: if (pParent != null) { // Le parent est déja insérée
229: if (type == 1) {
230: //feuille
231: pReq = new ReqLeaf(pReqWrapper, pParent);
232: } else {
233: //famille
234: pReq = new ReqFamily(pReqWrapper, pParent);
235: }
236: pParent.addRequirement(pReq);
237: reqTree.addRequirementToReqNode(pReq, pParent, false);
238: reqInserted.put(idR, pReq);
239: return true;
240: } else { // Sinon, il faut l'insérer avant
241: ReqWrapper pReqParentWrapper = reqProjet.get(idP);
242: if (pReqParentWrapper == null) { //Il n'y a pas de parent dans la base de données !!!!!!
243: /* Les données sont corompues
244: * mais on insert quand meme a la racine
245: * throw new Exception ("[DataLoader->loadData] no parent in requirement tree");
246: */
247: Util
248: .log("[DataLoader->insertRequiment] Data are corrupted for requirments "
249: + pReqWrapper);
250: pReqWrapper.setParent_id(0);
251: pReqWrapper
252: .setName("Restore2_" + pReqWrapper.getName());
253: return insertRequimentIT(reqTree, pReqWrapper,
254: reqInserted, reqProjet);
255: } else {
256: return false;
257: //insertRequiment(reqTree, pReqParentWrapper , reqInserted, reqProjet );
258: }
259: }
260: }
261:
262: }
|