Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 1e7e547bc547293902635c512f022ce232721583 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
package org.eclipse.emf.cdo.server.internal.mongodb;

import org.eclipse.emf.cdo.server.CDOServerBrowser;
import org.eclipse.emf.cdo.server.CDOServerBrowser.AbstractPage;
import org.eclipse.emf.cdo.server.mongodb.IMongoDBStore;
import org.eclipse.emf.cdo.spi.server.InternalRepository;

import org.eclipse.net4j.util.factory.ProductCreationException;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;

import java.io.PrintStream;
import java.util.Set;

/**
 * @author Eike Stepper
 * @since 4.0
 */
public class MongoDBBrowserPage extends AbstractPage
{
  private static final boolean SHOW_INDEXES = true;

  private static final boolean SHOW_DOCUMENTS = true;

  private static final boolean SHOW_INITIAL_COMMIT = true;

  public MongoDBBrowserPage()
  {
    super("collections", "MongoDB Collections");
  }

  public boolean canDisplay(InternalRepository repository)
  {
    return repository.getStore() instanceof IMongoDBStore;
  }

  public void display(CDOServerBrowser browser, InternalRepository repository, PrintStream out)
  {
    IMongoDBStore store = (IMongoDBStore)repository.getStore();
    DB db = (DB)store.getDB();

    out.print("<table border=\"0\">\r\n");
    out.print("<tr>\r\n");

    out.print("<td valign=\"top\">\r\n");
    String collection = showCollections(browser, out, db, repository.getName());
    out.print("</td>\r\n");
    out.print("<td>&nbsp;&nbsp;&nbsp;</td>\r\n");

    if (collection != null)
    {
      out.print("<td valign=\"top\">\r\n");
      showCollection(browser, out, db, collection);
      out.print("</td>\r\n");
    }

    out.print("</tr>\r\n");
    out.print("</table>\r\n");
  }

  protected String showCollections(CDOServerBrowser browser, PrintStream pout, DB db, String repo)
  {
    String collection = browser.getParam("collection");

    Set<String> allCollectionNames = db.getCollectionNames();
    for (String collectionName : allCollectionNames)
    {
      if (collection == null)
      {
        collection = collectionName;
      }

      String label = browser.escape(collectionName)/* .toLowerCase() */;
      if (collectionName.equals(collection))
      {
        pout.print("<b>" + label + "</b><br>\r\n");
      }
      else
      {
        pout.print(browser.href(label, getName(), "collection", collectionName) + "<br>\r\n");
      }
    }

    return collection;
  }

  protected void showCollection(CDOServerBrowser browser, PrintStream pout, DB db, String collection)
  {
    DBCollection coll = db.getCollection(collection);
    pout.print("<table border=\"1\" cellpadding=\"4\">\r\n");
    pout.print("<tr><td colspan=\"2\" align=\"center\"><h2>" + collection + "</h2></td></tr>\r\n");

    if (SHOW_INDEXES)
    {
      showIndexes(browser, pout, coll);
    }

    if (SHOW_DOCUMENTS)
    {
      showDocuments(browser, pout, coll);
    }
  }

  protected void showIndexes(CDOServerBrowser browser, PrintStream pout, DBCollection coll)
  {
    pout.print("<tr><td colspan=\"2\" align=\"center\" bgcolor=\"EEEEEE\"><h4>Indexes</h4></td></tr>\r\n");

    int i = 0;
    for (DBObject index : coll.getIndexInfo())
    {
      ++i;
      String bg = (i & 1) == 1 ? "bgcolor=\"DDDDDD\"" : "bgcolor=\"EEEEEE\"";
      pout.print("<tr><td valign=\"top\" " + bg + "><b>" + i + "&nbsp;</b></td><td valign=\"top\">");
      showObject(browser, pout, index, "");
      pout.print("</td></tr>\r\n");
    }
  }

  protected void showDocuments(CDOServerBrowser browser, PrintStream pout, DBCollection coll)
  {
    DBCursor cursor = null;

    try
    {
      pout.print("<tr><td colspan=\"2\" align=\"center\" bgcolor=\"EEEEEE\"><h4>Documents</h4></td></tr>\r\n");

      int i = 0;
      cursor = coll.find();

      try
      {
        cursor = cursor.sort(new BasicDBObject("_id", 1));
      }
      catch (Exception ex)
      {
        // Ignore
      }

      while (cursor.hasNext())
      {
        DBObject doc = cursor.next();

        ++i;
        if (i == 1 && showFirstCommit(coll))
        {
          continue;
        }

        String bg = (i & 1) == 1 ? "bgcolor=\"DDDDDD\"" : "bgcolor=\"EEEEEE\"";
        pout.print("<tr><td valign=\"top\" " + bg + "><b>" + i + "&nbsp;</b></td><td valign=\"top\">");
        showObject(browser, pout, doc, "");
        pout.print("</td></tr>\r\n");
      }

      pout.print("</table>\r\n");
    }
    finally
    {
      if (cursor != null)
      {
        cursor.close();
      }
    }
  }

  protected void showObject(CDOServerBrowser browser, PrintStream pout, DBObject doc, String level)
  {
    Set<String> keySet = doc.keySet();

    boolean highlight = false;
    try
    {
      String paramKey = browser.getParam("key");
      if (paramKey != null)
      {
        String paramValue = browser.getParam("value");
        Object value = doc.get(paramKey);
        if (String.valueOf(value).equals(paramValue))
        {
          highlight = true;
        }
      }
    }
    catch (Exception ex)
    {
      // Ignore
    }

    if (highlight)
    {
      pout.print("<table border=\"0\" bgcolor=\"#FFFFA8\"><tr><td>");
    }

    for (String key : keySet)
    {
      pout.print(level);
      pout.print("<b>");
      pout.print(key);
      pout.print("</b> = ");

      Object value = doc.get(key);
      if (value instanceof DBObject)
      {
        DBObject child = (DBObject)value;
        pout.print("<br>");
        showObject(browser, pout, child, level + "&nbsp;&nbsp;");
      }
      else
      {
        pout.print("<font color=\"#0000FF\">");
        if (value instanceof String)
        {
          pout.print("\"");
          pout.print(browser.escape((String)value));
          pout.print("\"");
        }
        else
        {
          String string = String.valueOf(value);
          pout.print(browser.href(string, "collections", "key", key, "value", string));
        }

        pout.print("</font><br>");
      }
    }

    if (highlight)
    {
      pout.print("</td></tr></table>");
    }
  }

  protected boolean showFirstCommit(DBCollection coll)
  {
    return coll.getName().equals(Commits.COMMITS) && !SHOW_INITIAL_COMMIT;
  }

  /**
   * @author Eike Stepper
   */
  public static class Factory extends org.eclipse.net4j.util.factory.Factory
  {
    public static final String TYPE = "default";

    public Factory()
    {
      super(PRODUCT_GROUP, TYPE);
    }

    public MongoDBBrowserPage create(String description) throws ProductCreationException
    {
      return new MongoDBBrowserPage();
    }
  }
}

Back to the top