Java Code Examples for com.couchbase.lite.Document

The following examples show how to use com.couchbase.lite.Document. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: mini-hacks   Source File: PlacesAdapter.java    License: MIT License 6 votes vote down vote up
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    Place place = dataSet.get(position);

    holder.restaurantName.setText(place.getName());
    holder.restaurantText.setText(place.getAddress());

    Document document = database.getDocument(place.getId());
    Attachment attachment = document.getCurrentRevision().getAttachment("photo");
    if (attachment != null) {
        InputStream is = null;
        try {
            is = attachment.getContent();
        } catch (CouchbaseLiteException e) {
            e.printStackTrace();
        }
        Drawable drawable = Drawable.createFromStream(is, "photo");
        holder.restaurantImage.setImageDrawable(drawable);
    }
}
 
Example 2
Source Project: mini-hacks   Source File: MainActivity.java    License: MIT License 6 votes vote down vote up
private Document createListItem(String text) throws Exception {

        SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");

        UUID uuid = UUID.randomUUID();
        Calendar calendar = GregorianCalendar.getInstance();
        long currentTime = calendar.getTimeInMillis();
        String currentTimeString = dateFormatter.format(calendar.getTime());

        String id = currentTime + "-" + uuid.toString();

        // Step 7 - Create document from text box's field entry.  code replaces this
        Document document = database.getDocument(id); // creates a document with the given id

        Map<String, Object> properties = new HashMap<String, Object>();
        properties.put("_id", id);
        properties.put("text", text);
        properties.put("check", Boolean.FALSE);
        properties.put("created_at", currentTimeString);
        document.putProperties(properties);

        return document;
    }
 
Example 3
Source Project: mini-hacks   Source File: MainActivity.java    License: MIT License 6 votes vote down vote up
/**
 * Handle click on item in list
 */
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {

    // Step 10 code goes here
    // This code handles checkbox touches.  Couchbase Lite documents are like versioned-maps.
    // To change a Document, add a new Revision.
    QueryRow row = (QueryRow) adapterView.getItemAtPosition(position);
    Document document = row.getDocument();
    Map<String, Object> newProperties = new HashMap<String, Object>(document.getProperties());

    boolean checked = ((Boolean) newProperties.get("check")).booleanValue();
    newProperties.put("check", !checked);

    try {
        document.putProperties(newProperties);
        kitchenSyncArrayAdapter.notifyDataSetChanged();
    } catch (Exception e) {
        Toast.makeText(getApplicationContext(), "Error updating database, see logs for details", Toast.LENGTH_LONG).show();
        Log.e(TAG, "Error updating database", e);
    }

}
 
Example 4
Source Project: mini-hacks   Source File: MainActivity.java    License: MIT License 5 votes vote down vote up
private Document createListItem(String text) throws Exception {

        SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");

        UUID uuid = UUID.randomUUID();
        Calendar calendar = GregorianCalendar.getInstance();
        long currentTime = calendar.getTimeInMillis();
        String currentTimeString = dateFormatter.format(calendar.getTime());

        String id = currentTime + "-" + uuid.toString();

        // Step 7 - Create document from text box's field entry.  code replaces this
        return null;
    }
 
Example 5
Source Project: mini-hacks   Source File: UniqueRatingFragment.java    License: MIT License 5 votes vote down vote up
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_unique, container, false);

    nameInput = (EditText) rootView.findViewById(R.id.nameInput);
    ratingBar = (RatingBar) rootView.findViewById(R.id.ratingBar);
    button = (Button) rootView.findViewById(R.id.button);

    database = ((MainActivity) getActivity()).storageManager.database;

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Document document = database.createDocument();

            Map<String, Object> properties = new HashMap<String, Object>();

            properties.put("name", nameInput.getText().toString());
            properties.put("rating", String.valueOf(ratingBar.getRating()));
            properties.put("type", "unique");

            try {
                document.putProperties(properties);
            } catch (CouchbaseLiteException e) {
                e.printStackTrace();
            }
        }
    });

    return rootView;
}
 
