Advertisement
Guest User

Untitled

a guest
Jul 26th, 2016
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.47 KB | None | 0 0
  1. public class MainNavigation {
  2.  
  3. private NavigationTranslator translator = new NavigationTranslator();
  4. private static String wpUrl = "sql-hostname";
  5. private static String wpUser = "user";
  6. private static String wpPassword = "password";
  7.  
  8. public static void main(String[] args) {
  9.  
  10. MainNavigation navigation = new MainNavigation();
  11. Connection connection = navigation.connect(wpUrl, connectionProperties());
  12. navigation.createNavigationMenu(connection);
  13.  
  14.  
  15. }
  16.  
  17. private Connection connect(String host, Properties connectionProperties) {
  18. try {
  19. return DriverManager.getConnection(host, connectionProperties);
  20. } catch (SQLException e) {
  21. throw Throwables.propagate(e);
  22. }
  23. }
  24.  
  25. private void createNavigationMenu(Connection connection) {
  26. DSLContext context = DSL.using(connection);
  27. createNavigation(context);
  28.  
  29. }
  30.  
  31.  
  32. private void createNavigation(DSLContext context) {
  33.  
  34. // Fist we will create a new term in stage_terms table and insert it.
  35.  
  36. ImmutableMap<Field<?>, Object> navTerm = translator.navigationTerm();
  37. context.insertInto(table("stage_terms")).set(navTerm).execute();
  38.  
  39. String termIdStatement = "select term_id from stage_terms order by term_id desc limit 1";
  40. Record termIdRecord = context.fetch(termIdStatement).get(0);
  41. Integer termId = termIdRecord.getValue("term_id", Integer.class);
  42.  
  43. ImmutableMap<Field<?>, Object> navTermTaxonomy = translator.navigationTermTaxonomy(termId);
  44. context.insertInto(table("stage_term_taxonomy")).set(navTermTaxonomy).execute();
  45.  
  46.  
  47. String taxonomyIdStatement = "select term_taxonomy_id from stage_term_taxonomy order by term_taxonomy_id desc limit 1";
  48. Record taxonomyIdRecord = context.fetch(taxonomyIdStatement).get(0);
  49. Integer taxonomyId = taxonomyIdRecord.getValue("term_taxonomy_id", Integer.class);
  50.  
  51. String maxIdStatement = "select ID from stage_posts order by ID desc limit 1";
  52. Record maxRecord = context.fetch(maxIdStatement).get(0);
  53.  
  54. Integer id = maxRecord.getValue("ID", Integer.class) + 1;
  55.  
  56. ImmutableList<ImmutableMap<Field<?>, Object>> navigationPosts = translator.navigationPosts(
  57. id);
  58. for (ImmutableMap<Field<?>, Object> post : navigationPosts) {
  59. context.insertInto(table("stage_posts")).set(post).execute();
  60. }
  61.  
  62. ImmutableList<ImmutableMap<Field<?>, Object>> navList = translator.postMetaData();
  63.  
  64. for (ImmutableMap<Field<?>, Object> nav : navList) {
  65. context.insertInto(table("stage_postmeta")).set(nav).execute();
  66. }
  67.  
  68. Field postType = field("post_type");
  69. Result<Record> records = context
  70. .select()
  71. .from(table("stage_posts"))
  72. .where(postType.equal("nav_menu_item"))
  73. .fetch();
  74.  
  75. records.forEach(navItem -> {
  76. ImmutableMap<Field<?>, Object> relationship = translator.navigationRelationship(navItem.getValue(
  77. "ID",
  78. Integer.class
  79. ),
  80. taxonomyId);
  81. context.insertInto(table("stage_term_relationships")).set(relationship).execute();
  82. });
  83.  
  84. }
  85.  
  86. private static Properties connectionProperties() {
  87. Properties connectionProperties = new Properties();
  88. connectionProperties.put("user", wpUser);
  89. connectionProperties.put("password", wpPassword);
  90. return connectionProperties;
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement