Source Code Cross Referenced for StyleManagerImpl.java in  » ERP-CRM-Financial » sakai » org » theospi » portfolio » style » impl » 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 » ERP CRM Financial » sakai » org.theospi.portfolio.style.impl 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /**********************************************************************************
002:         * $URL:https://source.sakaiproject.org/svn/osp/trunk/common/api-impl/src/java/org/theospi/portfolio/style/impl/StyleManagerImpl.java $
003:         * $Id:StyleManagerImpl.java 9134 2006-05-08 20:28:42Z chmaurer@iupui.edu $
004:         ***********************************************************************************
005:         *
006:         * Copyright (c) 2006 The Sakai Foundation.
007:         *
008:         * Licensed under the Educational Community License, Version 1.0 (the "License");
009:         * you may not use this file except in compliance with the License.
010:         * You may obtain a copy of the License at
011:         *
012:         *      http://www.opensource.org/licenses/ecl1.php
013:         *
014:         * Unless required by applicable law or agreed to in writing, software
015:         * distributed under the License is distributed on an "AS IS" BASIS,
016:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017:         * See the License for the specific language governing permissions and
018:         * limitations under the License.
019:         *
020:         **********************************************************************************/package org.theospi.portfolio.style.impl;
021:
022:        import java.io.BufferedInputStream;
023:        import java.io.BufferedOutputStream;
024:        import java.io.ByteArrayInputStream;
025:        import java.io.ByteArrayOutputStream;
026:        import java.io.File;
027:        import java.io.IOException;
028:        import java.io.InputStream;
029:        import java.io.OutputStream;
030:        import java.sql.SQLException;
031:        import java.util.ArrayList;
032:        import java.util.Collection;
033:        import java.util.Date;
034:        import java.util.Hashtable;
035:        import java.util.Iterator;
036:        import java.util.List;
037:        import java.util.Map;
038:        import java.util.Set;
039:        import java.util.zip.Adler32;
040:        import java.util.zip.CheckedOutputStream;
041:        import java.util.zip.ZipEntry;
042:        import java.util.zip.ZipInputStream;
043:        import java.util.zip.ZipOutputStream;
044:
045:        import org.hibernate.HibernateException;
046:        import org.hibernate.Session;
047:        import org.jdom.CDATA;
048:        import org.jdom.Document;
049:        import org.jdom.Element;
050:        import org.jdom.JDOMException;
051:        import org.jdom.input.SAXBuilder;
052:        import org.jdom.output.XMLOutputter;
053:        import org.sakaiproject.authz.api.SecurityService;
054:        import org.sakaiproject.content.api.ContentCollection;
055:        import org.sakaiproject.content.api.ContentHostingService;
056:        import org.sakaiproject.content.api.ContentResource;
057:        import org.sakaiproject.content.api.ContentResourceEdit;
058:        import org.sakaiproject.content.api.LockManager;
059:        import org.sakaiproject.entity.api.Reference;
060:        import org.sakaiproject.entity.api.ResourceProperties;
061:        import org.sakaiproject.entity.api.ResourcePropertiesEdit;
062:        import org.sakaiproject.exception.IdUnusedException;
063:        import org.sakaiproject.exception.PermissionException;
064:        import org.sakaiproject.exception.ServerOverloadException;
065:        import org.sakaiproject.exception.TypeException;
066:        import org.sakaiproject.metaobj.security.AuthenticationManager;
067:        import org.sakaiproject.metaobj.shared.mgt.AgentManager;
068:        import org.sakaiproject.metaobj.shared.mgt.IdManager;
069:        import org.sakaiproject.metaobj.shared.model.Agent;
070:        import org.sakaiproject.metaobj.shared.model.Id;
071:        import org.sakaiproject.metaobj.shared.model.MimeType;
072:        import org.sakaiproject.metaobj.shared.model.StructuredArtifactDefinitionBean;
073:        import org.sakaiproject.metaobj.worksite.mgt.WorksiteManager;
074:        import org.sakaiproject.site.api.Site;
075:        import org.sakaiproject.site.api.SiteService.SelectionType;
076:        import org.sakaiproject.site.cover.SiteService;
077:        import org.springframework.orm.hibernate3.HibernateCallback;
078:        import org.springframework.orm.hibernate3.HibernateObjectRetrievalFailureException;
079:        import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
080:        import org.theospi.portfolio.security.AllowMapSecurityAdvisor;
081:        import org.theospi.portfolio.security.AuthorizationFacade;
082:        import org.theospi.portfolio.shared.model.Node;
083:        import org.theospi.portfolio.style.StyleConsumer;
084:        import org.theospi.portfolio.style.StyleFunctionConstants;
085:        import org.theospi.portfolio.style.mgt.StyleManager;
086:        import org.theospi.portfolio.style.model.Style;
087:
088:        public class StyleManagerImpl extends HibernateDaoSupport implements 
089:                StyleManager {
090:
091:            private AuthorizationFacade authzManager = null;
092:            private IdManager idManager = null;
093:            private WorksiteManager worksiteManager;
094:            private AuthenticationManager authnManager = null;
095:            private LockManager lockManager;
096:            private ContentHostingService contentHosting = null;
097:            private AgentManager agentManager;
098:            private SecurityService securityService = null;
099:            private List globalSites;
100:            private List globalSiteTypes;
101:            private List consumers;
102:            private boolean autoDdl = false;
103:
104:            public Style storeStyle(Style style) {
105:                return storeStyle(style, true);
106:            }
107:
108:            public Style storeStyle(Style style, boolean checkAuthz) {
109:                updateFields(style, checkAuthz);
110:                getHibernateTemplate().saveOrUpdate(style);
111:                lockStyleFiles(style);
112:
113:                return style;
114:            }
115:
116:            public Style mergeStyle(Style style) {
117:                return mergeStyle(style, true);
118:            }
119:
120:            public Style mergeStyle(Style style, boolean checkAuthz) {
121:                if (style.getId() == null) {
122:                    return storeStyle(style, checkAuthz);
123:                }
124:                updateFields(style, checkAuthz);
125:                getHibernateTemplate().merge(style);
126:                lockStyleFiles(style);
127:
128:                return style;
129:            }
130:
131:            protected void updateFields(Style style, boolean checkAuthz) {
132:                style.setModified(new Date(System.currentTimeMillis()));
133:                style.setStyleHash(calculateStyleHash(style));
134:
135:                boolean newStyle = (style.getId() == null);
136:
137:                if (newStyle) {
138:                    style.setCreated(new Date(System.currentTimeMillis()));
139:
140:                    if (checkAuthz) {
141:                        getAuthzManager().checkPermission(
142:                                StyleFunctionConstants.CREATE_STYLE,
143:                                getIdManager().getId(style.getSiteId()));
144:                    }
145:                } else {
146:                    if (checkAuthz) {
147:                        getAuthzManager().checkPermission(
148:                                StyleFunctionConstants.EDIT_STYLE,
149:                                style.getId());
150:                    }
151:                }
152:            }
153:
154:            protected void lockStyleFiles(Style style) {
155:                getLockManager().removeAllLocks(style.getId().getValue());
156:                getLockManager().lockObject(style.getStyleFile().getValue(),
157:                        style.getId().getValue(), "saving a style", true);
158:
159:            }
160:
161:            public Style getStyle(Id styleId) {
162:                return (Style) getHibernateTemplate().get(Style.class, styleId);
163:            }
164:
165:            public Style getLightWeightStyle(final Id styleId) {
166:                HibernateCallback callback = new HibernateCallback() {
167:
168:                    public Object doInHibernate(Session session)
169:                            throws HibernateException, SQLException {
170:                        Style style = (Style) session.get(Style.class, styleId);
171:                        return style;
172:                    }
173:
174:                };
175:
176:                try {
177:                    Style style = (Style) getHibernateTemplate().execute(
178:                            callback);
179:                    return style;
180:                } catch (HibernateObjectRetrievalFailureException e) {
181:                    logger.debug(e);
182:                    return null;
183:                }
184:            }
185:
186:            public Collection findSiteStyles(String currentWorksiteId) {
187:                Object[] params = new Object[] { currentWorksiteId,
188:                        getAuthnManager().getAgent() };
189:                return getHibernateTemplate().findByNamedQuery(
190:                        "findSiteStyles", params);
191:            }
192:
193:            public Collection findPublishedStyles(String currentWorksiteId) {
194:                Object[] params = new Object[] {
195:                        new Integer(Style.STATE_PUBLISHED), currentWorksiteId,
196:                        getAuthnManager().getAgent() };
197:                return getHibernateTemplate().findByNamedQuery(
198:                        "findPublishedStyles", params);
199:            }
200:
201:            protected String buildGlobalSiteList() {
202:                String query = "(";
203:
204:                for (Iterator i = getGlobalSites().iterator(); i.hasNext();) {
205:                    String site = (String) i.next();
206:                    query += "'" + site + "',";
207:                }
208:
209:                for (Iterator j = getGlobalSiteTypes().iterator(); j.hasNext();) {
210:                    String type = (String) j.next();
211:                    List sites = SiteService.getSites(SelectionType.ANY, type,
212:                            null, null, null, null);
213:                    for (Iterator k = sites.iterator(); k.hasNext();) {
214:                        Site theSite = (Site) k.next();
215:                        query += "'" + theSite.getId() + "',";
216:                    }
217:                }
218:
219:                query += "'')";
220:
221:                return query;
222:            }
223:
224:            public Collection findGlobalStyles(Agent agent) {
225:                String query = "from Style s where ((s.siteId in "
226:                        + buildGlobalSiteList()
227:                        + " and (s.globalState = ? or s.owner = ?)) or s.globalState = 1)";
228:                Object[] params = new Object[] {
229:                        new Integer(Style.STATE_PUBLISHED), agent };
230:                return getHibernateTemplate().find(query, params);
231:            }
232:
233:            public boolean isGlobal() {
234:                String siteId = getWorksiteManager().getCurrentWorksiteId()
235:                        .getValue();
236:
237:                if (getGlobalSites().contains(siteId)) {
238:                    return true;
239:                }
240:
241:                Site site = getWorksiteManager().getSite(siteId);
242:                if (site.getType() != null
243:                        && getGlobalSiteTypes().contains(site.getType())) {
244:                    return true;
245:                }
246:
247:                return false;
248:            }
249:
250:            public Node getNode(Id artifactId) {
251:                String id = getContentHosting().resolveUuid(
252:                        artifactId.getValue());
253:                if (id == null) {
254:                    return null;
255:                }
256:
257:                try {
258:                    String ref = getContentHosting().getReference(id);
259:                    getSecurityService().pushAdvisor(
260:                            new AllowMapSecurityAdvisor(
261:                                    ContentHostingService.EVENT_RESOURCE_READ,
262:                                    ref));
263:                    ContentResource resource = getContentHosting().getResource(
264:                            id);
265:                    String ownerId = resource.getProperties().getProperty(
266:                            resource.getProperties().getNamePropCreator());
267:                    Agent owner = getAgentManager().getAgent(
268:                            getIdManager().getId(ownerId));
269:                    return new Node(artifactId, resource, owner);
270:                } catch (PermissionException e) {
271:                    logger.error("", e);
272:                    throw new RuntimeException(e);
273:                } catch (IdUnusedException e) {
274:                    logger.error("", e);
275:                    throw new RuntimeException(e);
276:                } catch (TypeException e) {
277:                    logger.error("", e);
278:                    throw new RuntimeException(e);
279:                }
280:            }
281:
282:            public Node getNode(Reference ref) {
283:                String nodeId = getContentHosting().getUuid(ref.getId());
284:
285:                return getNode(getIdManager().getId(nodeId));
286:            }
287:
288:            public boolean deleteStyle(final Id styleId) {
289:                Style style = getStyle(styleId);
290:                getAuthzManager().checkPermission(
291:                        StyleFunctionConstants.DELETE_STYLE, style.getId());
292:
293:                // handle things that are using this style
294:                if (!checkStyleConsumption(styleId)) {
295:
296:                    getLockManager().removeAllLocks(styleId.getValue());
297:
298:                    getHibernateTemplate().delete(style);
299:                } else {
300:                    return false;
301:                }
302:                // If we get here, I think it was deleted
303:                return true;
304:            }
305:
306:            protected boolean checkStyleConsumption(Id styleId) {
307:                for (Iterator i = getConsumers().iterator(); i.hasNext();) {
308:                    StyleConsumer sc = (StyleConsumer) i.next();
309:                    if (sc.checkStyleConsumption(styleId)) {
310:                        return true;
311:                    }
312:                }
313:                return false;
314:            }
315:
316:            public List getConsumers() {
317:                return this .consumers;
318:            }
319:
320:            public void setConsumers(List consumers) {
321:                this .consumers = consumers;
322:            }
323:
324:            public Collection getStylesForWarehouse() {
325:                return getHibernateTemplate().findByNamedQuery("findStyles");
326:            }
327:
328:            public void packageStyleForExport(Set styleIds, OutputStream os)
329:                    throws IOException {
330:                CheckedOutputStream checksum = new CheckedOutputStream(os,
331:                        new Adler32());
332:                ZipOutputStream zos = new ZipOutputStream(
333:                        new BufferedOutputStream(checksum));
334:                List exportedRefs = new ArrayList();
335:                for (Iterator i = styleIds.iterator(); i.hasNext();) {
336:                    String id = (String) i.next();
337:                    processStyle(id, zos, exportedRefs);
338:                }
339:
340:                zos.finish();
341:                zos.flush();
342:            }
343:
344:            protected void processStyle(String styleId, ZipOutputStream zos,
345:                    List exportedRefs) throws IOException {
346:                Style style = getStyle(getIdManager().getId(styleId));
347:
348:                Node node = getNode(style.getStyleFile());
349:
350:                try {
351:                    processStyleFile(zos, node);
352:                } catch (ServerOverloadException e) {
353:                    throw new RuntimeException(e);
354:                }
355:
356:                ZipEntry definitionFile = new ZipEntry("style-" + styleId
357:                        + ".xml");
358:                zos.putNextEntry(definitionFile);
359:                Document doc = createStyleAsXml(style);
360:                String docStr = (new XMLOutputter()).outputString(doc);
361:                zos.write(docStr.getBytes("UTF-8"));
362:                zos.closeEntry();
363:            }
364:
365:            protected void processStyleFile(ZipOutputStream zos, Node node)
366:                    throws ServerOverloadException, IOException {
367:                ContentResource entity = node.getResource();
368:
369:                String newName = entity.getProperties().getProperty(
370:                        entity.getProperties().getNamePropDisplayName());
371:                String cleanedName = newName.substring(newName
372:                        .lastIndexOf('\\') + 1);
373:                String entryName = "attachments/" + entity.getContentType()
374:                        + "/" + getNodeHash(node) + "/" + cleanedName;
375:
376:                storeFileInZip(zos, entity.streamContent(), entryName);
377:            }
378:
379:            protected void storeFileInZip(ZipOutputStream zos, InputStream in,
380:                    String entryName) throws IOException {
381:
382:                byte data[] = new byte[1024 * 10];
383:
384:                if (File.separatorChar == '\\') {
385:                    entryName = entryName.replace('\\', '/');
386:                }
387:
388:                ZipEntry newfileEntry = new ZipEntry(entryName);
389:
390:                zos.putNextEntry(newfileEntry);
391:
392:                BufferedInputStream origin = new BufferedInputStream(in,
393:                        data.length);
394:
395:                int count;
396:                while ((count = origin.read(data, 0, data.length)) != -1) {
397:                    zos.write(data, 0, count);
398:                }
399:                zos.closeEntry();
400:                in.close();
401:            }
402:
403:            protected Document createStyleAsXml(Style style) {
404:                Element rootNode = new Element("style");
405:
406:                rootNode.setAttribute("formatVersion", "2.1");
407:                addNode(rootNode, "id", style.getId().getValue());
408:                addNode(rootNode, "name", style.getName());
409:                addNode(rootNode, "description", style.getDescription());
410:                addNode(rootNode, "owner", style.getOwner().getId().getValue());
411:                addNode(rootNode, "siteId", style.getSiteId());
412:
413:                Node styleFileNode = getNode(style.getStyleFile());
414:
415:                addNode(rootNode, "nodeRef", styleFileNode.getResource()
416:                        .getReference());
417:
418:                Element items = new Element("items");
419:
420:                rootNode.addContent(items);
421:
422:                return new Document(rootNode);
423:            }
424:
425:            protected void addNode(Element parentNode, String name, String value) {
426:                Element attrNode = new Element(name);
427:                attrNode.addContent(new CDATA(value));
428:                parentNode.addContent(attrNode);
429:            }
430:
431:            protected int getNodeHash(Node node) {
432:                return node.getResource().getReference().hashCode();
433:            }
434:
435:            /**
436:             * @param parent the parent resource folder where attachments go
437:             * @param siteId the site which will recieve the imported "stuff"
438:             * @param in  The Input stream representing the output stream of the export
439:             * @return Map contains a map with keys being of type String as old Ids and the 
440:             * values as being the Style object
441:             */
442:            public Map importStyleList(ContentCollection parent, String siteId,
443:                    InputStream in) throws IOException {
444:                Map styleMap = new Hashtable();
445:                ZipInputStream zis = new ZipInputStream(in);
446:                ZipEntry currentEntry = zis.getNextEntry();
447:
448:                Map attachmentMap = new Hashtable();
449:
450:                while (currentEntry != null) {
451:                    if (!currentEntry.isDirectory()) {
452:                        if (currentEntry.getName().startsWith("style-")) {
453:                            importStyle(siteId, zis, styleMap);
454:                        } else if (currentEntry.getName().startsWith(
455:                                "attachments/")) {
456:                            importAttachmentRef(parent, currentEntry, siteId,
457:                                    zis, attachmentMap);
458:                        }
459:                    }
460:
461:                    zis.closeEntry();
462:                    currentEntry = zis.getNextEntry();
463:                }
464:
465:                postPocessAttachments(styleMap.values(), attachmentMap);
466:
467:                for (Iterator i = styleMap.keySet().iterator(); i.hasNext();) {
468:                    String key = (String) i.next();
469:                    Style style = (Style) styleMap.get(key);
470:                    Style found = findMatchingStyle(style);
471:                    if (found == null) {
472:                        storeStyle(style, false);
473:                    } else {
474:                        removeFromSession(style);
475:                        removeFromSession(found);
476:                        styleMap.put(key, found);
477:                    }
478:                }
479:
480:                return styleMap;
481:            }
482:
483:            /**
484:             * given a stream this reads in an xml style and places the new style into the map
485:             * @param siteId
486:             * @param is
487:             * @param styleMap
488:             * @throws IOException
489:             */
490:            protected void importStyle(String siteId, InputStream is,
491:                    Map styleMap) throws IOException {
492:                SAXBuilder builder = new SAXBuilder();
493:                Document document = null;
494:
495:                byte[] bytes = readStreamToBytes(is);
496:
497:                try {
498:                    document = builder.build(new ByteArrayInputStream(bytes));
499:                } catch (JDOMException e) {
500:                    throw new RuntimeException(e);
501:                }
502:                Element docRoot = document.getRootElement();
503:
504:                Id oldStyleId = getIdManager()
505:                        .getId(docRoot.getChildText("id"));
506:                String name = docRoot.getChildText("name");
507:                String description = docRoot.getChildText("description");
508:                String nodeRef = docRoot.getChildTextNormalize("nodeRef");
509:
510:                Style style = new Style();
511:                //style.setId(getIdManager().createId());
512:                style.setName(name);
513:                style.setDescription(description);
514:                style.setOwner(getAuthnManager().getAgent());
515:                style.setSiteId(siteId);
516:                style.setCreated(new Date(System.currentTimeMillis()));
517:                style.setModified(style.getCreated());
518:                style.setGlobalState(Style.STATE_UNPUBLISHED);
519:                style.setNodeRef(nodeRef);
520:
521:                styleMap.put(oldStyleId.getValue(), style);
522:            }
523:
524:            protected byte[] readStreamToBytes(InputStream inStream)
525:                    throws IOException {
526:                ByteArrayOutputStream bytes = new ByteArrayOutputStream();
527:                byte data[] = new byte[10 * 1024];
528:
529:                int count;
530:                while ((count = inStream.read(data, 0, 10 * 1024)) != -1) {
531:                    bytes.write(data, 0, count);
532:                }
533:                byte[] tmp = bytes.toByteArray();
534:                bytes.close();
535:                return tmp;
536:            }
537:
538:            protected void importAttachmentRef(ContentCollection fileParent,
539:                    ZipEntry currentEntry, String siteId, ZipInputStream zis,
540:                    Map attachmentMap) {
541:                File file = new File(currentEntry.getName());
542:
543:                MimeType mimeType = new MimeType(file.getParentFile()
544:                        .getParentFile().getParentFile().getName(), file
545:                        .getParentFile().getParentFile().getName());
546:
547:                String contentType = mimeType.getValue();
548:
549:                String oldId = file.getParentFile().getName();
550:
551:                try {
552:                    ByteArrayOutputStream bos = new ByteArrayOutputStream();
553:                    int c = zis.read();
554:
555:                    while (c != -1) {
556:                        bos.write(c);
557:                        c = zis.read();
558:                    }
559:
560:                    String fileId = fileParent.getId() + file.getName();
561:                    ContentResource rez = null;
562:                    try {
563:                        rez = getContentHosting().getResource(fileId);
564:                    } catch (IdUnusedException iduue) {
565:                        logger.info(iduue);
566:                    }
567:                    if (rez == null) {
568:                        ContentResourceEdit resource = getContentHosting()
569:                                .addResource(fileId);
570:                        ResourcePropertiesEdit resourceProperties = resource
571:                                .getPropertiesEdit();
572:                        resourceProperties.addProperty(
573:                                ResourceProperties.PROP_DISPLAY_NAME, file
574:                                        .getName());
575:                        resource.setContent(bos.toByteArray());
576:                        resource.setContentType(contentType);
577:                        getContentHosting().commitResource(resource);
578:                        rez = resource;
579:                    }
580:                    attachmentMap.put(oldId, rez.getId());
581:                } catch (Exception exp) {
582:                    throw new RuntimeException(exp);
583:                }
584:            }
585:
586:            protected void postPocessAttachments(Collection styles,
587:                    Map attachmentMap) {
588:                for (Iterator i = styles.iterator(); i.hasNext();) {
589:                    Style style = (Style) i.next();
590:                    postProcessStyleFile(style, attachmentMap);
591:                }
592:            }
593:
594:            protected void postProcessStyleFile(Style style, Map attachmentMap) {
595:                int nodeHash = style.getNodeRef().hashCode();
596:                String id = (String) attachmentMap.get("" + nodeHash);
597:
598:                if (id != null) {
599:                    String nodeId = getContentHosting().getUuid(id);
600:                    style.setStyleFile(getIdManager().getId(nodeId));
601:                    style.setStyleHash(calculateStyleHash(style));
602:                } else {
603:                    style.setStyleFile(null);
604:                }
605:            }
606:
607:            /**
608:             * 
609:             * @param style
610:             * @return The calculated hash string using the Style file's content (the css file)
611:             */
612:            protected String calculateStyleHash(Style style) {
613:                String hashString = "";
614:                try {
615:                    Node styleFileNode = getNode(style.getStyleFile());
616:
617:                    if (styleFileNode != null) {
618:                        hashString += new String(styleFileNode.getResource()
619:                                .getContent());
620:                        //hashString += Integer.toString(styleFileNode.getInputStream().hashCode());
621:                    }
622:                } catch (ServerOverloadException e) {
623:                    return null;
624:                }
625:                return hashString.hashCode() + "";
626:            }
627:
628:            protected void updateStyleHash() {
629:                List styles = getHibernateTemplate().findByNamedQuery(
630:                        "findByNullStyleHash");
631:
632:                for (Iterator i = styles.iterator(); i.hasNext();) {
633:                    Style style = (Style) i.next();
634:                    style.setStyleHash(calculateStyleHash(style));
635:                    getHibernateTemplate().saveOrUpdate(style);
636:                }
637:            }
638:
639:            public void init() {
640:                logger.info("init()");
641:
642:                if (isAutoDdl()) {
643:                    try {
644:                        updateStyleHash();
645:                    } catch (Exception e) {
646:                        logger.error("Error in StyleManager.init()", e);
647:                    }
648:                }
649:            }
650:
651:            /**
652:             * 
653:             * @param style
654:             * @return Returns the first Style object it finds that is either globally published,
655:             *  or in the same site and also has the same styleHash value.
656:             */
657:            protected Style findMatchingStyle(Style style) {
658:                Object[] params = new Object[] {
659:                        new Integer(Style.STATE_PUBLISHED), style.getSiteId(),
660:                        style.getStyleHash() };
661:                List styles = getHibernateTemplate().findByNamedQuery(
662:                        "findMatchingStyle", params);
663:
664:                if (styles.size() > 0) {
665:                    return (Style) styles.get(0);
666:                }
667:                return null;
668:            }
669:
670:            public void removeFromSession(Object obj) {
671:                this .getHibernateTemplate().evict(obj);
672:                try {
673:                    getHibernateTemplate().getSessionFactory().evict(
674:                            obj.getClass());
675:                } catch (HibernateException e) {
676:                    logger.error(e);
677:                }
678:            }
679:
680:            public AuthenticationManager getAuthnManager() {
681:                return authnManager;
682:            }
683:
684:            public void setAuthnManager(AuthenticationManager authnManager) {
685:                this .authnManager = authnManager;
686:            }
687:
688:            public AuthorizationFacade getAuthzManager() {
689:                return authzManager;
690:            }
691:
692:            public void setAuthzManager(AuthorizationFacade authzManager) {
693:                this .authzManager = authzManager;
694:            }
695:
696:            public List getGlobalSites() {
697:                return globalSites;
698:            }
699:
700:            public void setGlobalSites(List globalSites) {
701:                this .globalSites = globalSites;
702:            }
703:
704:            public List getGlobalSiteTypes() {
705:                return globalSiteTypes;
706:            }
707:
708:            public void setGlobalSiteTypes(List globalSiteTypes) {
709:                this .globalSiteTypes = globalSiteTypes;
710:            }
711:
712:            public IdManager getIdManager() {
713:                return idManager;
714:            }
715:
716:            public void setIdManager(IdManager idManager) {
717:                this .idManager = idManager;
718:            }
719:
720:            public WorksiteManager getWorksiteManager() {
721:                return worksiteManager;
722:            }
723:
724:            public void setWorksiteManager(WorksiteManager worksiteManager) {
725:                this .worksiteManager = worksiteManager;
726:            }
727:
728:            public LockManager getLockManager() {
729:                return lockManager;
730:            }
731:
732:            public void setLockManager(LockManager lockManager) {
733:                this .lockManager = lockManager;
734:            }
735:
736:            public AgentManager getAgentManager() {
737:                return agentManager;
738:            }
739:
740:            public void setAgentManager(AgentManager agentManager) {
741:                this .agentManager = agentManager;
742:            }
743:
744:            public ContentHostingService getContentHosting() {
745:                return contentHosting;
746:            }
747:
748:            public void setContentHosting(ContentHostingService contentHosting) {
749:                this .contentHosting = contentHosting;
750:            }
751:
752:            /**
753:             * @return the securityService
754:             */
755:            public SecurityService getSecurityService() {
756:                return securityService;
757:            }
758:
759:            /**
760:             * @param securityService the securityService to set
761:             */
762:            public void setSecurityService(SecurityService securityService) {
763:                this .securityService = securityService;
764:            }
765:
766:            public boolean isAutoDdl() {
767:                return autoDdl;
768:            }
769:
770:            public void setAutoDdl(boolean autoDdl) {
771:                this.autoDdl = autoDdl;
772:            }
773:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.