Example 6
Source Project: couchbase-lite-android   Source File: MainActivity.java    License: Apache License 2.0 5 votes vote down vote up
void databaseOperation() {
    try {
        Manager mgr = new Manager(new AndroidContext(getBaseContext()), null);
        DatabaseOptions opt = new DatabaseOptions();
        opt.setCreate(true);
        Database db = mgr.openDatabase(DB_NAME, opt);

        Map<String, Object> props;
        Document doc = db.getExistingDocument(DOC_ID);
        if (doc == null) {
            // new doc
            doc = db.getDocument(DOC_ID);
            props = new HashMap<>();
            props.put("Database Version", "1.4.1");
            props.put("update", 1);
            doc.putProperties(props);
        } else {
            // update
            props = new HashMap<>(doc.getProperties());
            props.put("update", (Integer) props.get("update") + 1);
            doc.putProperties(props);
        }

        Log.i(TAG, "Num of docs: " + db.getDocumentCount());
        Log.i(TAG, "Doc content: " + db.getDocument(DOC_ID).getProperties());

        db.close();
    } catch (Exception e) {
        Log.e(TAG, "" + e.getMessage());
        e.printStackTrace();
    }
}
 
Example 7
Source Project: couchbase-lite-android   Source File: MainActivity.java    License: Apache License 2.0 5 votes vote down vote up
void databaseOperationSDCard() {
    try {
        Manager mgr = new Manager(new SDCardContext(getBaseContext()), null);
        DatabaseOptions opt = new DatabaseOptions();
        opt.setCreate(true);
        Database db = mgr.openDatabase(DB_NAME, opt);

        Map<String, Object> props;
        Document doc = db.getExistingDocument(DOC_ID);
        if (doc == null) {
            // new doc
            doc = db.getDocument(DOC_ID);
            props = new HashMap<>();
            props.put("Database Version", "1.4.1");
            props.put("update", 1);
            doc.putProperties(props);
        } else {
            // update
            props = new HashMap<>(doc.getProperties());
            props.put("update", (Integer) props.get("update") + 1);
            doc.putProperties(props);
        }

        Log.i(TAG, "Num of docs: " + db.getDocumentCount());
        Log.i(TAG, "Doc content: " + db.getDocument(DOC_ID).getProperties());

        db.close();
    } catch (Exception e) {
        Log.e(TAG, "" + e.getMessage());
        e.printStackTrace();
    }
}
 
Example 8
Source Project: couchbase-lite-android   Source File: DocSavePerfTest.java    License: Apache License 2.0 5 votes vote down vote up
private MutableDocument updateMutableDocument(String docID, Map<String, Object> value) {
    Document doc = db.getDocument(docID);
    MutableDocument mutableDoc = null;
    if (doc != null) {
        mutableDoc = doc.toMutable();
        mutableDoc.setData(value);
    } else
        mutableDoc = new MutableDocument(docID, value);
    return mutableDoc;
}
 
Example 9
private void indexedStringEntityQueriesRun(View indexedStringView, int count)
        throws CouchbaseLiteException {
    // create entities
    String[] fixedRandomStrings = StringGenerator.createFixedRandomStrings(count);
    database.beginTransaction();
    for (int i = 0; i < count; i++) {
        Document entity = database.getDocument(String.valueOf(i));
        Map<String, Object> properties = new HashMap<>();
        properties.put("indexedString", fixedRandomStrings[i]);
        entity.putProperties(properties);
    }
    database.endTransaction(true);
    log("Built and inserted entities.");

    // query for entities by indexed string at random
    int[] randomIndices = StringGenerator.getFixedRandomIndices(getQueryCount(), count - 1);

    // clear the document cache to force loading properties from the database
    database.clearDocumentCache();

    startClock();
    for (int i = 0; i < getQueryCount(); i++) {
        int nextIndex = randomIndices[i];
        List<Object> keyToQuery = new ArrayList<>(1);
        keyToQuery.add(fixedRandomStrings[nextIndex]);

        Query query = indexedStringView.createQuery();
        query.setKeys(keyToQuery);
        QueryEnumerator result = query.run();
        while (result.hasNext()) {
            QueryRow row = result.next();
            //noinspection unused
            Document document = row.getDocument();
        }
    }
    stopClock(Benchmark.Type.QUERY_INDEXED);

    // delete all entities
    deleteAll();
    log("Deleted all entities.");
}