001: /**
002: * Copyright (c) 2003-2007, David A. Czarnecki
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions are met:
007: *
008: * Redistributions of source code must retain the above copyright notice, this list of conditions and the
009: * following disclaimer.
010: * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
011: * following disclaimer in the documentation and/or other materials provided with the distribution.
012: * Neither the name of "David A. Czarnecki" and "blojsom" nor the names of its contributors may be used to
013: * endorse or promote products derived from this software without specific prior written permission.
014: * Products derived from this software may not be called "blojsom", nor may "blojsom" appear in their name,
015: * without prior written permission of David A. Czarnecki.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
018: * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
019: * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
020: * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
021: * EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
022: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
023: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
025: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
026: * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
027: * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
028: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
029: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
030: */package org.blojsom.upgrade;
031:
032: import org.apache.commons.io.FileUtils;
033: import org.apache.commons.logging.Log;
034: import org.apache.commons.logging.LogFactory;
035: import org.blojsom.blog.Blog;
036: import org.blojsom.blog.*;
037: import org.blojsom.blog.database.*;
038: import org.blojsom.fetcher.Fetcher;
039: import org.blojsom.fetcher.FetcherException;
040: import org.blojsom.plugin.common.ResponseConstants;
041: import org.blojsom.util.BlojsomConstants;
042: import org.blojsom.util.BlojsomMetaDataConstants;
043: import org.blojsom2.BlojsomException;
044: import org.blojsom2.blog.*;
045: import org.blojsom2.blog.Pingback;
046: import org.blojsom2.blog.Trackback;
047: import org.blojsom2.fetcher.BlojsomFetcher;
048: import org.blojsom2.fetcher.BlojsomFetcherException;
049: import org.blojsom2.util.BlojsomProperties;
050: import org.blojsom2.util.BlojsomUtils;
051: import org.springframework.beans.FatalBeanException;
052: import org.springframework.beans.InvalidPropertyException;
053:
054: import javax.servlet.ServletConfig;
055: import javax.servlet.ServletException;
056: import java.io.File;
057: import java.io.FileInputStream;
058: import java.io.IOException;
059: import java.io.InputStream;
060: import java.util.*;
061:
062: /**
063: * Utility class to migrate from blojsom 2 to blojsom 3
064: *
065: * @author David Czarnecki
066: * @version $Id: Blojsom2ToBlojsom3Utility.java,v 1.20 2007/01/17 02:35:21 czarneckid Exp $
067: * @since blojsom 3
068: */
069: public class Blojsom2ToBlojsom3Utility {
070:
071: private static Log _logger = LogFactory
072: .getLog(Blojsom2ToBlojsom3Utility.class);
073:
074: private ServletConfig _servletConfig;
075: private Fetcher _fetcher;
076:
077: private BlojsomFetcher _blojsom2Fetcher;
078: private BlojsomConfiguration _blojsomConfiguration;
079:
080: private String _blojsom2Path;
081: private String _blojsom3Path;
082:
083: /**
084: * Construct a new instance of the blojsom 2 to blojsom 3 utility
085: */
086: public Blojsom2ToBlojsom3Utility() {
087: }
088:
089: /**
090: * Set the path to the blojsom 2 installation directory
091: *
092: * @param blojsom2Path blojsom 2 installation directory
093: */
094: public void setBlojsom2Path(String blojsom2Path) {
095: _blojsom2Path = blojsom2Path;
096: }
097:
098: /**
099: * Set the path to the blojsom 3 installation directory
100: *
101: * @param blojsom3Path blojsom 3 installation directory
102: */
103: public void setBlojsom3Path(String blojsom3Path) {
104: _blojsom3Path = blojsom3Path;
105: }
106:
107: /**
108: * Set the {@link Fetcher}
109: *
110: * @param fetcher {@link Fetcher}
111: */
112: public void setFetcher(Fetcher fetcher) {
113: _fetcher = fetcher;
114: }
115:
116: /**
117: * Set the {@link ServletConfig}
118: *
119: * @param servletConfig {@link ServletConfig}
120: */
121: public void setServletConfig(ServletConfig servletConfig) {
122: _servletConfig = servletConfig;
123: }
124:
125: /**
126: * Configure the {@link BlojsomFetcher} that will be used to fetch categories and
127: * entries
128: *
129: * @param servletConfig Servlet configuration information
130: * @param blojsomConfiguration blojsom properties
131: * @throws javax.servlet.ServletException If the {@link BlojsomFetcher} class could not be loaded and/or initialized
132: */
133: protected void configureFetcher(ServletConfig servletConfig,
134: BlojsomConfiguration blojsomConfiguration)
135: throws ServletException {
136: String fetcherClassName = blojsomConfiguration
137: .getFetcherClass();
138: try {
139: Class fetcherClass = Class.forName(fetcherClassName);
140: _blojsom2Fetcher = (BlojsomFetcher) fetcherClass
141: .newInstance();
142: _blojsom2Fetcher.init(servletConfig, blojsomConfiguration);
143: _logger.info("Added blojsom fetcher: " + fetcherClassName);
144: } catch (ClassNotFoundException e) {
145: _logger.error(e);
146: throw new ServletException(e);
147: } catch (InstantiationException e) {
148: _logger.error(e);
149: throw new ServletException(e);
150: } catch (IllegalAccessException e) {
151: _logger.error(e);
152: throw new ServletException(e);
153: } catch (BlojsomFetcherException e) {
154: _logger.error(e);
155: throw new ServletException(e);
156: }
157: }
158:
159: /**
160: * Load the blojsom 2 configuration information
161: */
162: private void loadBlojsom2Configuration() {
163: String blojsomPropertiesPath = _blojsom2Path + "/"
164: + BlojsomConstants.DEFAULT_CONFIGURATION_BASE_DIRECTORY
165: + "/blojsom.properties";
166: BlojsomProperties _blojsomProperties;
167: try {
168: _blojsomProperties = new BlojsomProperties();
169: _blojsomProperties.load(new FileInputStream(
170: blojsomPropertiesPath));
171: } catch (IOException e) {
172: if (_logger.isErrorEnabled()) {
173: _logger.error(e);
174: }
175:
176: _blojsomProperties = null;
177: }
178:
179: if (_blojsomProperties == null) {
180: throw new FatalBeanException(
181: "Unable to load blojsom properties file: "
182: + blojsomPropertiesPath);
183: }
184:
185: try {
186: _blojsomConfiguration = new BlojsomConfiguration(
187: _servletConfig, org.blojsom.util.BlojsomUtils
188: .propertiesToMap(_blojsomProperties));
189: } catch (BlojsomConfigurationException e) {
190: if (_logger.isErrorEnabled()) {
191: _logger.error(e);
192: }
193: }
194:
195: if (_blojsomConfiguration == null) {
196: throw new FatalBeanException(
197: "Unable to construct blojsom configuration object");
198: }
199:
200: try {
201: configureFetcher(_servletConfig, _blojsomConfiguration);
202: } catch (ServletException e) {
203: if (_logger.isErrorEnabled()) {
204: _logger.error(e);
205: }
206:
207: throw new FatalBeanException(
208: "Unable to construct blojsom 2 fetcher object", e);
209: }
210: }
211:
212: /**
213: * Upgrade the blojsom 2 instance to blojsom 3
214: */
215: public void upgrade() {
216: if (org.blojsom.util.BlojsomUtils
217: .checkNullOrBlank(_blojsom2Path)) {
218: throw new InvalidPropertyException(
219: Blojsom2ToBlojsom3Utility.class, "blojsom2Path",
220: "blojsom2Path property was null or blank");
221: }
222:
223: if (org.blojsom.util.BlojsomUtils
224: .checkNullOrBlank(_blojsom3Path)) {
225: throw new InvalidPropertyException(
226: Blojsom2ToBlojsom3Utility.class, "blojsom3Path",
227: "blojsom3Path property was null or blank");
228: }
229:
230: if (_logger.isDebugEnabled()) {
231: _logger.debug("blojsom 2 path: " + _blojsom2Path);
232: _logger.debug("blojsom 3 path: " + _blojsom3Path);
233: }
234:
235: loadBlojsom2Configuration();
236:
237: // Migrate each blog
238: String[] blojsom2IDs = _blojsomConfiguration.getBlojsomUsers();
239: for (int i = 0; i < blojsom2IDs.length; i++) {
240: String blojsom2ID = blojsom2IDs[i];
241: Blog blog;
242:
243: // Try and load the blog in the blojsom 3 installation, otherwise, create a new blog
244: try {
245: blog = _fetcher.loadBlog(blojsom2ID);
246:
247: if (_logger.isDebugEnabled()) {
248: _logger.debug("Updating existing blog: "
249: + blojsom2ID);
250: }
251: } catch (FetcherException e) {
252: if (_logger.isDebugEnabled()) {
253: _logger.debug("Creating new blog: " + blojsom2ID);
254: }
255:
256: blog = new DatabaseBlog();
257: blog.setBlogId(blojsom2ID);
258: }
259:
260: BlogUser blogUser;
261: try {
262: blogUser = _blojsomConfiguration.loadBlog(blojsom2ID);
263: } catch (BlojsomException e) {
264: if (_logger.isErrorEnabled()) {
265: _logger.error("Unable to load blojsom 2 blog ID: "
266: + blojsom2ID);
267: _logger.error(e);
268: }
269:
270: continue;
271: }
272:
273: // Migrate the properties
274: Map blojsom2BlogProperties = blogUser.getBlog()
275: .getBlogProperties();
276: Properties blogProperties = org.blojsom2.util.BlojsomUtils
277: .mapToProperties(blojsom2BlogProperties);
278: // Remove unused blog properties in blojsom 3
279: blogProperties.remove("blog-home");
280: blogProperties.remove("blog-directory-depth");
281: blogProperties.remove("blog-file-extensions");
282: blogProperties.remove("blog-entry-meta-data-extension");
283: blogProperties.remove("blog-properties-extensions");
284: blogProperties.remove("blog-default-category-mapping");
285: blogProperties.remove("blog-comments-directory");
286: blogProperties.remove("blog-trackbacks-directory");
287: blogProperties.remove("blog-pingbacks-directory");
288: blogProperties.remove("blog-xmlrpc-entry-extension");
289: blogProperties.remove("blog-directory-filter");
290: blogProperties.remove("blog-blacklist-file");
291: blog.setProperties(org.blojsom.util.BlojsomUtils
292: .propertiesToMap(blogProperties));
293: // Sanitize some properties
294: String url = blog.getBlogURL();
295: url = org.blojsom.util.BlojsomUtils
296: .removeTrailingSlash(url);
297: blog.setBlogURL(url);
298:
299: url = blog.getBlogBaseURL();
300: url = org.blojsom.util.BlojsomUtils
301: .removeTrailingSlash(url);
302: blog.setBlogBaseURL(url);
303:
304: blog.setBlogBaseAdminURL(url);
305:
306: url = blog.getBlogAdminURL();
307: url = org.blojsom.util.BlojsomUtils
308: .removeTrailingSlash(url);
309: blog.setBlogAdminURL(url);
310:
311: if (BlojsomUtils.checkNullOrBlank(blog.getBlogAdminURL())
312: || BlojsomUtils.checkNullOrBlank(blog
313: .getBlogBaseAdminURL())
314: || BlojsomUtils.checkNullOrBlank(blog
315: .getBlogBaseURL())
316: || BlojsomUtils.checkNullOrBlank(blog.getBlogURL())) {
317: blog.setProperty(
318: BlojsomConstants.USE_DYNAMIC_BLOG_URLS, "true");
319: }
320:
321: // Migrate the plugin chains
322: Map blojsom2PluginChains = blogUser.getPluginChain();
323: Map blojsom3PluginChains = new HashMap();
324: Iterator pluginIterator = blojsom2PluginChains.keySet()
325: .iterator();
326: while (pluginIterator.hasNext()) {
327: String pluginChainForFlavor = (String) pluginIterator
328: .next();
329: String[] flavorForPluginChain = pluginChainForFlavor
330: .split("\\.");
331: if (flavorForPluginChain.length == 2) {
332: blojsom3PluginChains.put(flavorForPluginChain[0],
333: blojsom2PluginChains
334: .get(pluginChainForFlavor));
335: } else {
336: blojsom3PluginChains.put("default",
337: blojsom2PluginChains
338: .get(pluginChainForFlavor));
339: }
340: }
341: blog.setPlugins(blojsom3PluginChains);
342:
343: // Migrate the flavor and template mappings
344: Map blojsom2BlogFlavors = blogUser.getFlavors();
345: Map blojsom3FlavorToTemplate = new HashMap();
346: Iterator flavorIterator = blojsom2BlogFlavors.keySet()
347: .iterator();
348: while (flavorIterator.hasNext()) {
349: String flavor = (String) flavorIterator.next();
350: blojsom3FlavorToTemplate.put(flavor, blogUser
351: .getFlavorToTemplate().get(flavor).toString()
352: + ", "
353: + blogUser.getFlavorToContentType().get(flavor)
354: .toString());
355: }
356: blog.setTemplates(blojsom3FlavorToTemplate);
357:
358: // Save the blojsom 3 blog
359: try {
360: _fetcher.saveBlog(blog);
361: blog = _fetcher.loadBlog(blog.getBlogId());
362: } catch (FetcherException e) {
363: if (_logger.isErrorEnabled()) {
364: _logger.error(e);
365: }
366:
367: continue;
368: }
369:
370: // Migrate the users and permissions for each user
371: String blogPermissionsPath = _blojsom2Path
372: + "/"
373: + BlojsomConstants.DEFAULT_CONFIGURATION_BASE_DIRECTORY
374: + "/" + blogUser.getId()
375: + "/permissions.properties";
376: Map blojsom2PermissionsForBlog = new HashMap();
377: try {
378: InputStream is = new FileInputStream(
379: blogPermissionsPath);
380: BlojsomProperties permissions = new BlojsomProperties(
381: true);
382: permissions.load(is);
383: is.close();
384:
385: blojsom2PermissionsForBlog = org.blojsom2.util.BlojsomUtils
386: .propertiesToMap(permissions);
387:
388: if (_logger.isDebugEnabled()) {
389: _logger
390: .debug("Loaded permissions for blojsom 2 blog: "
391: + blogUser.getId());
392: }
393: } catch (IOException e) {
394: if (_logger.isErrorEnabled()) {
395: _logger.error(e);
396: }
397: }
398:
399: Map authorizationMap = blogUser.getBlog()
400: .getAuthorization();
401: Iterator blojsom2UserIterator = authorizationMap.keySet()
402: .iterator();
403: while (blojsom2UserIterator.hasNext()) {
404: String userID = (String) blojsom2UserIterator.next();
405: if (_logger.isDebugEnabled()) {
406: _logger
407: .debug("Migrating blojsom 2 user: "
408: + userID);
409: }
410:
411: User blojsom3User = new DatabaseUser();
412:
413: blojsom3User.setBlogId(blog.getId());
414: blojsom3User.setUserLogin(userID);
415: blojsom3User.setUserName(userID);
416: blojsom3User.setUserRegistered(new Date());
417: blojsom3User.setUserStatus("new");
418: String[] parsedPasswordAndEmail = BlojsomUtils
419: .parseLastComma((String) authorizationMap
420: .get(userID));
421: blojsom3User.setUserPassword(parsedPasswordAndEmail[0]);
422: if (parsedPasswordAndEmail.length == 2) {
423: blojsom3User
424: .setUserEmail(parsedPasswordAndEmail[1]);
425: } else {
426: blojsom3User.setUserEmail(blogUser.getBlog()
427: .getBlogOwnerEmail());
428: }
429:
430: Map blojsom3UserMetadata = new HashMap();
431:
432: if (blojsom2PermissionsForBlog.containsKey(userID)) {
433: Object permissionsForUser = blojsom2PermissionsForBlog
434: .get(userID);
435:
436: // Check where user has multiple permissions
437: if (permissionsForUser instanceof List) {
438: List permissions = (List) permissionsForUser;
439: for (int j = 0; j < permissions.size(); j++) {
440: String permission = (String) permissions
441: .get(j);
442: String updatedPermission;
443:
444: if ("*".equals(permission)) {
445: updatedPermission = "all_permissions_permission";
446: } else {
447: updatedPermission = permission
448: .replaceAll("-", "_")
449: + "_permission";
450: }
451:
452: blojsom3UserMetadata.put(updatedPermission,
453: "true");
454: }
455: // Check where user has only a single permission
456: } else {
457: if ("*".equals(permissionsForUser)) {
458: blojsom3UserMetadata.put(
459: "all_permissions_permission",
460: "true");
461: } else {
462: blojsom3UserMetadata.put(permissionsForUser
463: .toString().replaceAll("-", "_")
464: + "_permission", "true");
465: }
466: }
467: }
468:
469: blojsom3User.setMetaData(blojsom3UserMetadata);
470:
471: try {
472: _fetcher.saveUser(blog, blojsom3User);
473: } catch (FetcherException e) {
474: if (_logger.isErrorEnabled()) {
475: _logger.error(e);
476: }
477: }
478: }
479:
480: // Migrate the categories
481: Map blojsom2CategoriesMap = new HashMap();
482: Map blojsom3CategoriesMap = new HashMap();
483: try {
484: BlogCategory[] blojsom2Categories = _blojsom2Fetcher
485: .fetchCategories(null, blogUser);
486: for (int j = 0; j < blojsom2Categories.length; j++) {
487: BlogCategory blojsom2Category = blojsom2Categories[j];
488: Category blojsom3Category = new DatabaseCategory();
489: blojsom3Category.setBlogId(blog.getId());
490: if (!org.blojsom.util.BlojsomUtils
491: .checkNullOrBlank((String) blojsom2Category
492: .getMetaData()
493: .get(
494: org.blojsom2.util.BlojsomConstants.NAME_KEY))) {
495: blojsom3Category
496: .setDescription((String) blojsom2Category
497: .getMetaData()
498: .get(
499: org.blojsom2.util.BlojsomConstants.NAME_KEY));
500: } else if (org.blojsom.util.BlojsomUtils
501: .checkNullOrBlank(blojsom2Category
502: .getDescription())) {
503: blojsom3Category
504: .setDescription(blojsom2Category
505: .getEncodedCategory()
506: .replaceAll("/", " "));
507: } else {
508: blojsom3Category
509: .setDescription(blojsom2Category
510: .getDescription());
511: }
512: blojsom3Category.setName(blojsom2Category
513: .getEncodedCategory());
514:
515: String parents[] = blojsom2Category
516: .getEncodedCategory().split("/");
517: if (parents.length > 1) {
518: // Build the parent's name
519: String parentCategoryName = "/";
520: for (int k = 1; k < parents.length - 1; k++) {
521: parentCategoryName = parentCategoryName
522: + parents[k] + "/";
523: }
524:
525: try {
526: // Find the parent's id
527: Category blojsom3Categories[] = _fetcher
528: .loadAllCategories(blog);
529: for (int k = 0; k < blojsom3Categories.length; k++) {
530: if (parentCategoryName
531: .equals(blojsom3Categories[k]
532: .getName())) {
533: blojsom3Category
534: .setParentCategoryId(blojsom3Categories[k]
535: .getId());
536: }
537: }
538: } catch (FetcherException e) {
539: if (_logger.isErrorEnabled()) {
540: _logger.error(e);
541: }
542: }
543: } else {
544: blojsom3Category.setParentCategoryId(null);
545: }
546:
547: blojsom3Category.setMetaData(blojsom2Category
548: .getMetaData());
549:
550: try {
551: _fetcher.saveCategory(blog, blojsom3Category);
552: if (_logger.isDebugEnabled()) {
553: _logger
554: .debug("Created blojsom 3 category: "
555: + blojsom3Category
556: .getName());
557: }
558:
559: blojsom3Category = _fetcher.loadCategory(blog,
560: blojsom3Category.getName());
561: blojsom2CategoriesMap
562: .put(blojsom2Category
563: .getEncodedCategory(),
564: blojsom2Category);
565: blojsom3CategoriesMap
566: .put(blojsom2Category
567: .getEncodedCategory(),
568: blojsom3Category);
569: } catch (FetcherException e) {
570: if (_logger.isErrorEnabled()) {
571: _logger.error(e);
572: }
573: }
574: }
575: } catch (BlojsomFetcherException e) {
576: if (_logger.isErrorEnabled()) {
577: _logger.error(e);
578: }
579:
580: continue;
581: }
582:
583: // Migrate the entries
584: Iterator blojsom2CategoriesIterator = blojsom2CategoriesMap
585: .keySet().iterator();
586: while (blojsom2CategoriesIterator.hasNext()) {
587: String categoryName = (String) blojsom2CategoriesIterator
588: .next();
589: BlogCategory blogCategory = (BlogCategory) blojsom2CategoriesMap
590: .get(categoryName);
591:
592: Map fetchParameters = new HashMap();
593: fetchParameters.put(BlojsomFetcher.FETCHER_CATEGORY,
594: blogCategory);
595: fetchParameters.put(
596: BlojsomFetcher.FETCHER_NUM_POSTS_INTEGER,
597: new Integer(-1));
598: try {
599: BlogEntry[] entries = _blojsom2Fetcher
600: .fetchEntries(fetchParameters, blogUser);
601:
602: if (_logger.isDebugEnabled()) {
603: _logger.debug("Migrating " + entries.length
604: + " entries from blojsom 2 category: "
605: + blogCategory.getEncodedCategory());
606: }
607:
608: for (int j = 0; j < entries.length; j++) {
609: BlogEntry entry = entries[j];
610: Entry blojsom3Entry = new DatabaseEntry();
611: blojsom3Entry.setBlogId(blog.getId());
612:
613: Category category = (Category) blojsom3CategoriesMap
614: .get(blogCategory.getEncodedCategory());
615: blojsom3Entry.setBlogCategoryId(category
616: .getId());
617: if (entry.getMetaData()
618: .get("blog-entry-author") != null) {
619: blojsom3Entry.setAuthor(entry.getMetaData()
620: .get("blog-entry-author")
621: .toString());
622: }
623:
624: if (entry.supportsComments()) {
625: blojsom3Entry.setAllowComments(new Integer(
626: 1));
627: } else {
628: blojsom3Entry.setAllowComments(new Integer(
629: 0));
630: }
631:
632: blojsom3Entry.setDate(entry.getDate());
633: blojsom3Entry.setDescription(entry
634: .getDescription());
635: blojsom3Entry.setMetaData(entry.getMetaData());
636: blojsom3Entry.setModifiedDate(entry.getDate());
637:
638: if (entry.supportsPingbacks()) {
639: blojsom3Entry
640: .setAllowPingbacks(new Integer(1));
641: } else {
642: blojsom3Entry
643: .setAllowPingbacks(new Integer(0));
644: }
645:
646: blojsom3Entry
647: .setPostSlug(org.blojsom.util.BlojsomUtils
648: .urlDecode(entry.getPermalink()));
649: blojsom3Entry
650: .setStatus(BlojsomMetaDataConstants.PUBLISHED_STATUS);
651: blojsom3Entry.setTitle(entry.getTitle());
652:
653: if (entry.supportsTrackbacks()) {
654: blojsom3Entry
655: .setAllowTrackbacks(new Integer(1));
656: } else {
657: blojsom3Entry
658: .setAllowTrackbacks(new Integer(0));
659: }
660:
661: try {
662: _fetcher.saveEntry(blog, blojsom3Entry);
663: } catch (FetcherException e) {
664: if (_logger.isErrorEnabled()) {
665: _logger.error(e);
666: }
667: }
668:
669: // Migrate the comments
670: BlogComment[] comments = entry
671: .getCommentsAsArray();
672: for (int k = 0; k < comments.length; k++) {
673: BlogComment comment = comments[k];
674: Comment blojsom3Comment = new DatabaseComment();
675:
676: blojsom3Comment.setAuthor(comment
677: .getAuthor());
678: blojsom3Comment.setAuthorEmail(comment
679: .getAuthorEmail());
680: blojsom3Comment.setAuthorURL(comment
681: .getAuthorURL());
682: blojsom3Comment
683: .setBlogEntryId(blojsom3Entry
684: .getId());
685: blojsom3Comment.setBlogId(blog.getId());
686: blojsom3Comment.setComment(comment
687: .getComment());
688: blojsom3Comment.setCommentDate(comment
689: .getCommentDate());
690: blojsom3Comment
691: .setIp((String) comment
692: .getMetaData()
693: .get(
694: "BLOJSOM_COMMENT_PLUGIN_METADATA_IP"));
695: blojsom3Comment.setMetaData(comment
696: .getMetaData());
697: blojsom3Comment
698: .setStatus(ResponseConstants.APPROVED_STATUS);
699:
700: try {
701: _fetcher.saveComment(blog,
702: blojsom3Comment);
703: } catch (FetcherException e) {
704: if (_logger.isErrorEnabled()) {
705: _logger.error(e);
706: }
707: }
708: }
709:
710: // Migrate the trackbacks
711: Trackback[] trackbacks = entry
712: .getTrackbacksAsArray();
713: for (int k = 0; k < trackbacks.length; k++) {
714: Trackback trackback = trackbacks[k];
715: DatabaseTrackback blojsom3Trackback = new DatabaseTrackback();
716:
717: blojsom3Trackback.setBlogName(trackback
718: .getBlogName());
719: blojsom3Trackback.setExcerpt(trackback
720: .getExcerpt());
721: blojsom3Trackback.setTitle(trackback
722: .getTitle());
723: blojsom3Trackback
724: .setUrl(trackback.getUrl());
725: blojsom3Trackback
726: .setTrackbackDate(trackback
727: .getTrackbackDate());
728: blojsom3Trackback
729: .setBlogEntryId(blojsom3Entry
730: .getId());
731: blojsom3Trackback.setBlogId(blog.getId());
732: blojsom3Trackback
733: .setIp((String) trackback
734: .getMetaData()
735: .get(
736: "BLOJSOM_TRACKBACK_PLUGIN_METADATA_IP"));
737: blojsom3Trackback.setMetaData(trackback
738: .getMetaData());
739: blojsom3Trackback
740: .setStatus(ResponseConstants.APPROVED_STATUS);
741:
742: try {
743: _fetcher.saveTrackback(blog,
744: blojsom3Trackback);
745: } catch (FetcherException e) {
746: if (_logger.isErrorEnabled()) {
747: _logger.error(e);
748: }
749: }
750: }
751:
752: // Migrate the pingbacks
753: Pingback[] pingbacks = entry
754: .getPingbacksAsArray();
755: for (int k = 0; k < pingbacks.length; k++) {
756: Pingback pingback = pingbacks[k];
757: DatabasePingback blojsom3Pingback = new DatabasePingback();
758:
759: blojsom3Pingback.setBlogName(pingback
760: .getBlogName());
761: blojsom3Pingback.setExcerpt(pingback
762: .getExcerpt());
763: blojsom3Pingback.setTitle(pingback
764: .getTitle());
765: blojsom3Pingback.setUrl(pingback.getUrl());
766: blojsom3Pingback.setTrackbackDate(pingback
767: .getTrackbackDate());
768: blojsom3Pingback
769: .setBlogEntryId(blojsom3Entry
770: .getId());
771: blojsom3Pingback.setBlogId(blog.getId());
772: blojsom3Pingback
773: .setIp((String) pingback
774: .getMetaData()
775: .get(
776: "BLOJSOM_PINGBACK_PLUGIN_METADATA_IP"));
777: blojsom3Pingback.setMetaData(pingback
778: .getMetaData());
779: blojsom3Pingback
780: .setStatus(ResponseConstants.APPROVED_STATUS);
781: blojsom3Pingback.setSourceURI(pingback
782: .getUrl());
783: blojsom3Pingback.setTargetURI(pingback
784: .getTitle());
785:
786: try {
787: _fetcher.savePingback(blog,
788: blojsom3Pingback);
789: } catch (FetcherException e) {
790: if (_logger.isErrorEnabled()) {
791: _logger.error(e);
792: }
793: }
794: }
795: }
796: } catch (BlojsomFetcherException e) {
797: if (_logger.isErrorEnabled()) {
798: _logger.error(e);
799: }
800: }
801: }
802:
803: // Migrate the resources and templates
804: File blojsom2BlogResourcesPath = new File(_blojsom2Path
805: + "/resources/" + blojsom2ID + "/");
806: File blojsom3BlogResourcesPath = new File(_blojsom3Path
807: + "/resources/" + blojsom2ID + "/");
808: try {
809: FileUtils.copyDirectory(blojsom2BlogResourcesPath,
810: blojsom3BlogResourcesPath);
811: if (_logger.isDebugEnabled()) {
812: _logger
813: .debug("Copied blojsom 2 blog resources from: "
814: + blojsom2BlogResourcesPath
815: .toString()
816: + " to: "
817: + blojsom3BlogResourcesPath
818: .toString());
819: }
820: } catch (IOException e) {
821: if (_logger.isErrorEnabled()) {
822: _logger.error(e);
823: }
824: }
825:
826: File blojsom2BlogPath = new File(
827: _blojsom2Path
828: + BlojsomConstants.DEFAULT_CONFIGURATION_BASE_DIRECTORY
829: + "/" + blojsom2ID + "/");
830: File blojsom3BlogPath = new File(
831: _blojsom3Path
832: + BlojsomConstants.DEFAULT_CONFIGURATION_BASE_DIRECTORY
833: + "/blogs/" + blojsom2ID + "/");
834: try {
835: FileUtils.copyDirectory(blojsom2BlogPath,
836: blojsom3BlogPath);
837: if (_logger.isDebugEnabled()) {
838: _logger.debug("Copied blojsom 2 blog data from: "
839: + blojsom2BlogPath.toString() + " to: "
840: + blojsom3BlogPath.toString());
841: }
842: } catch (IOException e) {
843: if (_logger.isErrorEnabled()) {
844: _logger.error(e);
845: }
846: }
847:
848: // Template method and variable substitution
849: File blojsom3TemplatePath = new File(blojsom3BlogPath,
850: "templates");
851: File[] templates = blojsom3TemplatePath.listFiles();
852: for (int j = 0; j < templates.length; j++) {
853: File template = templates[j];
854: try {
855: String templateData = FileUtils.readFileToString(
856: template, BlojsomConstants.UTF8);
857:
858: // Updated context variables
859: // "$BLOJSOM_USER" -> "$BLOJSOM_BLOG_ID"
860: templateData = templateData.replaceAll(
861: "\\$BLOJSOM_USER", "\\$BLOJSOM_BLOG_ID");
862:
863: // Updated method names
864: // "$entry.getPermalink()" -> "$entry.getPostSlug()"
865: templateData = templateData.replaceAll(
866: "\\$entry\\.getPermalink\\(\\s*\\)",
867: "\\$entry.getPostSlug()");
868: // "$BLOJSOM_REQUESTED_CATEGORY.getCategory()" -> "$BLOJSOM_REQUESTED_CATEGORY.getName()"
869: templateData = templateData
870: .replaceAll(
871: "\\$BLOJSOM_REQUESTED_CATEGORY\\.getCategory\\(\\s*\\)",
872: "\\$BLOJSOM_REQUESTED_CATEGORY.getName()");
873: // "#friendlyPermalink(" -> "#FriendlyPermalink("
874: templateData = templateData.replaceAll(
875: "#friendlyPermalink\\(",
876: "#FriendlyPermalink(");
877: // "$blogCategory.getCategoryURL()" -> "#BlogURL()$blogCategory.getName()"
878: templateData = templateData
879: .replaceAll(
880: "\\$blogCategory\\.getCategoryURL\\(\\s*\\)",
881: "#BlogURL()\\$blogCategory.getName()");
882:
883: // Update category conventions
884: // "#if ($entry.getBlogCategory().getName())$entry.getBlogCategory().getName()#else$entry.getBlogCategory().getCategory()#end" -> "#CategoryDescription($entry.getBlogCategory())"
885: templateData = templateData
886: .replaceAll(
887: "#if\\s*\\(\\s*\\$entry\\.getBlogCategory\\(\\s*\\)\\.getName\\(\\s*\\)\\s*\\)\\s*\\$entry\\.getBlogCategory\\(\\s*\\)\\.getName\\(\\s*\\)\\s*#else\\s*\\$entry\\.getBlogCategory\\(\\s*\\)\\.getCategory\\(\\s*\\)\\s*#end",
888: "#CategoryDescription(\\$entry.getBlogCategory())");
889: // "#if ($blogCategory.getName())\n\t\t\t\t#multilineLink($blogCategory.getName().split(\" \"))\n\t\t\t#else\n\t\t\t\t#multilineLink($blogCategory.getCategory().split(\" \"))\n\t\t\t#end" -> "#CategoryDescription($blogCategory).split(" ")"
890: templateData = templateData
891: .replaceAll(
892: "#if\\s*\\(\\s*\\$blogCategory.getName\\(\\s*\\)\\s*\\)\\s*#multilineLink\\(\\s*\\$blogCategory\\.getName\\(\\s*\\)\\.split\\(\\s*\" \"\\s*\\)\\s*\\)\\s*#else\\s*#multilineLink\\(\\s*\\$blogCategory\\.getCategory\\(\\s*\\)\\.split\\(\\s*\" \"\\s*\\)\\s*\\)\\s*#end",
893: "#if (\\$blogCategory.getDescription())#multilineLink(\\$blogCategory.getDescription().split(\" \"))#else#multilineLink(\\$blogCategory.getName().split(\" \"))#end");
894: // "#if ($blogCategory.getName())$blogCategory.getName()#else$blogCategory.getCategory()#end" -> "#CategoryDescription($blogCategory)"
895: templateData = templateData
896: .replaceAll(
897: "#if\\s*\\(\\s*\\$blogCategory.getName\\(\\s*\\)\\s*\\)\\s*\\$blogCategory\\.getName\\(\\s*\\)\\s*#else\\s*\\$blogCategory\\.getCategory\\(\\s*\\)\\s*#end",
898: "#CategoryDescription(\\$blogCategory)");
899: // "$entry.getBlogCategory().getCategoryURL()" -> "#BlogURL()$entry.getCategory()"
900: templateData = templateData
901: .replaceAll(
902: "\\$entry\\.getBlogCategory\\(\\s*\\)\\.getCategoryURL\\(\\s*\\)",
903: "#BlogURL()\\$entry.getCategory()");
904:
905: // Update link conventions
906: // "$entry.getLink()" -> "#FriendlyPermalink($entry)\n$entryLink\n"
907: templateData = templateData
908: .replaceAll(
909: "\\$entry\\.getLink\\(\\s*\\)(&)?&?",
910: "#FriendlyPermalink(\\$entry)\\$entryLink?");
911: // "page=comments" -> ""
912: //templateData = templateData.replaceAll("page=comments", "");
913:
914: // Updated feed URLs
915: // "$BLOJSOM_REQUESTED_CATEGORY.getCategoryURL()?flavor=rdf" -> "#BlogURL()/feed/rdf/"
916: templateData = templateData
917: .replaceAll(
918: "\\$BLOJSOM_REQUESTED_CATEGORY\\.getCategoryURL\\(\\s*\\)\\?flavor=rdf",
919: "#BlogURL()/feed/rdf/");
920: // "$BLOJSOM_REQUESTED_CATEGORY.getCategoryURL()?flavor=rss" -> "#BlogURL()/feed/"
921: templateData = templateData
922: .replaceAll(
923: "\\$BLOJSOM_REQUESTED_CATEGORY\\.getCategoryURL\\(\\s*\\)\\?flavor=rss",
924: "#BlogURL()/feed/");
925: // "$BLOJSOM_REQUESTED_CATEGORY.getCategoryURL()?flavor=atom", "#BlogURL()/feed/atom/"
926: templateData = templateData
927: .replaceAll(
928: "\\$BLOJSOM_REQUESTED_CATEGORY\\.getCategoryURL\\(\\s*\\)\\?flavor=atom",
929: "#BlogURL()/feed/atom/");
930:
931: // Updated comment and trackback checks
932: // "?tb=y" -> "?tb=y&entry_id=$entry.getId()"
933: templateData = templateData.replaceAll("\\?tb=y",
934: "?tb=y&entry_id=\\$entry.getId()");
935: // "#if ($BLOJSOM_COMMENTS_ENABLED.booleanValue() && $entry.supportsComments())" -> "#if ($entry.allowsComments())"
936: // "#if ($BLOJSOM_COMMENTS_ENABLED.booleanValue() && $entry.supportsComments() && ($entry.getMetaData() && !$entry.getMetaData().containsKey(\"blog-entry-comments-disabled\")))" -> "#if ($entry.allowsComments())"
937: templateData = templateData
938: .replaceAll(
939: "#if\\s*\\(\\s*\\$BLOJSOM_COMMENTS_ENABLED\\.booleanValue\\(\\s*\\)\\s*&&\\s*\\$entry\\.supportsComments\\(\\s*\\)\\s*\\)?(&&\\s*\\(\\$entry\\.getMetaData\\(\\s*\\)\\s*&&\\s*!\\$entry\\.getMetaData\\(\\s*\\)\\.containsKey\\(\\s*\"blog-entry-comments-disabled\"\\s*\\)\\s*\\)\\s*\\))?",
940: "#if (\\$entry.allowsComments())");
941: // "#if ($BLOJSOM_TRACKBACK_PLUGIN_ENABLED.booleanValue() && $entry.supportsTrackbacks() && ($entry.getMetaData() && !$entry.getMetaData().containsKey(\"blog-entry-trackbacks-disabled\")))" -> "#if ($entry.allowsTrackbacks())"
942: templateData = templateData
943: .replaceAll(
944: "#if\\s*\\(\\s*\\$BLOJSOM_TRACKBACK_PLUGIN_ENABLED\\.booleanValue\\(\\s*\\)\\s*&&\\s*\\$entry\\.supportsTrackbacks\\(\\s*\\)\\s*&&\\s*\\(\\s*\\$entry\\.getMetaData\\(\\s*\\)\\s*&&\\s*!\\$entry\\.getMetaData\\(\\s*\\)\\.containsKey\\(\\s*\"blog-entry-trackbacks-disabled\"\\s*\\)\\s*\\)\\s*\\)",
945: "#if (\\$entry.allowsTrackbacks())");
946: // "<input type=\"hidden\" name=\"comment\" value=\"y\" />" -> "<input type=\"hidden\" name=\"comment\" value=\"y\" />\n\t\t\t\t<input type=\"hidden\" name=\"entry_id\" value=\"$entry.getId()\" />\n\t\t\t\t<input type=\"hidden\" name=\"redirect_to\" value=\"#FriendlyPermalink($entry)$entryLink\" />"
947: templateData = templateData
948: .replaceAll(
949: "<input type=\"hidden\" name=\"comment\" value=\"y\"\\s*/?>",
950: "<input type=\"hidden\" name=\"comment\" value=\"y\" />\n\t\t\t\t<input type=\"hidden\" name=\"entry_id\" value=\"\\$entry.getId()\" />\n\t\t\t\t<input type=\"hidden\" name=\"redirect_to\" value=\"#FriendlyPermalink(\\$entry)\\$entryLink\" />");
951:
952: FileUtils.writeStringToFile(template, templateData,
953: BlojsomConstants.UTF8);
954: } catch (IOException e) {
955: if (_logger.isErrorEnabled()) {
956: _logger.error(e);
957: }
958: }
959: }
960: }
961:
962: if (_logger.isDebugEnabled()) {
963: _logger
964: .debug("Finished upgrading blojsom 2 instance to blojsom 3!");
965: }
966: }
967: }
|