Source Code Cross Referenced for AssetDBComponent.java in  » Science » Cougaar12_4 » org » cougaar » tools » csmart » society » db » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Science » Cougaar12_4 » org.cougaar.tools.csmart.society.db 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * <copyright>
003:         *  
004:         *  Copyright 2000-2004 BBNT Solutions, LLC
005:         *  under sponsorship of the Defense Advanced Research Projects
006:         *  Agency (DARPA).
007:         * 
008:         *  You can redistribute this software and/or modify it under the
009:         *  terms of the Cougaar Open Source License as published on the
010:         *  Cougaar Open Source Website (www.cougaar.org).
011:         * 
012:         *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
013:         *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
014:         *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
015:         *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
016:         *  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
017:         *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
018:         *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
019:         *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
020:         *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
021:         *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
022:         *  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
023:         *  
024:         * </copyright>
025:         */
026:        package org.cougaar.tools.csmart.society.db;
027:
028:        import org.cougaar.tools.csmart.core.cdata.AgentAssetData;
029:        import org.cougaar.tools.csmart.core.cdata.AgentComponentData;
030:        import org.cougaar.tools.csmart.core.cdata.ComponentData;
031:        import org.cougaar.tools.csmart.core.cdata.PGPropData;
032:        import org.cougaar.tools.csmart.core.cdata.PGPropMultiVal;
033:        import org.cougaar.tools.csmart.core.cdata.PropGroupData;
034:        import org.cougaar.tools.csmart.core.cdata.RelationshipData;
035:        import org.cougaar.tools.csmart.core.db.DBUtils;
036:        import org.cougaar.tools.csmart.core.property.ModifiableConfigurableComponent;
037:        import org.cougaar.tools.csmart.core.property.Property;
038:        import org.cougaar.tools.csmart.society.AssetComponent;
039:        import org.cougaar.tools.csmart.society.ContainerBase;
040:        import org.cougaar.tools.csmart.society.PropGroupBase;
041:        import org.cougaar.tools.csmart.society.PropGroupComponent;
042:        import org.cougaar.tools.csmart.society.RelationshipBase;
043:        import org.cougaar.tools.csmart.ui.viewer.CSMART;
044:        import org.cougaar.util.TimeSpan;
045:
046:        import java.io.Serializable;
047:        import java.sql.Connection;
048:        import java.sql.ResultSet;
049:        import java.sql.Statement;
050:        import java.sql.Timestamp;
051:        import java.text.DateFormat;
052:        import java.text.ParseException;
053:        import java.util.ArrayList;
054:        import java.util.Collection;
055:        import java.util.Date;
056:        import java.util.HashMap;
057:        import java.util.Hashtable;
058:        import java.util.Iterator;
059:        import java.util.Map;
060:
061:        /**
062:         * An AssetData loaded from the database
063:         **/
064:        public class AssetDBComponent extends ModifiableConfigurableComponent
065:                implements  AssetComponent {
066:
067:            // query names
068:            private static final String QUERY_AGENT_ASSET_CLASS = "queryAgentAssetClass";
069:            private static final String QUERY_AGENT_RELATIONS = "queryAgentRelationships";
070:            private static final String QUERY_PG_ID = "queryPGId";
071:            private static final String QUERY_PG_ATTRS = "queryPGAttrs";
072:            private static final String QUERY_PG_VALUES = "queryPGValues";
073:
074:            /** Property Definitions **/
075:            //  public static final String PROP_TYPE = "Asset Type";
076:            //  public static final String PROP_TYPE_DESC = "Type of Asset";
077:            //    public static final String PROP_CLASS = "Asset Class";
078:            //    public static final String PROP_CLASS_DESC = "Class of the Asset";
079:            //    public static final String PROP_UID = "UID";
080:            //    public static final String PROP_UID_DESC = "UID of the Asset";
081:            //    public static final String PROP_UNITNAME = "Unit Name";
082:            //    public static final String PROP_UNITNAME_DESC = "Unit Name of the Asset";
083:            //    public static final String PROP_UIC = "UIC";
084:            //    public static final String PROP_UIC_DESC = "UIC of the Asset";
085:            private Map substitutions = new HashMap();
086:            //  private Property propAssetType;
087:            private Property propAssetClass;
088:            private Property propUniqueID;
089:            private Property propUnitName;
090:            private Property propUIC;
091:            private String agentName;
092:            private String assemblyID;
093:            private String assemblyMatch;
094:            private Hashtable pgAttributes = new Hashtable();
095:            private Hashtable propertyGroups = new Hashtable();
096:
097:            public AssetDBComponent(String agentName, String assemblyID) {
098:                super ("AssetData");
099:                this .agentName = agentName;
100:                this .assemblyID = assemblyID;
101:            }
102:
103:            public void initProperties() {
104:                //    propAssetType = addProperty(PROP_TYPE, new Integer(AgentAssetData.ORG));
105:                //    propAssetType.setToolTip(PROP_TYPE_DESC);
106:
107:                propAssetClass = addProperty(PROP_CLASS, queryOrgClass());
108:                propAssetClass.setToolTip(PROP_CLASS_DESC);
109:
110:                // TODO: where do you get the UID?
111:                propUniqueID = addProperty(PROP_UID, "");
112:                propUniqueID.setToolTip(PROP_UID_DESC);
113:
114:                propUnitName = addProperty(PROP_UNITNAME, "");
115:                propUnitName.setToolTip(PROP_UNITNAME_DESC);
116:
117:                propUIC = addProperty(PROP_UIC, "UIC/" + agentName);
118:                propUIC.setToolTip(PROP_UIC_DESC);
119:
120:                getAssemblies(); // for use in database lookups
121:                addPropGroups(); // add property groups from the database
122:                addRelationships(getRelationshipData()); // add relationships from database
123:            }
124:
125:            /**
126:             * Add component data for asset properties, relationships,
127:             * and property groups.
128:             * TODO: same as AssetFileComponent: should this be moved to a base class?
129:             */
130:
131:            public ComponentData addComponentData(ComponentData data) {
132:                AgentAssetData assetData = new AgentAssetData(
133:                        (AgentComponentData) data);
134:                //    assetData.setType(((Integer)propAssetType.getValue()).intValue());
135:                assetData.setAssetClass((String) propAssetClass.getValue());
136:                assetData.setUniqueID((String) propUniqueID.getValue());
137:                assetData.setUnitName((String) propUnitName.getValue());
138:                assetData.setUIC((String) propUIC.getValue());
139:
140:                // Add Relationships.
141:                Iterator iter = ((Collection) getDescendentsOfClass(ContainerBase.class))
142:                        .iterator();
143:                while (iter.hasNext()) {
144:                    ContainerBase container = (ContainerBase) iter.next();
145:                    if (container.getShortName().equals("Relationships")) {
146:                        for (int i = 0; i < container.getChildCount(); i++) {
147:                            RelationshipBase rel = (RelationshipBase) container
148:                                    .getChild(i);
149:                            RelationshipData rData = new RelationshipData();
150:                            if (rel == null) {
151:                                if (log.isErrorEnabled()) {
152:                                    log
153:                                            .error(
154:                                                    "Please report seeing Bug 1304: Using CSMART "
155:                                                            + CSMART
156:                                                                    .writeDebug()
157:                                                            + " had Null RelationshipBase at child spot # "
158:                                                            + i
159:                                                            + " out of "
160:                                                            + container
161:                                                                    .getChildCount()
162:                                                            + " for Agent "
163:                                                            + agentName
164:                                                            + " in assembly "
165:                                                            + assemblyID,
166:                                                    new Throwable());
167:                                }
168:                            } else if (rel
169:                                    .getProperty(RelationshipBase.PROP_TYPE) == null) {
170:                                if (log.isErrorEnabled()) {
171:                                    log
172:                                            .error(
173:                                                    "Please report seeing Bug 1304: Using CSMART "
174:                                                            + CSMART
175:                                                                    .writeDebug()
176:                                                            + " had Null Relationship type for relationship at child spot #"
177:                                                            + i
178:                                                            + " out of "
179:                                                            + container
180:                                                                    .getChildCount()
181:                                                            + " for Agent "
182:                                                            + agentName
183:                                                            + " in assembly "
184:                                                            + assemblyID
185:                                                            + " where relationship role="
186:                                                            + rel
187:                                                                    .getProperty(RelationshipBase.PROP_ROLE)
188:                                                            + ", and ItemID="
189:                                                            + rel
190:                                                                    .getProperty(RelationshipBase.PROP_ITEM)
191:                                                            + ", and supported="
192:                                                            + rel
193:                                                                    .getProperty(RelationshipBase.PROP_SUPPORTED),
194:                                                    new Throwable());
195:                                }
196:                            } else {
197:                                // Bug 1304 is an NPE in the next line
198:                                rData.setType((String) rel.getProperty(
199:                                        RelationshipBase.PROP_TYPE).getValue());
200:                            }
201:                            rData.setRole((String) rel.getProperty(
202:                                    RelationshipBase.PROP_ROLE).getValue());
203:                            rData.setItemId((String) rel.getProperty(
204:                                    RelationshipBase.PROP_ITEM).getValue());
205:                            // AMH: This line was missing? 7/2/02
206:                            rData.setTypeId((String) rel.getProperty(
207:                                    RelationshipBase.PROP_TYPEID).getValue());
208:                            rData
209:                                    .setSupported((String) rel.getProperty(
210:                                            RelationshipBase.PROP_SUPPORTED)
211:                                            .getValue());
212:
213:                            DateFormat df = DateFormat.getInstance();
214:                            try {
215:                                Date start = df.parse((String) rel.getProperty(
216:                                        RelationshipBase.PROP_STARTTIME)
217:                                        .getValue());
218:                                Date end = df.parse((String) rel.getProperty(
219:                                        RelationshipBase.PROP_STOPTIME)
220:                                        .getValue());
221:                                rData.setStartTime(start.getTime());
222:                                rData.setEndTime(end.getTime());
223:                            } catch (ParseException pe) {
224:                                if (log.isErrorEnabled()) {
225:                                    log
226:                                            .error(
227:                                                    "Caught Exception parsing Date, using default dates.",
228:                                                    pe);
229:                                }
230:                                rData.setStartTime(TimeSpan.MIN_VALUE);
231:                                rData.setEndTime(TimeSpan.MAX_VALUE);
232:                            }
233:
234:                            assetData.addRelationship(rData);
235:                        }
236:                    }
237:                }
238:
239:                // Add Property Groups.
240:                iter = ((Collection) getDescendentsOfClass(ContainerBase.class))
241:                        .iterator();
242:                while (iter.hasNext()) {
243:                    ContainerBase container = (ContainerBase) iter.next();
244:                    if (container.getShortName().equals("Property Groups")) {
245:                        for (int i = 0; i < container.getChildCount(); i++) {
246:                            PropGroupBase pg = (PropGroupBase) container
247:                                    .getChild(i);
248:                            assetData.addPropertyGroup(pg.getPropGroupData());
249:                        }
250:                    }
251:                }
252:
253:                data.addAgentAssetData(assetData);
254:                return data;
255:            }
256:
257:            private void getAssemblies() {
258:                // Get list of assemblies for use in query, ignoring CMT assemblies
259:                // Why ignore CMT? Makes no sense...
260:                //    assemblyMatch = DBUtils.getListMatch(assemblyID, "CMT");
261:                assemblyMatch = DBUtils.getListMatch(assemblyID);
262:
263:                if (assemblyMatch != null) {
264:                    substitutions.put(":assemblyMatch", assemblyMatch);
265:                    substitutions.put(":agent_name", agentName);
266:                }
267:            }
268:
269:            private RelationshipData[] getRelationshipData() {
270:                // creates relationships by creating RelationshipData
271:                // from the database and passing that as an argument
272:                // to RelationshipBase
273:                ArrayList relationshipData = new ArrayList();
274:                try {
275:                    Connection conn = DBUtils.getConnection();
276:                    try {
277:                        Statement stmt = conn.createStatement();
278:                        String query = DBUtils.getQuery(QUERY_AGENT_RELATIONS,
279:                                substitutions);
280:                        ResultSet rs = stmt.executeQuery(query);
281:                        while (rs.next()) {
282:                            RelationshipData rd = new RelationshipData();
283:                            // Get the Component ALIB ID being supported
284:                            String supported = rs.getString(1);
285:
286:                            // Using that, look up the AgentID, TypeID, ItemID
287:                            // But default back to the ALIB_ID if necessary
288:                            // FIXME: Look up these PG IDs
289:                            String aID = getPGVal("ClusterPG|MessageAddress",
290:                                    supported);
291:
292:                            // HACK for the 10.0 transition from ClusterIdentifier->MA
293:                            if (aID == null)
294:                                aID = getPGVal("ClusterPG|ClusterIdentifier",
295:                                        supported);
296:
297:                            if (aID == null || aID.equals(""))
298:                                aID = supported;
299:
300:                            String typeID = getPGVal(
301:                                    "TypeIdentificationPG|TypeIdentification",
302:                                    supported);
303:                            if (typeID == null || typeID.equals(""))
304:                                typeID = supported;
305:
306:                            String itemID = getPGVal(
307:                                    "ItemIdentificationPG|ItemIdentification",
308:                                    supported);
309:                            if (itemID == null || itemID.equals(""))
310:                                itemID = supported;
311:
312:                            rd.setItemId(itemID);
313:                            rd.setSupported(aID);
314:                            rd.setTypeId(typeID);
315:
316:                            String role = rs.getString(2);
317:                            Timestamp startDate = rs.getTimestamp(3);
318:                            Timestamp endDate = rs.getTimestamp(4);
319:                            if (role.equals("Subordinate")) {
320:                                rd.setRole("");
321:                                rd.setType("Subordinate");
322:                            } else {
323:                                rd.setRole(role);
324:                                rd.setType("Supporting");
325:                            }
326:                            if (startDate != null) {
327:                                rd.setStartTime(startDate.getTime());
328:                            }
329:                            if (endDate != null)
330:                                rd.setEndTime(endDate.getTime());
331:                            relationshipData.add(rd);
332:                            if (log.isDebugEnabled()) {
333:                                log.debug(this  + " doing query " + query
334:                                        + " and created Relationship: " + rd);
335:                            }
336:                            if (rd.getType() == null && log.isErrorEnabled()) {
337:                                log
338:                                        .error(
339:                                                "Please report bug 1304: using CSMART "
340:                                                        + CSMART.writeDebug()
341:                                                        + " just created a RelationshipData from the DB and type is now null: "
342:                                                        + rd, new Throwable());
343:                            }
344:                        }
345:                        rs.close();
346:                        stmt.close();
347:                    } finally {
348:                        conn.close();
349:                    }
350:                } catch (Exception e) {
351:                    if (log.isErrorEnabled()) {
352:                        log.error("Exception", e);
353:                    }
354:                    throw new RuntimeException("Error" + e);
355:                }
356:                return (RelationshipData[]) relationshipData
357:                        .toArray(new RelationshipData[relationshipData.size()]);
358:            }
359:
360:            // Given a PG_ATTRIBUTE_LIB_ID and a COMPONENT_ALIB_ID, 
361:            // look up the first value given
362:            private String getPGVal(String pgID, String agent) {
363:                if (agent == null || agent.equals("") || pgID == null
364:                        || pgID.equals(""))
365:                    return null;
366:
367:                // This hashmap gets re-used so dont kill the agent_name
368:                Object old = substitutions.get(":agent_name");
369:
370:                substitutions.put(":agent_name", agent);
371:                substitutions.put(":pgAttrLibId", pgID);
372:                String result = null;
373:                try {
374:                    Connection conn2 = DBUtils.getConnection();
375:                    try {
376:                        Statement stmt2 = conn2.createStatement();
377:                        String query2 = DBUtils.getQuery(QUERY_PG_VALUES,
378:                                substitutions);
379:                        ResultSet rs2 = stmt2.executeQuery(query2);
380:                        if (rs2.next()) {
381:                            result = rs2.getString(1);
382:                        }
383:                        rs2.close();
384:                        stmt2.close();
385:                    } finally {
386:                        conn2.close();
387:                    }
388:                } catch (Exception e) {
389:                    if (log.isErrorEnabled()) {
390:                        log.error("Exception getting PGVal for agent " + agent
391:                                + " and PG " + pgID, e);
392:                    }
393:                }
394:
395:                substitutions.put(":agent_name", old);
396:
397:                if (log.isDebugEnabled()) {
398:                    log.debug("getPGVal returning " + result + " for agent "
399:                            + agent + " and pgID " + pgID);
400:                }
401:                return result;
402:            }
403:
404:            private void addRelationships(RelationshipData[] rel) {
405:                ContainerBase relContainer = new ContainerBase("Relationships");
406:                relContainer.initProperties();
407:                addChild(relContainer);
408:                for (int i = 0; i < rel.length; i++) {
409:                    RelationshipBase newR = new RelationshipBase(rel[i]);
410:                    newR.initProperties();
411:                    if (newR.getProperty(RelationshipBase.PROP_TYPE) == null
412:                            && log.isErrorEnabled()) {
413:                        log
414:                                .error(
415:                                        "Please report bug 1304: using CSMART"
416:                                                + CSMART.writeDebug()
417:                                                + " just created a RelationshipBase with null PROP_TYPE ("
418:                                                + newR
419:                                                + ") built from RelationshipData: "
420:                                                + rel[i] + " at child spot# "
421:                                                + i, new Throwable());
422:                    }
423:                    relContainer.addChild(newR);
424:                }
425:                if (log.isDebugEnabled()) {
426:                    for (int i = 0; i < relContainer.getChildCount(); i++) {
427:                        RelationshipBase relt = (RelationshipBase) relContainer
428:                                .getChild(i);
429:                        if (relt == null) {
430:                            if (log.isErrorEnabled()) {
431:                                log
432:                                        .error(
433:                                                "Please report seeing Bug 1304: Using CSMART "
434:                                                        + CSMART.writeDebug()
435:                                                        + " had Null RelationshipBase at child spot # "
436:                                                        + i
437:                                                        + " out of "
438:                                                        + relContainer
439:                                                                .getChildCount()
440:                                                        + " where the corresponding RelationshipData should have been: "
441:                                                        + rel[i],
442:                                                new Throwable());
443:                            }
444:                        } else if (relt.getProperty(RelationshipBase.PROP_TYPE) == null) {
445:                            if (log.isErrorEnabled()) {
446:                                log
447:                                        .error(
448:                                                "Please report seeing Bug 1304: Using CSMART "
449:                                                        + CSMART.writeDebug()
450:                                                        + " had Null Relationship type for relationship at child spot #"
451:                                                        + i
452:                                                        + " out of "
453:                                                        + relContainer
454:                                                                .getChildCount()
455:                                                        + " where relationship role="
456:                                                        + relt
457:                                                                .getProperty(RelationshipBase.PROP_ROLE)
458:                                                        + ", and ItemID="
459:                                                        + relt
460:                                                                .getProperty(RelationshipBase.PROP_ITEM)
461:                                                        + ", and supported="
462:                                                        + relt
463:                                                                .getProperty(RelationshipBase.PROP_SUPPORTED)
464:                                                        + ", where the corresponding RelationshipData should have been "
465:                                                        + rel[i],
466:                                                new Throwable());
467:                            }
468:                        }
469:                    } // for loop
470:                } // if debug
471:            }
472:
473:            private void addPropGroups() {
474:                String pgName = "";
475:                String pgAttrName = "";
476:                String pgAttrType = "";
477:                String pgValue = "";
478:                String pgAggregateType = "";
479:                Timestamp pgStartDate;
480:                Timestamp pgEndDate;
481:                try {
482:                    Connection conn = DBUtils.getConnection();
483:                    try {
484:                        Statement stmt = conn.createStatement();
485:                        // get the propery group attribute lib ids for this agent
486:                        ArrayList pgAttrLibIds = new ArrayList();
487:                        String query = DBUtils.getQuery(QUERY_PG_ID,
488:                                substitutions);
489:                        ResultSet rs = stmt.executeQuery(query);
490:                        while (rs.next()) {
491:                            String res = rs.getString(1);
492:                            if (res == null || res.equals("")) {
493:                                if (log.isWarnEnabled()) {
494:                                    log.warn("addPropGroups for " + agentName
495:                                            + " got empty PG ID using subs: "
496:                                            + substitutions);
497:                                }
498:                                continue;
499:                            }
500:                            pgAttrLibIds.add(res);
501:                        }
502:
503:                        // get pg name and attribute names and types for each property group
504:                        for (int i = 0; i < pgAttrLibIds.size(); i++) {
505:                            substitutions.put(":pgAttrLibId",
506:                                    (String) pgAttrLibIds.get(i));
507:                            query = DBUtils.getQuery(QUERY_PG_ATTRS,
508:                                    substitutions);
509:                            rs = stmt.executeQuery(query);
510:                            while (rs.next()) {
511:                                pgName = rs.getString(1);
512:                                pgAttrName = rs.getString(2);
513:                                pgAttrType = rs.getString(3);
514:                                pgAggregateType = rs.getString(4);
515:
516:                                // HACK: Updating DB on fly, replacing CI with MA
517:                                if (pgAttrName.equals("ClusterIdentifier")) {
518:                                    // Create the single instance of the correct MA version
519:                                    // Have I already done so?
520:                                    PGAttr mapg = (PGAttr) pgAttributes
521:                                            .get("ClusterPG|MessageAddress");
522:                                    if (mapg == null) {
523:                                        pgAttributes.put(
524:                                                "ClusterPG|MessageAddress",
525:                                                new PGAttr("ClusterPG",
526:                                                        "MessageAddress",
527:                                                        "MessageAddress",
528:                                                        "SINGLE"));
529:                                    }
530:                                }
531:
532:                                // AMH: Moved this put inside the while loop,
533:                                // otherwise apparently only the last value get stored
534:                                pgAttributes.put((String) pgAttrLibIds.get(i),
535:                                        new PGAttr(pgName, pgAttrName,
536:                                                pgAttrType, pgAggregateType));
537:                            }
538:                        } // loop over pg_attr_lib_ids to get lib definition
539:
540:                        // get values for each attribute in each property group
541:                        PGPropMultiVal multiValueData = null;
542:                        for (int i = 0; i < pgAttrLibIds.size(); i++) {
543:                            String pgAttrLibId = (String) pgAttrLibIds.get(i);
544:                            substitutions.put(":pgAttrLibId", pgAttrLibId);
545:                            PGAttr pgAttr = (PGAttr) pgAttributes
546:                                    .get(pgAttrLibId);
547:                            // FIXME: Why is attrType unused here? ATTRIBUTE_TYPE col from lib_pg_attr
548:                            // It is String, MessageAddress, etc
549:                            //          String attrType = pgAttr.getAttrType();
550:                            String aggregateType = "SINGLE";
551:                            if (pgAttr == null) {
552:                                if (log.isWarnEnabled()) {
553:                                    log.warn("addPropGroups for " + agentName
554:                                            + " got null PGAttr for ID "
555:                                            + pgAttrLibId + " using subs: "
556:                                            + substitutions);
557:                                }
558:                            } else {
559:                                aggregateType = pgAttr.getAggregateType();
560:                            }
561:                            query = DBUtils.getQuery(QUERY_PG_VALUES,
562:                                    substitutions);
563:                            rs = stmt.executeQuery(query);
564:                            while (rs.next()) {
565:                                if (aggregateType.equals("SINGLE")) {
566:                                    pgValue = rs.getString(1);
567:                                    pgStartDate = rs.getTimestamp(2);
568:                                    pgEndDate = rs.getTimestamp(3);
569:                                    addPGAttributeData(pgAttrLibId, pgValue,
570:                                            pgStartDate, pgEndDate);
571:                                } else {
572:                                    pgValue = rs.getString(1);
573:                                    pgStartDate = rs.getTimestamp(2);
574:                                    pgEndDate = rs.getTimestamp(3);
575:                                    if (multiValueData == null)
576:                                        multiValueData = new PGPropMultiVal();
577:                                    multiValueData.addValue(pgValue);
578:                                }
579:                            }
580:                            if (multiValueData != null) {
581:                                addPGAttributeData(pgAttrLibId, multiValueData,
582:                                        null, null);
583:                                multiValueData = null;
584:                            }
585:                        } // loop over PG Attr Lib Ids (PG property / slots)
586:                        rs.close();
587:                        stmt.close();
588:                    } finally {
589:                        conn.close();
590:                    }
591:                } catch (Exception e) {
592:                    if (log.isErrorEnabled()) {
593:                        log.error("Exception adding PropGroups for "
594:                                + agentName + ": ", e);
595:                    }
596:                    throw new RuntimeException("Error adding PropGroups for "
597:                            + agentName + ": " + e);
598:                }
599:                // after creating all the property group data
600:                // create the property groups, initialize them and add them as children
601:                ContainerBase pgContainer = new ContainerBase("Property Groups");
602:                pgContainer.initProperties();
603:                addChild(pgContainer);
604:                Iterator propGroupData = propertyGroups.values().iterator();
605:                while (propGroupData.hasNext()) {
606:                    PropGroupData pgd = (PropGroupData) propGroupData.next();
607:                    PropGroupComponent newPG = new PropGroupBase(pgd);
608:                    newPG.initProperties();
609:                    pgContainer.addChild(newPG);
610:                }
611:            }
612:
613:            /**
614:             * Creates PropGroupData and PGPropData
615:             * for each property group and attribute respectively.
616:             * TODO: save start and end dates?
617:             */
618:            private void addPGAttributeData(String attrLibId, Object value,
619:                    Timestamp startDate, Timestamp endDate) {
620:                // HACK: 10.0 change of CI for MA
621:                if (attrLibId.equals("ClusterPG|ClusterIdentifier")) {
622:                    attrLibId = "ClusterPG|MessageAddress";
623:                    if (log.isWarnEnabled()) {
624:                        log
625:                                .warn("ClusterIdentifier obsolete. Replacing with MessageAddress.");
626:                    }
627:                }
628:
629:                PGAttr pgAttr = (PGAttr) pgAttributes.get(attrLibId);
630:                PropGroupData pgd = (PropGroupData) propertyGroups.get(pgAttr
631:                        .getName());
632:                if (pgd == null) {
633:                    pgd = new PropGroupData(pgAttr.getName());
634:                    propertyGroups.put(pgAttr.getName(), pgd);
635:                }
636:                PGPropData pgPropData = new PGPropData();
637:
638:                pgPropData.setName(pgAttr.getAttrName());
639:                String aggregateType = pgAttr.getAggregateType();
640:                if (aggregateType.equals("SINGLE"))
641:                    pgPropData.setType(pgAttr.getAttrType());
642:                else
643:                    pgPropData.setType(aggregateType, pgAttr.getAttrType());
644:                pgPropData.setValue(value);
645:                pgd.addProperty(pgPropData);
646:            }
647:
648:            // Get the Asset class (ie MilitaryOrganization, which is the default)
649:            private String queryOrgClass() {
650:                String orgClass = null;
651:                substitutions.put(":agent_name", agentName);
652:                try {
653:                    Connection conn = DBUtils.getConnection();
654:                    try {
655:                        Statement stmt = conn.createStatement();
656:                        String query = DBUtils.getQuery(
657:                                QUERY_AGENT_ASSET_CLASS, substitutions);
658:                        ResultSet rs = stmt.executeQuery(query);
659:                        if (rs.next()) {
660:                            orgClass = rs.getString(1);
661:                        }
662:                        rs.close();
663:                        stmt.close();
664:
665:                    } finally {
666:                        conn.close();
667:                    }
668:                } catch (Exception e) {
669:                    if (log.isErrorEnabled()) {
670:                        log
671:                                .error(
672:                                        "queryOrgClass: exception getting asset class: ",
673:                                        e);
674:                    }
675:                }
676:                if (orgClass != null)
677:                    return orgClass;
678:                else {
679:                    if (log.isWarnEnabled())
680:                        log.warn("Found no asset class for " + agentName
681:                                + ". Default is MilitaryOrganization");
682:                    return "MilitaryOrganization";
683:                }
684:            }
685:
686:            /**
687:             * Information on a property group attribute.
688:             */
689:
690:            class PGAttr implements  Serializable {
691:                String name;
692:                String attrName;
693:                String attrType;
694:                String aggregateType;
695:
696:                public PGAttr(String name, String attrName, String attrType,
697:                        String aggregateType) {
698:                    this .name = name;
699:                    this .attrName = attrName;
700:                    this .attrType = attrType;
701:                    this .aggregateType = aggregateType;
702:                }
703:
704:                public String getName() {
705:                    return name;
706:                }
707:
708:                public String getAttrName() {
709:                    return attrName;
710:                }
711:
712:                public String getAttrType() {
713:                    return attrType;
714:                }
715:
716:                public String getAggregateType() {
717:                    return aggregateType;
718:                }
719:            }
720:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.