Source Code Cross Referenced for AnalysisActions.java in  » Web-Framework » argun » biz » hammurapi » web » analysis » 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 » Web Framework » argun » biz.hammurapi.web.analysis 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


0001:        /*
0002:         * argun 1.0
0003:         * Web 2.0 delivery framework 
0004:         * Copyright (C) 2007  Hammurapi Group
0005:         *
0006:         * This program is free software; you can redistribute it and/or
0007:         * modify it under the terms of the GNU Lesser General Public
0008:         * License as published by the Free Software Foundation; either
0009:         * version 2 of the License, or (at your option) any later version.
0010:         *
0011:         * This program is distributed in the hope that it will be useful,
0012:         * but WITHOUT ANY WARRANTY; without even the implied warranty of
0013:         * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0014:         * Lesser General Public License for more details.
0015:         *
0016:         * You should have received a copy of the GNU Lesser General Public
0017:         * License along with this library; if not, write to the Free Software
0018:         * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
0019:         *
0020:         * URL: http://www.hammurapi.biz
0021:         * e-Mail: support@hammurapi.biz 
0022:         */
0023:        package biz.hammurapi.web.analysis;
0024:
0025:        import java.io.IOException;
0026:        import java.security.NoSuchAlgorithmException;
0027:        import java.sql.Connection;
0028:        import java.sql.SQLException;
0029:        import java.util.ArrayList;
0030:        import java.util.Collection;
0031:        import java.util.Enumeration;
0032:        import java.util.HashMap;
0033:        import java.util.Iterator;
0034:        import java.util.Map;
0035:
0036:        import javax.servlet.ServletException;
0037:        import javax.servlet.http.HttpServletRequest;
0038:        import javax.servlet.http.HttpServletResponse;
0039:        import javax.xml.parsers.FactoryConfigurationError;
0040:        import javax.xml.parsers.ParserConfigurationException;
0041:
0042:        import org.apache.commons.codec.digest.DigestUtils;
0043:        import org.apache.commons.fileupload.FileUploadException;
0044:        import org.w3c.dom.Element;
0045:
0046:        import biz.hammurapi.config.ConfigurationException;
0047:        import biz.hammurapi.config.MapContext;
0048:        import biz.hammurapi.sql.DatabaseObject;
0049:        import biz.hammurapi.sql.IdentityGenerator;
0050:        import biz.hammurapi.sql.IdentityManager;
0051:        import biz.hammurapi.sql.IdentityRetriever;
0052:        import biz.hammurapi.sql.SQLExceptionEx;
0053:        import biz.hammurapi.sql.SQLProcessor;
0054:        import biz.hammurapi.sql.Transaction;
0055:        import biz.hammurapi.sql.columns.Column;
0056:        import biz.hammurapi.util.Attributable;
0057:        import biz.hammurapi.web.ActionServlet;
0058:        import biz.hammurapi.web.ActionsBase;
0059:        import biz.hammurapi.web.DispatchingServlet;
0060:        import biz.hammurapi.web.FileActions;
0061:        import biz.hammurapi.web.HammurapiWebException;
0062:        import biz.hammurapi.web.HttpError;
0063:        import biz.hammurapi.web.RequestContext;
0064:        import biz.hammurapi.web.RequestParametersContext;
0065:        import biz.hammurapi.web.SimpleRedirect;
0066:        import biz.hammurapi.web.FileActions.DbFileEx;
0067:        import biz.hammurapi.web.analysis.sql.AnalysisDocument;
0068:        import biz.hammurapi.web.analysis.sql.AnalysisDocumentImpl;
0069:        import biz.hammurapi.web.analysis.sql.AnalysisEngine;
0070:        import biz.hammurapi.web.analysis.sql.AnalysisGlossaryLink;
0071:        import biz.hammurapi.web.analysis.sql.AnalysisTerm;
0072:        import biz.hammurapi.web.analysis.sql.AnalysisTermImpl;
0073:        import biz.hammurapi.web.eval.ResultSetEvaluatorFactory;
0074:        import biz.hammurapi.web.file.sql.DbFileImpl;
0075:        import biz.hammurapi.web.file.sql.FileEngine;
0076:        import biz.hammurapi.web.file.sql.FileInfo;
0077:        import biz.hammurapi.web.interaction.InteractionInstance;
0078:        import biz.hammurapi.web.mda.Template;
0079:        import biz.hammurapi.web.mda.TemplateFactory;
0080:        import biz.hammurapi.web.mda.Util;
0081:        import biz.hammurapi.web.mda.db.model.Column.PresentationParametersHelper;
0082:        import biz.hammurapi.web.menu.MenuActions;
0083:        import biz.hammurapi.web.menu.sql.HelpTopic;
0084:        import biz.hammurapi.web.menu.sql.HelpTopicImpl;
0085:        import biz.hammurapi.web.menu.sql.MenuEngine;
0086:        import biz.hammurapi.web.menu.sql.XmenuImpl;
0087:        import biz.hammurapi.web.properties.PropertySet;
0088:        import biz.hammurapi.web.util.GuidGenerator;
0089:        import biz.hammurapi.web.util.PrefixContext;
0090:        import biz.hammurapi.xml.dom.DOMUtils;
0091:        import biz.hammurapi.xml.dom.DomSerializable;
0092:
0093:        /**
0094:         * Performs file operations
0095:         * @author Pavel Vlasov
0096:         */
0097:        public class AnalysisActions extends ActionsBase {
0098:
0099:            protected static FileEngine getEngine(HttpServletRequest request) { // TODO - Replace with AnalysisEngine
0100:                return new FileEngine((SQLProcessor) getGlobal(request,
0101:                        "sql-processor"));
0102:            }
0103:
0104:            /*** Analysis document ***/
0105:
0106:            // Create analysis document
0107:            // Update analysis document
0108:            // Get analysis document for view
0109:            /*** Glossary ***/
0110:
0111:            /**
0112:             * Uploads file content to database (update).
0113:             * @param request
0114:             * @param response
0115:             * @throws IOException 
0116:             * @throws SQLException 
0117:             * @throws ConfigurationException 
0118:             * @throws FileUploadException 
0119:             * @throws HammurapiWebException 
0120:             * @throws NoSuchAlgorithmException 
0121:             */
0122:            public void updateGlossary(HttpServletRequest request,
0123:                    final HttpServletResponse response) throws Exception {
0124:                FileEngine engine = getEngine(request);
0125:
0126:                DbFileImpl dbFile = new DbFileImpl(false) {
0127:                    public void copy(DatabaseObject source) {
0128:                        super .copy(source);
0129:
0130:                        // Clean modified flags.
0131:                        Iterator it = columns.iterator();
0132:                        while (it.hasNext()) {
0133:                            ((Column) it.next()).clearModified();
0134:                        }
0135:                    }
0136:                };
0137:
0138:                String fileId = request.getParameter("ID");
0139:                if (fileId == null) {
0140:                    response.getWriter().write("ID parameter is missing");
0141:                    return;
0142:                }
0143:                FileInfo fileInfo = engine
0144:                        .getFileInfo(Integer.parseInt(fileId));
0145:
0146:                if (fileInfo == null) {
0147:                    response.getWriter().write("Invalid file ID: " + fileId);
0148:                    return;
0149:                }
0150:
0151:                dbFile.copy((DatabaseObject) fileInfo);
0152:
0153:                dbFile.configure(new RequestParametersContext(request),
0154:                        converter);
0155:
0156:                String text = request.getParameter("fileText");
0157:                if (text != null) {
0158:                    text = MenuActions.normalizeLineSeparator(text);
0159:                    byte[] textBytes = text.getBytes();
0160:                    dbFile.setFileSize(new Long(textBytes.length));
0161:                    dbFile.setFileContent(textBytes);
0162:                    dbFile.setDigest(DigestUtils
0163:                            .shaHex(dbFile.getFileContent()));
0164:                    dbFile.setDigestAlgorithm("SHA");
0165:                    if (dbFile.getContentType() == null) {
0166:                        dbFile.setContentType("text/plain");
0167:                    }
0168:                }
0169:
0170:                GuidGenerator guidGenerator = (GuidGenerator) getGlobal(
0171:                        request, "db/guid-generator");
0172:                if (guidGenerator != null && isBlank(dbFile.getGuid())) {
0173:                    dbFile.setGuid(guidGenerator.nextGUID());
0174:                }
0175:
0176:                if (dbFile.isModified()) {
0177:                    dbFile.setLastModified(System.currentTimeMillis());
0178:                    engine.updateDbFile(dbFile);
0179:                }
0180:
0181:                processTerms(dbFile.getId(), new Integer(dbFile.getId()),
0182:                        dbFile.getId(), text, request);
0183:
0184:                String referrer = getReferrer(request);
0185:
0186:                if (referrer == null) {
0187:                    StringBuffer rb = new StringBuffer(
0188:                            (String) new RequestContext(request)
0189:                                    .get("context-path"));
0190:                    rb
0191:                            .append("/ef/xmenu/system/FileActions/getInfo/file/list.html");
0192:                    if (dbFile.getParent() != null) {
0193:                        rb.append("?ID=");
0194:                        rb.append(dbFile.getParent());
0195:                    } else if (dbFile.getOwnerId() != null
0196:                            && dbFile.getOwnerType() != null) {
0197:                        rb.append("?OWNER_TYPE=");
0198:                        rb.append(dbFile.getOwnerType());
0199:                        rb.append("&OWNER_ID=");
0200:                        rb.append(dbFile.getOwnerId());
0201:                    }
0202:                    referrer = rb.toString();
0203:                }
0204:
0205:                response.sendRedirect(referrer);
0206:            }
0207:
0208:            /**
0209:             * Update terms for the glossary
0210:             * @param docId Document id (same as glossary id for glossaries)
0211:             * @param glossaryId Glossary id
0212:             * @param text Glossary text
0213:             * @throws SQLException 
0214:             */
0215:            private void processTerms(int docId, Integer originalGlossaryId,
0216:                    int glossaryId, String text, HttpServletRequest request)
0217:                    throws SQLException {
0218:                SQLProcessor processor = (SQLProcessor) getGlobal(request,
0219:                        "sql-processor");
0220:
0221:                // Delete unreferenced autocreated terms
0222:                AnalysisEngine engine = new AnalysisEngine(processor);
0223:                engine.deleteAnalysisDocTermByDocument(docId);
0224:
0225:                // Parse and update
0226:                if (!isBlank(text)) {
0227:                    IdentityManager identityManager = (IdentityManager) getGlobal(
0228:                            request, "db/IdentityManager");
0229:                    AnalysisTermSource ats = new AnalysisTermSource(glossaryId,
0230:                            processor, identityManager, true, false);
0231:                    AnalysisParser parser = new AnalysisParser(ats);
0232:                    parser.parse(text, false);
0233:
0234:                    Iterator it = ats.getTerms().iterator();
0235:                    while (it.hasNext()) {
0236:                        AnalysisTermEx term = (AnalysisTermEx) it.next();
0237:                        engine.insertAnalysisDocTerm(docId, term.getId(), term
0238:                                .getRefCount());
0239:                    }
0240:                }
0241:                engine.cleanupAutocreatedTerms(glossaryId);
0242:                if (originalGlossaryId != null) {
0243:                    engine.cleanupAutocreatedTerms(originalGlossaryId
0244:                            .intValue());
0245:                }
0246:                Iterator it = engine.getAnalysisGlossaryLinkBySlave(glossaryId)
0247:                        .iterator();
0248:                while (it.hasNext()) {
0249:                    AnalysisGlossaryLink agl = (AnalysisGlossaryLink) it.next();
0250:                    engine.cleanupAutocreatedTerms(agl.getMaster());
0251:                }
0252:            }
0253:
0254:            /**
0255:             * Uploads file content to database (update).
0256:             * @param request
0257:             * @param response
0258:             * @throws IOException 
0259:             * @throws SQLException 
0260:             * @throws ConfigurationException 
0261:             * @throws HammurapiWebException 
0262:             */
0263:            public void createGlossary(HttpServletRequest request,
0264:                    final HttpServletResponse response) throws IOException,
0265:                    SQLException, ConfigurationException, HammurapiWebException {
0266:                String referrer = getReferrer(request);
0267:
0268:                // Read fields
0269:                DbFileImpl dbFile = new DbFileImpl(true);
0270:                String text = request.getParameter("fileText");
0271:                dbFile.configure(new RequestParametersContext(request),
0272:                        converter);
0273:                String actionId = request.getParameter(FileActions.ACTION_ID);
0274:                if (actionId != null) {
0275:                    Object[] info = (Object[]) request.getSession()
0276:                            .getAttribute(
0277:                                    FileActions.ACTION_INFO_PREFIX + actionId);
0278:                    if (info != null) {
0279:                        dbFile.configure(new MapContext((Map) info[0]),
0280:                                converter);
0281:                        if (info[1] != null) {
0282:                            referrer = (String) info[1];
0283:                        }
0284:                    }
0285:                }
0286:                if (text != null) {
0287:                    text = MenuActions.normalizeLineSeparator(text);
0288:                    byte[] textBytes = text.getBytes();
0289:                    dbFile.setFileSize(new Long(textBytes.length));
0290:                    dbFile.setFileContent(textBytes);
0291:                    dbFile.setDigest(DigestUtils
0292:                            .shaHex(dbFile.getFileContent()));
0293:                    dbFile.setDigestAlgorithm("SHA");
0294:                    if (dbFile.getContentType() == null) {
0295:                        dbFile.setContentType("text/plain");
0296:                    }
0297:                }
0298:
0299:                GuidGenerator guidGenerator = (GuidGenerator) getGlobal(
0300:                        request, "db/guid-generator");
0301:                if (guidGenerator != null && isBlank(dbFile.getGuid())) {
0302:                    dbFile.setGuid(guidGenerator.nextGUID());
0303:                }
0304:
0305:                if (dbFile.isModified()) {
0306:                    dbFile.setLastModified(System.currentTimeMillis());
0307:                    IdentityManager identityManager = (IdentityManager) getGlobal(
0308:                            request, "db/IdentityManager");
0309:                    SQLProcessor processor = (SQLProcessor) getGlobal(request,
0310:                            "sql-processor");
0311:                    Connection con = processor.getConnection();
0312:                    FileEngine engine = new FileEngine(new SQLProcessor(con,
0313:                            null));
0314:                    if (identityManager instanceof  IdentityGenerator) {
0315:                        dbFile.setId(((IdentityGenerator) identityManager)
0316:                                .generate(con, "DB_FILE"));
0317:                    }
0318:                    engine.insertDbFile(dbFile);
0319:                    if (identityManager instanceof  IdentityRetriever) {
0320:                        dbFile.setId(((IdentityRetriever) identityManager)
0321:                                .retrieve(con));
0322:                    }
0323:
0324:                    processor.releaseConnection(con);
0325:                }
0326:
0327:                processTerms(dbFile.getId(), new Integer(dbFile.getId()),
0328:                        dbFile.getId(), text, request);
0329:
0330:                if (referrer == null) {
0331:                    StringBuffer rb = new StringBuffer(
0332:                            (String) new RequestContext(request)
0333:                                    .get("context-path"));
0334:                    rb
0335:                            .append("/ef/xmenu/system/FileActions/getInfo/file/list.html");
0336:                    if (dbFile.getParent() != null) {
0337:                        rb.append("?ID=");
0338:                        rb.append(dbFile.getParent());
0339:                    } else if (dbFile.getOwnerId() != null
0340:                            && dbFile.getOwnerType() != null) {
0341:                        rb.append("?OWNER_TYPE=");
0342:                        rb.append(dbFile.getOwnerType());
0343:                        rb.append("&OWNER_ID=");
0344:                        rb.append(dbFile.getOwnerId());
0345:                    }
0346:                    referrer = rb.toString();
0347:                }
0348:
0349:                response.sendRedirect(referrer);
0350:            }
0351:
0352:            // Memory-sensitive cache read-only term sources. Flush on updates.
0353:
0354:            // Get glossary for view
0355:            /**
0356:             * Returns file record with content as text
0357:             */
0358:            public Object getGlossary(HttpServletRequest request,
0359:                    final HttpServletResponse response) throws SQLException {
0360:                String fileId = request.getParameter("ID");
0361:                if (fileId == null) {
0362:                    return "ID parameter is missing";
0363:                }
0364:
0365:                DbFileEx ret = (DbFileEx) getEngine(request).getDbFile(
0366:                        Integer.parseInt(fileId), DbFileEx.class);
0367:                if (ret == null) {
0368:                    return "Invalid file ID";
0369:                }
0370:
0371:                if (ret.getFileContent() != null) {
0372:                    ((DbFileImpl) ret).setColumnAttribute("FILE_CONTENT",
0373:                            "as-text", new String(ret.getFileContent()));
0374:                    ret.setFileContent(null);
0375:                }
0376:
0377:                String referrer = getReferrer(request);
0378:                if (referrer != null) {
0379:                    ret.setAttribute("referrer", referrer);
0380:                }
0381:                return ret;
0382:            }
0383:
0384:            /**
0385:             * Returns file record with content as expanded text and with terms attached.
0386:             */
0387:            public Object viewGlossary(HttpServletRequest request,
0388:                    final HttpServletResponse response) throws SQLException {
0389:                String fileId = request.getParameter("ID");
0390:                if (fileId == null) {
0391:                    return "ID parameter is missing";
0392:                }
0393:                return viewGlossary(Integer.parseInt(fileId), request,
0394:                        response, false);
0395:            }
0396:
0397:            /**
0398:             * Returns file record with content as expanded text and with terms attached.
0399:             */
0400:            private Object viewGlossary(int id, HttpServletRequest request,
0401:                    final HttpServletResponse response, boolean isReadOnlyView)
0402:                    throws SQLException {
0403:                DbFileEx ret = (DbFileEx) getEngine(request).getDbFile(id,
0404:                        DbFileEx.class);
0405:                if (ret == null) {
0406:                    return "Invalid file ID";
0407:                }
0408:
0409:                SQLProcessor processor = (SQLProcessor) getGlobal(request,
0410:                        "sql-processor");
0411:                AnalysisTermSource ats = new AnalysisTermSource(ret.getId(),
0412:                        processor, null, false, isReadOnlyView);
0413:                AnalysisParser ap = new AnalysisParser(ats);
0414:                ats.loadTerms();
0415:                Iterator it = new ArrayList(ats.getTerms()).iterator();
0416:                while (it.hasNext()) {
0417:                    AnalysisTerm term = (AnalysisTerm) it.next();
0418:                    if (term.getDescription() != null) {
0419:                        term.setDescription(ap.parse(term.getDescription(),
0420:                                false));
0421:                    }
0422:                }
0423:
0424:                AnalysisEngine engine = new AnalysisEngine(processor);
0425:                Map groupedRootTerms = ats.getGroupedRootTerms();
0426:                setUsage(groupedRootTerms.values(), engine);
0427:                ret.setAttribute("terms", groupedRootTerms);
0428:                ret.setAttribute("masters", engine.getMasterGlossaries(ret
0429:                        .getId(), new ArrayList()));
0430:                ret.setAttribute("slaves", engine.getSlaveGlossaries(ret
0431:                        .getId(), new ArrayList()));
0432:
0433:                if (ret.getFileContent() != null) {
0434:                    String text = new String(ret.getFileContent());
0435:                    ((DbFileImpl) ret).setColumnAttribute("FILE_CONTENT",
0436:                            "as-text", ap.parse(text, false));
0437:                    ret.setFileContent(null);
0438:                }
0439:
0440:                String referrer = getReferrer(request);
0441:                if (referrer != null) {
0442:                    ret.setAttribute("referrer", referrer);
0443:                }
0444:                return ret;
0445:            }
0446:
0447:            private void setUsage(Collection collection, AnalysisEngine engine)
0448:                    throws SQLException {
0449:                Iterator it = collection.iterator();
0450:                while (it.hasNext()) {
0451:                    Object next = it.next();
0452:                    if (next instanceof  Map) {
0453:                        setUsage(((Map) next).values(), engine);
0454:                    } else if (next instanceof  Collection) {
0455:                        setUsage((Collection) next, engine);
0456:                    } else if (next instanceof  AnalysisTerm) {
0457:                        AnalysisTerm at = (AnalysisTerm) next;
0458:                        if (at.getGlossaryId() != null) {
0459:                            Collection termUse = engine.getTermUse(at.getId(),
0460:                                    at.getId(), at.getGlossaryId().intValue(),
0461:                                    new ArrayList());
0462:                            if (!termUse.isEmpty()) {
0463:                                ((Attributable) at).setAttribute("usage",
0464:                                        termUse);
0465:                            }
0466:                        }
0467:                    }
0468:                }
0469:            }
0470:
0471:            /**
0472:             * Returns file record with content as expanded text and with terms attached. 
0473:             * This method is for read-only view rendering 
0474:             * @throws ServletException 
0475:             * @throws FactoryConfigurationError 
0476:             * @throws ParserConfigurationException 
0477:             */
0478:            public Object viewGlossaryRo(HttpServletRequest request,
0479:                    final HttpServletResponse response,
0480:                    DispatchingServlet servlet, String path)
0481:                    throws SQLException, ParserConfigurationException,
0482:                    FactoryConfigurationError, ServletException {
0483:                if (isBlank(path)) {
0484:                    return "Invalid file path";
0485:                }
0486:
0487:                int idx = path.indexOf("/");
0488:                if (idx < 1) {
0489:                    return "Invalid file path";
0490:                }
0491:
0492:                Object ret = viewGlossary(Integer.parseInt(path.substring(0,
0493:                        idx)), request, response, true);
0494:                if (ret instanceof  DbFileEx) {
0495:                    ((DbFileEx) ret).setAttribute("read-only", "yes");
0496:                    servlet.style(ret, "analysis/viewGlossary.html", request,
0497:                            response);
0498:                    return null;
0499:                }
0500:
0501:                return ret;
0502:            }
0503:
0504:            /*** Term ***/
0505:
0506:            /**
0507:             * Uploads file content to database (update).
0508:             * @param request
0509:             * @param response
0510:             * @throws IOException 
0511:             * @throws SQLException 
0512:             * @throws ConfigurationException 
0513:             * @throws FileUploadException 
0514:             * @throws HammurapiWebException 
0515:             * @throws NoSuchAlgorithmException 
0516:             */
0517:            public Object updateTerm(HttpServletRequest request,
0518:                    final HttpServletResponse response) throws Exception {
0519:                SQLProcessor processor = (SQLProcessor) getGlobal(request,
0520:                        "sql-processor");
0521:                AnalysisEngine engine = new AnalysisEngine(processor);
0522:
0523:                if (processor == null) {
0524:                    return "SQL processor global:sql-processor not found";
0525:                }
0526:
0527:                AnalysisTermImpl term = (AnalysisTermImpl) engine
0528:                        .getAnalysisTerm(Integer.parseInt(request
0529:                                .getParameter("ID")));
0530:                term
0531:                        .configure(new RequestParametersContext(request),
0532:                                converter);
0533:                if (term.isModified()) {
0534:                    term.setIsAutocreated(false);
0535:                    if (engine.updateAnalysisTerm(term) == 0) {
0536:                        return "Update failed";
0537:                    }
0538:
0539:                    processTermTerms(term, request);
0540:                }
0541:
0542:                String referrer = getReferrer(request);
0543:                if (referrer == null) {
0544:                    return "Term created";
0545:                }
0546:
0547:                return new SimpleRedirect(referrer, "Term created");
0548:            }
0549:
0550:            /**
0551:             * Update term references
0552:             * @param id Glossary id
0553:             * @param text Glossary text
0554:             * @throws SQLException 
0555:             */
0556:            private void processTermTerms(AnalysisTerm term,
0557:                    HttpServletRequest request) throws SQLException {
0558:                SQLProcessor processor = (SQLProcessor) getGlobal(request,
0559:                        "sql-processor");
0560:
0561:                // Delete unreferenced autocreated terms
0562:                AnalysisEngine engine = new AnalysisEngine(processor);
0563:                engine.deleteAnalysisTermReferenceBySource(term.getId());
0564:                if (term.getGlossaryId() != null) {
0565:                    engine.cleanupAutocreatedTerms(term.getGlossaryId()
0566:                            .intValue());
0567:
0568:                    // Parse and update
0569:                    if (!isBlank(term.getDescription())) {
0570:                        IdentityManager identityManager = (IdentityManager) getGlobal(
0571:                                request, "db/IdentityManager");
0572:                        AnalysisTermSource ats = new AnalysisTermSource(term
0573:                                .getGlossaryId().intValue(), processor,
0574:                                identityManager, true, false);
0575:                        AnalysisParser parser = new AnalysisParser(ats);
0576:                        parser.parse(term.getDescription(), false);
0577:
0578:                        Iterator it = ats.getTerms().iterator();
0579:                        while (it.hasNext()) {
0580:                            AnalysisTermEx referenced = (AnalysisTermEx) it
0581:                                    .next();
0582:                            engine.insertAnalysisTermReference(term.getId(),
0583:                                    referenced.getId(), referenced
0584:                                            .getRefCount());
0585:                        }
0586:                    }
0587:                }
0588:            }
0589:
0590:            /**
0591:             * Uploads file content to database (update).
0592:             * @param request
0593:             * @param response
0594:             * @throws SQLException 
0595:             * @throws ConfigurationException 
0596:             */
0597:            public Object createTerm(HttpServletRequest request,
0598:                    final HttpServletResponse response) throws SQLException,
0599:                    ConfigurationException {
0600:                SQLProcessor processor = (SQLProcessor) getGlobal(request,
0601:                        "sql-processor");
0602:
0603:                if (processor == null) {
0604:                    return "SQL processor global:sql-processor not found";
0605:                }
0606:
0607:                IdentityManager identityManager = (IdentityManager) getGlobal(
0608:                        request, "db/IdentityManager");
0609:                AnalysisTermSource ats = new AnalysisTermSource(Integer
0610:                        .parseInt(request.getParameter("GLOSSARY_ID")),
0611:                        processor, identityManager, true, false);
0612:                AnalysisTermEx term = (AnalysisTermEx) ats.getTerm(request
0613:                        .getParameter("NAMESPACE"), request
0614:                        .getParameter("NAME"), null);
0615:                String termName = term.getName();
0616:                term
0617:                        .configure(new RequestParametersContext(request),
0618:                                converter);
0619:                term.setName(termName);
0620:                term.setIsAutocreated(false);
0621:                AnalysisEngine engine = new AnalysisEngine(processor);
0622:                if (term.isModified()) {
0623:                    if (engine.updateAnalysisTerm(term) == 0) {
0624:                        return "Update failed";
0625:                    }
0626:                }
0627:
0628:                AnalysisTermEx parent = term.getParent();
0629:                if (parent != null && parent.getIsAutocreated()) {
0630:                    parent.setIsAutocreated(false);
0631:                    if (engine.updateAnalysisTerm(parent) == 0) {
0632:                        return "Parent term update failed";
0633:                    }
0634:                }
0635:                processTermTerms(term, request);
0636:
0637:                String referrer = getReferrer(request);
0638:                if (referrer == null) {
0639:                    return "Term created";
0640:                }
0641:
0642:                return new SimpleRedirect(referrer, "Term created");
0643:            }
0644:
0645:            /**
0646:             * Uploads bulk of terms
0647:             * @param request
0648:             * @param response
0649:             * @throws SQLException 
0650:             * @throws ConfigurationException 
0651:             */
0652:            public Object createTerms(HttpServletRequest request,
0653:                    final HttpServletResponse response) throws SQLException,
0654:                    ConfigurationException {
0655:                SQLProcessor processor = (SQLProcessor) getGlobal(request,
0656:                        "sql-processor");
0657:
0658:                if (processor == null) {
0659:                    return "SQL processor global:sql-processor not found";
0660:                }
0661:
0662:                IdentityManager identityManager = (IdentityManager) getGlobal(
0663:                        request, "db/IdentityManager");
0664:                AnalysisTermSource ats = new AnalysisTermSource(Integer
0665:                        .parseInt(request.getParameter("GLOSSARY_ID")),
0666:                        processor, identityManager, true, false);
0667:
0668:                Enumeration pne = request.getParameterNames();
0669:                while (pne.hasMoreElements()) {
0670:                    String pName = (String) pne.nextElement();
0671:                    if (pName.endsWith(":NAME")
0672:                            && !isBlank(request.getParameter(pName))) {
0673:                        String prefix = pName.substring(0, pName.length()
0674:                                - "NAME".length());
0675:                        AnalysisTermEx term = (AnalysisTermEx) ats.getTerm(
0676:                                request.getParameter(prefix + "NAMESPACE"),
0677:                                request.getParameter(prefix + "NAME"), null);
0678:                        String termName = term.getName();
0679:                        term.configure(new PrefixContext(
0680:                                new RequestParametersContext(request), prefix),
0681:                                converter);
0682:                        term.setName(termName);
0683:                        term.setIsAutocreated(false);
0684:                        AnalysisEngine engine = new AnalysisEngine(processor);
0685:                        if (term.isModified()) {
0686:                            if (engine.updateAnalysisTerm(term) == 0) {
0687:                                return "Update failed";
0688:                            }
0689:                        }
0690:
0691:                        AnalysisTermEx parent = term.getParent();
0692:                        if (parent != null && parent.getIsAutocreated()) {
0693:                            parent.setIsAutocreated(false);
0694:                            if (engine.updateAnalysisTerm(parent) == 0) {
0695:                                return "Parent term update failed";
0696:                            }
0697:                        }
0698:                        processTermTerms(term, request);
0699:                    }
0700:                }
0701:
0702:                String referrer = getReferrer(request);
0703:                if (referrer == null) {
0704:                    return "Term created";
0705:                }
0706:
0707:                return new SimpleRedirect(referrer, "Term created");
0708:            }
0709:
0710:            // Get term tooltip
0711:            public Object getTermTooltip(HttpServletRequest request,
0712:                    final HttpServletResponse response, ActionServlet servlet,
0713:                    String path) throws SQLException {
0714:                if (isBlank(path)) {
0715:                    return new HttpError("Invalid URL");
0716:                }
0717:
0718:                if (!path.endsWith(AnalysisTermEx.TERM_TOOLTIP_SUFFIX)) {
0719:                    return new HttpError("Invalid URL");
0720:                }
0721:
0722:                String termId;
0723:                boolean readOnlyView;
0724:                if (path.startsWith(AnalysisTermEx.TERM_TOOLTIP_PREFIX)) {
0725:                    readOnlyView = false;
0726:                    termId = path.substring(AnalysisTermEx.TERM_TOOLTIP_PREFIX
0727:                            .length(), path.length()
0728:                            - AnalysisTermEx.TERM_TOOLTIP_SUFFIX.length());
0729:                } else if (path
0730:                        .startsWith(AnalysisTermEx.RO_TERM_TOOLTIP_PREFIX)) {
0731:                    readOnlyView = true;
0732:                    termId = path.substring(
0733:                            AnalysisTermEx.RO_TERM_TOOLTIP_PREFIX.length(),
0734:                            path.length()
0735:                                    - AnalysisTermEx.TERM_TOOLTIP_SUFFIX
0736:                                            .length());
0737:                } else {
0738:                    return new HttpError("Invalid URL");
0739:                }
0740:
0741:                SQLProcessor processor = (SQLProcessor) getGlobal(request,
0742:                        "sql-processor");
0743:                AnalysisEngine engine = new AnalysisEngine(processor);
0744:                AnalysisTerm term = engine.getAnalysisTerm(Integer
0745:                        .parseInt(termId));
0746:                if (term == null) {
0747:                    return "Invalid term ID";
0748:                }
0749:
0750:                if (term.getGlossaryId() != null
0751:                        && !isBlank(term.getDescription())) {
0752:                    AnalysisTermSource ats = new AnalysisTermSource(term
0753:                            .getGlossaryId().intValue(), processor, null,
0754:                            false, readOnlyView);
0755:                    AnalysisParser ap = new AnalysisParser(ats);
0756:                    //			ats.loadTerms();
0757:                    return ap.parse(term.getDescription(), true);
0758:                }
0759:                return "";
0760:            }
0761:
0762:            /**
0763:             * Generates content from analysis term
0764:             */
0765:            public String generate(HttpServletRequest request)
0766:                    throws SQLException, HammurapiWebException {
0767:                Map evalContext = (Map) request.getAttribute("evalContext");
0768:                if (evalContext == null) {
0769:                    throw new HammurapiWebException(
0770:                            "Evaluation context is not available");
0771:                }
0772:
0773:                InteractionInstance interactionInstance = (InteractionInstance) evalContext
0774:                        .get("interaction");
0775:                if (interactionInstance == null) {
0776:                    throw new HammurapiWebException(
0777:                            "Interaction instance is not available");
0778:                }
0779:
0780:                SQLProcessor processor = (SQLProcessor) getGlobal(request,
0781:                        "sql-processor");
0782:                if (processor == null) {
0783:                    throw new HammurapiWebException(
0784:                            "SQL processor global:sql-processor not found");
0785:                }
0786:
0787:                String termId = (String) interactionInstance
0788:                        .getProperty("termId");
0789:                if (termId == null) {
0790:                    throw new HammurapiWebException(
0791:                            "Term ID is not set in interaction");
0792:                }
0793:
0794:                AnalysisEngine engine = new AnalysisEngine(processor);
0795:                AnalysisTermEx term = (AnalysisTermEx) engine.getAnalysisTerm(
0796:                        Integer.parseInt(termId), AnalysisTermEx.class);
0797:
0798:                ResultSetEvaluatorFactory evaluatorFactory = (ResultSetEvaluatorFactory) new RequestContext(
0799:                        request).get("global:db/EvaluatorFactory");
0800:                TemplateFactory templateFactory = new TemplateFactory(
0801:                        processor, evaluatorFactory);
0802:
0803:                // Instantiate sub-term templates
0804:                Collection subTerms = new ArrayList(); // Instantiated sub-terms
0805:                Iterator it = Util.multiValues(
0806:                        (String) interactionInstance.getProperty("subTerms"))
0807:                        .iterator();
0808:                while (it.hasNext()) {
0809:                    int id = Integer.parseInt((String) it.next());
0810:                    Iterator stit = term.getChildren().iterator();
0811:                    while (stit.hasNext()) {
0812:                        AnalysisTermEx child = (AnalysisTermEx) stit.next();
0813:                        if (child.getId() == id) {
0814:                            String subTermPresentation = (String) interactionInstance
0815:                                    .getProperty("terms/" + id);
0816:                            subTerms.add(child); // Add child AS-IS - no presentation set.
0817:                            if (subTermPresentation != null) { // Render sub-term presentation using fake Column.
0818:                                Map context = new HashMap();
0819:                                biz.hammurapi.web.mda.db.model.Column column = new biz.hammurapi.web.mda.db.model.Column(); // Fake column for generation
0820:                                column.setName((String) interactionInstance
0821:                                        .getProperty("controlNames/" + id));
0822:                                column.setJavaName(column.getName());
0823:                                column.setId(id);
0824:                                column.setLabel(child.getName());
0825:                                column.setDescription(term.getDescription());
0826:                                context.put("column", column);
0827:                                final PropertySet presentationParameters = interactionInstance
0828:                                        .getProperties().getSubset(
0829:                                                "terms/" + id + "/parameter/");
0830:                                context.put("presentationParameters",
0831:                                        presentationParameters);
0832:                                context.put("presentationParametersHelper",
0833:                                        new PresentationParametersHelper() {
0834:
0835:                                            public String getIf(
0836:                                                    String parameterName,
0837:                                                    String value,
0838:                                                    String ifTrue,
0839:                                                    String ifFalse) {
0840:                                                return value
0841:                                                        .equals(presentationParameters
0842:                                                                .get(parameterName)) ? ifTrue
0843:                                                        : ifFalse;
0844:                                            }
0845:
0846:                                            public String getIf(
0847:                                                    String parameterName,
0848:                                                    String value, String ifTrue) {
0849:                                                return getIf(parameterName,
0850:                                                        value, ifTrue, "");
0851:                                            }
0852:
0853:                                        });
0854:                                Template template = templateFactory
0855:                                        .getTemplate(Integer
0856:                                                .parseInt(subTermPresentation));
0857:                                if (template == null) {
0858:                                    throw new HammurapiWebException(
0859:                                            "Temlate not found for term " + id);
0860:                                }
0861:                                child.setAttribute("content", template
0862:                                        .evaluate(context, getClass()
0863:                                                .getClassLoader()));
0864:                            }
0865:                            break;
0866:                        }
0867:                    }
0868:                }
0869:
0870:                Template template;
0871:                String target = (String) interactionInstance
0872:                        .getProperty("target");
0873:                if ("Custom".equals(target)) {
0874:                    template = templateFactory.getTemplate(Integer
0875:                            .parseInt((String) interactionInstance
0876:                                    .getProperty("customTemplate")));
0877:                } else {
0878:                    template = templateFactory.getTemplate("Analysis term",
0879:                            target);
0880:                    if (template == null) {
0881:                        throw new HammurapiWebException("Template '" + target
0882:                                + "' not found");
0883:                    }
0884:                }
0885:
0886:                Map context = new HashMap();
0887:                context.put("term", term);
0888:                context.put("subTerms", subTerms);
0889:                context.put("properties", interactionInstance.getProperties());
0890:
0891:                return template.evaluate(context, getClass().getClassLoader());
0892:            }
0893:
0894:            /**
0895:             * Creates menu from analysis term and collected information. Redirects to interaction controller on success.
0896:             * @param request
0897:             * @param response
0898:             * @return
0899:             * @throws SQLException
0900:             * @throws HammurapiWebException
0901:             */
0902:            public Object createMenu(HttpServletRequest request,
0903:                    final HttpServletResponse response) throws SQLException,
0904:                    HammurapiWebException {
0905:                Map evalContext = (Map) request.getAttribute("evalContext");
0906:                if (evalContext == null) {
0907:                    return "Evaluation context is not available";
0908:                }
0909:
0910:                InteractionInstance interactionInstance = (InteractionInstance) evalContext
0911:                        .get("interaction");
0912:                if (interactionInstance == null) {
0913:                    return "Interaction instance is not available";
0914:                }
0915:
0916:                SQLProcessor processor = (SQLProcessor) getGlobal(request,
0917:                        "sql-processor");
0918:                if (processor == null) {
0919:                    return "SQL Processor not found";
0920:                }
0921:
0922:                String content = generate(request);
0923:                processor.executeTransaction(generatePage(request, content,
0924:                        interactionInstance));
0925:
0926:                return new SimpleRedirect(
0927:                        new RequestContext(request).get("context-path")
0928:                                + "/system/interaction.InterActions/next?interactionInstance="
0929:                                + interactionInstance.getId()
0930:                                + "&interactionStep="
0931:                                + request.getParameter("interactionStep"),
0932:                        "Generation successful");
0933:            }
0934:
0935:            private Transaction generatePage(final HttpServletRequest request,
0936:                    final String content,
0937:                    final InteractionInstance interactionInstance) {
0938:                return new Transaction() {
0939:
0940:                    public boolean execute(SQLProcessor processor)
0941:                            throws SQLException {
0942:                        IdentityManager identityManager = (IdentityManager) getGlobal(
0943:                                request, "db/IdentityManager");
0944:                        Connection con = processor.getConnection();
0945:                        SQLProcessor cprc = new SQLProcessor(con, null);
0946:                        AnalysisEngine engine = new AnalysisEngine(cprc);
0947:                        int termId = Integer
0948:                                .parseInt((String) interactionInstance
0949:                                        .getProperty("termId"));
0950:                        AnalysisTermEx term = (AnalysisTermEx) engine
0951:                                .getAnalysisTerm(termId, AnalysisTermEx.class);
0952:
0953:                        XmenuImpl menu = new XmenuImpl(false);
0954:                        MenuEngine menuEngine = new MenuEngine(cprc);
0955:
0956:                        GuidGenerator guidGenerator = (GuidGenerator) getGlobal(
0957:                                request, "db/guid-generator");
0958:                        if (guidGenerator != null) {
0959:                            try {
0960:                                menu.setGuid(guidGenerator.nextGUID());
0961:                            } catch (HammurapiWebException e) {
0962:                                throw new SQLExceptionEx(e);
0963:                            }
0964:                        }
0965:
0966:                        menu.setContent(content);
0967:                        menu.setName((String) interactionInstance
0968:                                .getProperty("itemName"));
0969:                        menu.setDescription(term.getDescription());
0970:                        menu.setTitle("Generated from analysis term "
0971:                                + term.getName());
0972:                        menu.setScope("Public");
0973:                        menu.setContentType("Jxp");
0974:                        menu.setType("Item");
0975:                        menu.setXid((String) interactionInstance
0976:                                .getProperty("itemName"));
0977:                        String parent = (String) interactionInstance
0978:                                .getProperty("parentMenu");
0979:                        if (parent != null) {
0980:                            menu.setParent(new Integer(parent));
0981:                        }
0982:
0983:                        if (identityManager instanceof  IdentityGenerator) {
0984:                            menu.setId(((IdentityGenerator) identityManager)
0985:                                    .generate(con, "XMENU"));
0986:                        }
0987:
0988:                        menuEngine.insertXmenu(menu);
0989:
0990:                        if (identityManager instanceof  IdentityRetriever) {
0991:                            menu.setId(((IdentityRetriever) identityManager)
0992:                                    .retrieve(con));
0993:                        }
0994:
0995:                        HelpTopic mainTopic = new HelpTopicImpl(false);
0996:                        mainTopic.setContent(menu.getDescription());
0997:                        mainTopic.setName("main");
0998:                        mainTopic.setMenuId(menu.getId());
0999:                        mainTopic.setLastModified(System.currentTimeMillis());
1000:                        mainTopic.setRenderTemplate("AjaxTooltip");
1001:                        mainTopic.setType("Content");
1002:                        menuEngine.insertHelpTopic(mainTopic);
1003:
1004:                        Iterator it = Util.multiValues(
1005:                                (String) interactionInstance
1006:                                        .getProperty("subTerms")).iterator();
1007:                        while (it.hasNext()) {
1008:                            int id = Integer.parseInt((String) it.next());
1009:                            Iterator stit = term.getChildren().iterator();
1010:                            while (stit.hasNext()) {
1011:                                AnalysisTermEx child = (AnalysisTermEx) stit
1012:                                        .next();
1013:                                if (child.getId() == id) {
1014:                                    if (!isBlank(child.getDescription())) {
1015:                                        HelpTopic childTopic = new HelpTopicImpl(
1016:                                                false);
1017:                                        childTopic.setContent(child
1018:                                                .getDescription());
1019:                                        childTopic.setName("#"
1020:                                                + child.getName());
1021:                                        childTopic.setMenuId(menu.getId());
1022:                                        childTopic.setLastModified(System
1023:                                                .currentTimeMillis());
1024:                                        childTopic
1025:                                                .setRenderTemplate("AjaxTooltip");
1026:                                        childTopic.setType("Content");
1027:                                        menuEngine.insertHelpTopic(childTopic);
1028:                                    }
1029:                                    break;
1030:                                }
1031:                            }
1032:                        }
1033:                        return true;
1034:                    }
1035:
1036:                };
1037:            }
1038:
1039:            public Object updateGlossaryLinks(HttpServletRequest request,
1040:                    final HttpServletResponse response) throws Exception {
1041:                String slaveStr = request.getParameter("SLAVE");
1042:                if (slaveStr == null) {
1043:                    return "Parameter SLAVE is not set";
1044:                }
1045:
1046:                SQLProcessor processor = (SQLProcessor) getGlobal(request,
1047:                        "sql-processor");
1048:                AnalysisEngine engine = new AnalysisEngine(processor);
1049:                int slaveId = Integer.parseInt(slaveStr);
1050:                engine.deleteAnalysisGlossaryLinkBySlave(slaveId);
1051:                String[] masters = request.getParameterValues("MASTER");
1052:                for (int i = 0; masters != null && i < masters.length; ++i) {
1053:                    engine.insertAnalysisGlossaryLink(Integer
1054:                            .parseInt(masters[i]), slaveId, request
1055:                            .getParameter("NAMESPACE_" + masters[i]));
1056:                }
1057:
1058:                String referrer = getReferrer(request);
1059:                if (referrer == null) {
1060:                    return "Links updated";
1061:                }
1062:
1063:                return new SimpleRedirect(referrer, "Term created");
1064:            }
1065:
1066:            /**
1067:             * Retrieves data needed for population of the analysis document create form.
1068:             * @param request
1069:             * @param response
1070:             * @return
1071:             * @throws SQLException
1072:             */
1073:            public Object beforeCreateAnalysisDocument(
1074:                    HttpServletRequest request,
1075:                    final HttpServletResponse response) throws SQLException {
1076:                SQLProcessor processor = (SQLProcessor) getGlobal(request,
1077:                        "sql-processor");
1078:                AnalysisEngine engine = new AnalysisEngine(processor);
1079:                final Collection glossaries = engine
1080:                        .getGlossaries(new ArrayList());
1081:                final DomSerializable ds = compositeDomSerializer
1082:                        .toDomSerializable(request);
1083:                return new DomSerializable() {
1084:
1085:                    public void toDom(Element holder) {
1086:                        ds.toDom(holder);
1087:                        DOMUtils.toDom(glossaries, "glossaries", holder);
1088:                    }
1089:
1090:                };
1091:            }
1092:
1093:            /**
1094:             * Returns file record with content as text
1095:             */
1096:            public Object getAnalysisDocument(HttpServletRequest request,
1097:                    final HttpServletResponse response) throws SQLException {
1098:                String fileId = request.getParameter("ID");
1099:                if (fileId == null) {
1100:                    return "ID parameter is missing";
1101:                }
1102:
1103:                DbFileEx ret = (DbFileEx) getEngine(request).getDbFile(
1104:                        Integer.parseInt(fileId), DbFileEx.class);
1105:                if (ret == null) {
1106:                    return "Invalid file ID";
1107:                }
1108:
1109:                if (ret.getFileContent() != null) {
1110:                    ((DbFileImpl) ret).setColumnAttribute("FILE_CONTENT",
1111:                            "as-text", new String(ret.getFileContent()));
1112:                    ret.setFileContent(null);
1113:                }
1114:
1115:                SQLProcessor processor = (SQLProcessor) getGlobal(request,
1116:                        "sql-processor");
1117:                AnalysisEngine engine = new AnalysisEngine(processor);
1118:                Collection glossaries = engine.getGlossaries(new ArrayList());
1119:                ret.setAttribute("glossaries", glossaries);
1120:                AnalysisDocument info = engine.getAnalysisDocument(ret.getId());
1121:                if (info != null) {
1122:                    ret.setAttribute("info", info);
1123:                }
1124:
1125:                String referrer = getReferrer(request);
1126:                if (referrer != null) {
1127:                    ret.setAttribute("referrer", referrer);
1128:                }
1129:                return ret;
1130:            }
1131:
1132:            /**
1133:             * Uploads file content to database (update).
1134:             * @param request
1135:             * @param response
1136:             * @throws IOException 
1137:             * @throws SQLException 
1138:             * @throws ConfigurationException 
1139:             * @throws HammurapiWebException 
1140:             */
1141:            public void createAnalysisDocument(HttpServletRequest request,
1142:                    final HttpServletResponse response) throws IOException,
1143:                    SQLException, ConfigurationException, HammurapiWebException {
1144:                String referrer = getReferrer(request);
1145:
1146:                // Read fields
1147:                DbFileImpl dbFile = new DbFileImpl(true);
1148:                String text = request.getParameter("fileText");
1149:                dbFile.configure(new RequestParametersContext(request),
1150:                        converter);
1151:                String actionId = request.getParameter(FileActions.ACTION_ID);
1152:                if (actionId != null) {
1153:                    Object[] info = (Object[]) request.getSession()
1154:                            .getAttribute(
1155:                                    FileActions.ACTION_INFO_PREFIX + actionId);
1156:                    if (info != null) {
1157:                        dbFile.configure(new MapContext((Map) info[0]),
1158:                                converter);
1159:                        if (info[1] != null) {
1160:                            referrer = (String) info[1];
1161:                        }
1162:                    }
1163:                }
1164:                if (text != null) {
1165:                    text = MenuActions.normalizeLineSeparator(text);
1166:                    byte[] textBytes = text.getBytes();
1167:                    dbFile.setFileSize(new Long(textBytes.length));
1168:                    dbFile.setFileContent(textBytes);
1169:                    dbFile.setDigest(DigestUtils
1170:                            .shaHex(dbFile.getFileContent()));
1171:                    dbFile.setDigestAlgorithm("SHA");
1172:                    if (dbFile.getContentType() == null) {
1173:                        dbFile.setContentType("text/plain");
1174:                    }
1175:                }
1176:
1177:                GuidGenerator guidGenerator = (GuidGenerator) getGlobal(
1178:                        request, "db/guid-generator");
1179:                if (guidGenerator != null && isBlank(dbFile.getGuid())) {
1180:                    dbFile.setGuid(guidGenerator.nextGUID());
1181:                }
1182:
1183:                if (dbFile.isModified()) {
1184:                    dbFile.setLastModified(System.currentTimeMillis());
1185:                    IdentityManager identityManager = (IdentityManager) getGlobal(
1186:                            request, "db/IdentityManager");
1187:                    SQLProcessor processor = (SQLProcessor) getGlobal(request,
1188:                            "sql-processor");
1189:                    Connection con = processor.getConnection();
1190:                    FileEngine engine = new FileEngine(new SQLProcessor(con,
1191:                            null));
1192:                    if (identityManager instanceof  IdentityGenerator) {
1193:                        dbFile.setId(((IdentityGenerator) identityManager)
1194:                                .generate(con, "DB_FILE"));
1195:                    }
1196:                    engine.insertDbFile(dbFile);
1197:                    if (identityManager instanceof  IdentityRetriever) {
1198:                        dbFile.setId(((IdentityRetriever) identityManager)
1199:                                .retrieve(con));
1200:                    }
1201:
1202:                    processor.releaseConnection(con);
1203:                }
1204:
1205:                String glossaryIdStr = request.getParameter("GLOSSARY_ID");
1206:                if (glossaryIdStr != null) {
1207:                    int glossaryId = Integer.parseInt(glossaryIdStr);
1208:                    processTerms(dbFile.getId(), null, glossaryId, text,
1209:                            request);
1210:                    SQLProcessor processor = (SQLProcessor) getGlobal(request,
1211:                            "sql-processor");
1212:                    AnalysisEngine engine = new AnalysisEngine(processor);
1213:                    AnalysisDocument doc = new AnalysisDocumentImpl(true);
1214:                    doc.setId(dbFile.getId());
1215:                    doc.setGlossaryId(new Integer(glossaryId));
1216:                    engine.insertAnalysisDocument(doc);
1217:                }
1218:
1219:                if (referrer == null) {
1220:                    StringBuffer rb = new StringBuffer(
1221:                            (String) new RequestContext(request)
1222:                                    .get("context-path"));
1223:                    rb
1224:                            .append("/ef/xmenu/system/FileActions/getInfo/file/list.html");
1225:                    if (dbFile.getParent() != null) {
1226:                        rb.append("?ID=");
1227:                        rb.append(dbFile.getParent());
1228:                    } else if (dbFile.getOwnerId() != null
1229:                            && dbFile.getOwnerType() != null) {
1230:                        rb.append("?OWNER_TYPE=");
1231:                        rb.append(dbFile.getOwnerType());
1232:                        rb.append("&OWNER_ID=");
1233:                        rb.append(dbFile.getOwnerId());
1234:                    }
1235:                    referrer = rb.toString();
1236:                }
1237:
1238:                response.sendRedirect(referrer);
1239:            }
1240:
1241:            /**
1242:             * Uploads file content to database (update).
1243:             * @param request
1244:             * @param response
1245:             */
1246:            public void updateAnalysisDocument(HttpServletRequest request,
1247:                    final HttpServletResponse response) throws Exception {
1248:                FileEngine engine = getEngine(request);
1249:
1250:                DbFileImpl dbFile = new DbFileImpl(false) {
1251:                    public void copy(DatabaseObject source) {
1252:                        super .copy(source);
1253:
1254:                        // Clean modified flags.
1255:                        Iterator it = columns.iterator();
1256:                        while (it.hasNext()) {
1257:                            ((Column) it.next()).clearModified();
1258:                        }
1259:                    }
1260:                };
1261:
1262:                String fileId = request.getParameter("ID");
1263:                if (fileId == null) {
1264:                    response.getWriter().write("ID parameter is missing");
1265:                    return;
1266:                }
1267:                FileInfo fileInfo = engine
1268:                        .getFileInfo(Integer.parseInt(fileId));
1269:
1270:                if (fileInfo == null) {
1271:                    response.getWriter().write("Invalid file ID: " + fileId);
1272:                    return;
1273:                }
1274:
1275:                dbFile.copy((DatabaseObject) fileInfo);
1276:
1277:                dbFile.configure(new RequestParametersContext(request),
1278:                        converter);
1279:
1280:                String text = request.getParameter("fileText");
1281:                if (text != null) {
1282:                    text = MenuActions.normalizeLineSeparator(text);
1283:                    byte[] textBytes = text.getBytes();
1284:                    dbFile.setFileSize(new Long(textBytes.length));
1285:                    dbFile.setFileContent(textBytes);
1286:                    dbFile.setDigest(DigestUtils
1287:                            .shaHex(dbFile.getFileContent()));
1288:                    dbFile.setDigestAlgorithm("SHA");
1289:                    if (dbFile.getContentType() == null) {
1290:                        dbFile.setContentType("text/plain");
1291:                    }
1292:                }
1293:
1294:                GuidGenerator guidGenerator = (GuidGenerator) getGlobal(
1295:                        request, "db/guid-generator");
1296:                if (guidGenerator != null && isBlank(dbFile.getGuid())) {
1297:                    dbFile.setGuid(guidGenerator.nextGUID());
1298:                }
1299:
1300:                if (dbFile.isModified()) {
1301:                    dbFile.setLastModified(System.currentTimeMillis());
1302:                    engine.updateDbFile(dbFile);
1303:                }
1304:
1305:                String ogis = request.getParameter("ORIGINAL:GLOSSARY_ID");
1306:                Integer originalGlossaryId = ogis == null ? null : new Integer(
1307:                        ogis);
1308:
1309:                String glossaryIdStr = request.getParameter("GLOSSARY_ID");
1310:                if (glossaryIdStr != null) {
1311:                    int glossaryId = Integer.parseInt(glossaryIdStr);
1312:                    processTerms(dbFile.getId(), originalGlossaryId,
1313:                            glossaryId, text, request);
1314:
1315:                    if (ogis != null
1316:                            && !ogis.trim().equals(glossaryIdStr.trim())) {
1317:                        SQLProcessor processor = (SQLProcessor) getGlobal(
1318:                                request, "sql-processor");
1319:                        AnalysisEngine aEngine = new AnalysisEngine(processor);
1320:                        AnalysisDocument doc = aEngine
1321:                                .getAnalysisDocument(dbFile.getId());
1322:                        if (doc != null) {
1323:                            doc.setGlossaryId(new Integer(glossaryId));
1324:                            aEngine.updateAnalysisDocument(doc);
1325:                        }
1326:                    }
1327:                }
1328:
1329:                String referrer = getReferrer(request);
1330:
1331:                if (referrer == null) {
1332:                    StringBuffer rb = new StringBuffer(
1333:                            (String) new RequestContext(request)
1334:                                    .get("context-path"));
1335:                    rb
1336:                            .append("/ef/xmenu/system/FileActions/getInfo/file/list.html");
1337:                    if (dbFile.getParent() != null) {
1338:                        rb.append("?ID=");
1339:                        rb.append(dbFile.getParent());
1340:                    } else if (dbFile.getOwnerId() != null
1341:                            && dbFile.getOwnerType() != null) {
1342:                        rb.append("?OWNER_TYPE=");
1343:                        rb.append(dbFile.getOwnerType());
1344:                        rb.append("&OWNER_ID=");
1345:                        rb.append(dbFile.getOwnerId());
1346:                    }
1347:                    referrer = rb.toString();
1348:                }
1349:
1350:                response.sendRedirect(referrer);
1351:            }
1352:
1353:            /**
1354:             * Returns file record with content as expanded text and with terms attached.
1355:             */
1356:            public Object viewAnalysisDocument(HttpServletRequest request,
1357:                    final HttpServletResponse response) throws SQLException {
1358:                String fileId = request.getParameter("ID");
1359:                if (fileId == null) {
1360:                    return "ID parameter is missing";
1361:                }
1362:                return viewAnalysisDocument(Integer.parseInt(fileId), request,
1363:                        response, false);
1364:            }
1365:
1366:            /**
1367:             * Returns file record with content as expanded text and with terms attached.
1368:             */
1369:            private Object viewAnalysisDocument(int id,
1370:                    HttpServletRequest request,
1371:                    final HttpServletResponse response, boolean isReadOnlyView)
1372:                    throws SQLException {
1373:                DbFileEx ret = (DbFileEx) getEngine(request).getDbFile(id,
1374:                        DbFileEx.class);
1375:                if (ret == null) {
1376:                    return "Invalid file ID";
1377:                }
1378:
1379:                SQLProcessor processor = (SQLProcessor) getGlobal(request,
1380:                        "sql-processor");
1381:                AnalysisEngine aEngine = new AnalysisEngine(processor);
1382:                AnalysisDocument ad = aEngine.getAnalysisDocument(ret.getId());
1383:                if (ad != null) {
1384:                    ret.setAttribute("info", ad);
1385:                }
1386:
1387:                if (ret.getFileContent() != null) {
1388:                    if (ad == null || ad.getGlossaryId() == null) {
1389:                        ((DbFileImpl) ret).setColumnAttribute("FILE_CONTENT",
1390:                                "as-text", new String(ret.getFileContent()));
1391:                    } else {
1392:                        AnalysisTermSource ats = new AnalysisTermSource(ad
1393:                                .getGlossaryId().intValue(), processor, null,
1394:                                false, isReadOnlyView);
1395:                        AnalysisParser ap = new AnalysisParser(ats);
1396:                        String text = new String(ret.getFileContent());
1397:                        ((DbFileImpl) ret).setColumnAttribute("FILE_CONTENT",
1398:                                "as-text", ap.parse(text, false));
1399:                    }
1400:                    ret.setFileContent(null);
1401:                }
1402:
1403:                String referrer = getReferrer(request);
1404:                if (referrer != null) {
1405:                    ret.setAttribute("referrer", referrer);
1406:                }
1407:                return ret;
1408:            }
1409:
1410:            /**
1411:             * Returns file record with content as expanded text and with terms attached. 
1412:             * This method is for read-only view rendering 
1413:             * @throws ServletException 
1414:             * @throws FactoryConfigurationError 
1415:             * @throws ParserConfigurationException 
1416:             */
1417:            public Object viewAnalysisDocumentRo(HttpServletRequest request,
1418:                    final HttpServletResponse response,
1419:                    DispatchingServlet servlet, String path)
1420:                    throws SQLException, ParserConfigurationException,
1421:                    FactoryConfigurationError, ServletException {
1422:                if (isBlank(path)) {
1423:                    return "Invalid file path";
1424:                }
1425:
1426:                int idx = path.indexOf("/");
1427:                if (idx < 1) {
1428:                    return "Invalid file path";
1429:                }
1430:
1431:                Object ret = viewAnalysisDocument(Integer.parseInt(path
1432:                        .substring(0, idx)), request, response, true);
1433:                if (ret instanceof  DbFileEx) {
1434:                    ((DbFileEx) ret).setAttribute("read-only", "yes");
1435:                    servlet.style(ret, "analysis/viewAnalysisDocument.html",
1436:                            request, response);
1437:                    return null;
1438:                }
1439:
1440:                return ret;
1441:            }
1442:
1443:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.