Merge pull request #76 from lacerta-doc/feature/scan_and_save_document

Feature/scan and save document
This commit is contained in:
ろむねこ 2024-01-18 18:11:21 +09:00 committed by GitHub
commit f5bc740f58
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 149 additions and 8 deletions

View File

@ -26,8 +26,12 @@ import java.util.Objects;
import javax.inject.Inject; import javax.inject.Inject;
import dagger.hilt.android.AndroidEntryPoint; import dagger.hilt.android.AndroidEntryPoint;
import one.nem.lacerta.model.document.DocumentDetail;
import one.nem.lacerta.model.document.DocumentMeta;
import one.nem.lacerta.processor.factory.DocumentProcessorFactory;
import one.nem.lacerta.utils.LacertaLogger; import one.nem.lacerta.utils.LacertaLogger;
import one.nem.lacerta.data.Document; import one.nem.lacerta.data.Document;
import one.nem.lacerta.vcs.factory.LacertaVcsFactory;
@AndroidEntryPoint @AndroidEntryPoint
public class ScannerManagerActivity extends AppCompatActivity { public class ScannerManagerActivity extends AppCompatActivity {
@ -40,6 +44,11 @@ public class ScannerManagerActivity extends AppCompatActivity {
@Inject @Inject
Document document; Document document;
@Inject
DocumentProcessorFactory documentProcessorFactory;
@Inject
LacertaVcsFactory lacertaVcsFactory;
// Variables // Variables
private ArrayList<Bitmap> croppedImages = new ArrayList<>(); private ArrayList<Bitmap> croppedImages = new ArrayList<>();
@ -143,7 +152,21 @@ public class ScannerManagerActivity extends AppCompatActivity {
private void saveNewDocument() { private void saveNewDocument() {
logger.debug(TAG, "saveNewDocument"); logger.debug(TAG, "saveNewDocument");
DocumentMeta documentMeta = new DocumentMeta("Untitled"); // TODO-rca: デフォルトタイトルを指定できるようにする
DocumentDetail documentDetail = document.createDocument(documentMeta);
Bitmap[] bitmaps = new Bitmap[this.croppedImages.size()];
this.croppedImages.toArray(bitmaps);
try {
documentProcessorFactory.create(documentDetail).addNewPagesToLast(bitmaps);
Toast.makeText(this, "Saved.", Toast.LENGTH_SHORT).show();
lacertaVcsFactory.create(documentDetail.getMeta().getId()).generateRevisionAtCurrent("Initial commit");
lacertaVcsFactory.create(documentDetail.getMeta().getId()).printLog(); // Debug
lacertaVcsFactory.create(documentDetail.getMeta().getId()).printRev(); // Debug
finish();
} catch (Exception e) {
logger.error(TAG, "Error: " + e.getMessage());
logger.e_code("9dff2a28-20e8-4ccd-9d04-f0c7646faa6a");
}
} }
private void insertToExistDocument() { private void insertToExistDocument() {

View File

@ -24,6 +24,7 @@ import one.nem.lacerta.utils.LacertaLogger;
// Lacerta/vcs // Lacerta/vcs
import one.nem.lacerta.vcs.LacertaVcs; import one.nem.lacerta.vcs.LacertaVcs;
import one.nem.lacerta.vcs.factory.LacertaVcsFactory;
public class DocumentImpl implements Document { public class DocumentImpl implements Document {
@ -36,14 +37,14 @@ public class DocumentImpl implements Document {
@Inject @Inject
LacertaDatabase database; LacertaDatabase database;
// @Inject @Inject
// LacertaVcs vcs; LacertaVcsFactory vcsFactory;
@Inject @Inject
public DocumentImpl() { public DocumentImpl() {
// Init // Init
logger.debug(TAG, "called"); // logger.debug(TAG, "called");
} }
@ -69,7 +70,8 @@ public class DocumentImpl implements Document {
database.documentDao().insert(documentEntity); database.documentDao().insert(documentEntity);
// Vcs // Vcs
// vcs.createDocument(meta.getId()); LacertaVcs vcs = vcsFactory.create(meta.getId());
vcs.createDocument(meta.getId());
return detail; return detail;
} }

View File

@ -48,6 +48,17 @@ public class DocumentMeta { // TODO-rca: JavaDoc対応
public DocumentMeta() { public DocumentMeta() {
} }
public DocumentMeta(String title) {
this.id = UUID.randomUUID().toString();
this.title = title;
this.tags = new ArrayList<>();
this.author = ""; // TODO-rca: 作者のデフォルト値を設定できるようにする
this.defaultBranch = "main"; // TODO-rca: デフォルトブランチのデフォルト値を設定できるようにする
this.path = new PublicPath().getRoot();
this.updatedAt = new Date();
this.createdAt = new Date();
}
public DocumentMeta(String title, List<DocumentTag> tags, String author, String defaultBranch) { public DocumentMeta(String title, List<DocumentTag> tags, String author, String defaultBranch) {
this.id = UUID.randomUUID().toString(); this.id = UUID.randomUUID().toString();
this.title = title; this.title = title;

View File

@ -17,7 +17,7 @@ import one.nem.lacerta.source.database.dao.LibraryDao;
import one.nem.lacerta.source.database.dao.VcsRevDao; import one.nem.lacerta.source.database.dao.VcsRevDao;
import one.nem.lacerta.source.database.dao.VcsLogDao; import one.nem.lacerta.source.database.dao.VcsLogDao;
@Database(entities = {TagEntity.class, DocumentEntity.class, LibraryEntity.class, VcsRevEntity.class, VcsLogEntity.class}, version = 2) @Database(entities = {TagEntity.class, DocumentEntity.class, LibraryEntity.class, VcsRevEntity.class, VcsLogEntity.class}, version = 3)
public abstract class LacertaDatabase extends RoomDatabase { public abstract class LacertaDatabase extends RoomDatabase {
public abstract TagDao tagDao(); public abstract TagDao tagDao();
public abstract DocumentDao documentDao(); public abstract DocumentDao documentDao();

View File

@ -3,6 +3,7 @@ package one.nem.lacerta.source.database.dao;
import androidx.room.Dao; import androidx.room.Dao;
import androidx.room.Insert; import androidx.room.Insert;
import androidx.room.Query; import androidx.room.Query;
import androidx.room.Update;
import java.util.List; import java.util.List;
@ -24,6 +25,14 @@ public interface VcsLogDao {
@Query("SELECT * FROM vcs_log WHERE document_id = :documentId") @Query("SELECT * FROM vcs_log WHERE document_id = :documentId")
List<VcsLogEntity> findByDocumentId(String documentId); List<VcsLogEntity> findByDocumentId(String documentId);
@Query("SELECT * FROM vcs_log WHERE document_id = :documentId AND is_included = :isIncluded ORDER BY created_at")
List<VcsLogEntity> findByDocumentIdAndIncluded(String documentId, boolean isIncluded);
@Query("SELECT * FROM vcs_log WHERE document_id = :documentId AND branch_name = :branchName")
List<VcsLogEntity> findByDocumentIdAndBranchName(String documentId, String branchName);
@Query("SELECT * FROM vcs_log WHERE document_id = :documentId AND branch_name = :branchName AND is_included = :isIncluded ORDER BY created_at")
List<VcsLogEntity> findByDocumentIdAndBranchNameAndIncluded(String documentId, String branchName, boolean isIncluded);
// Insert // Insert
@Insert @Insert
@ -35,5 +44,12 @@ public interface VcsLogDao {
@Insert @Insert
void insert(VcsLogEntity vcsLog); void insert(VcsLogEntity vcsLog);
// TODO-rca: Update, Deleteが必要か検討 @Update
void update(VcsLogEntity vcsLog);
@Update
void updateAll(VcsLogEntity... vcsLogs);
@Update
void updateAll(List<VcsLogEntity> vcsLogs);
} }

View File

@ -45,4 +45,10 @@ public class VcsLogEntity {
*/ */
@ColumnInfo(name = "action") @ColumnInfo(name = "action")
public String action; public String action;
/**
* Revに含まれてるかどうか
*/
@ColumnInfo(name = "is_included")
public boolean isIncluded;
} }

View File

@ -11,7 +11,12 @@ public interface LacertaVcs {
public void createDocument(String documentId); public void createDocument(String documentId);
public void generateRevisionAtCurrent(String message);
// debug // debug
public void printLog(); public void printLog();
public void printRev();
} }

View File

@ -1,5 +1,7 @@
package one.nem.lacerta.vcs.impl; package one.nem.lacerta.vcs.impl;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID; import java.util.UUID;
import javax.inject.Inject; import javax.inject.Inject;
@ -8,8 +10,12 @@ import dagger.assisted.Assisted;
import dagger.assisted.AssistedInject; import dagger.assisted.AssistedInject;
import one.nem.lacerta.source.database.LacertaDatabase; import one.nem.lacerta.source.database.LacertaDatabase;
import one.nem.lacerta.source.database.entity.VcsLogEntity; import one.nem.lacerta.source.database.entity.VcsLogEntity;
import one.nem.lacerta.source.database.entity.VcsRevEntity;
import one.nem.lacerta.utils.LacertaLogger; import one.nem.lacerta.utils.LacertaLogger;
import one.nem.lacerta.vcs.ActionType;
import one.nem.lacerta.vcs.LacertaVcs; import one.nem.lacerta.vcs.LacertaVcs;
import one.nem.lacerta.vcs.internal.JsonUtils;
import one.nem.lacerta.vcs.model.action.InsertPage;
public class LacertaVcsImpl implements LacertaVcs { public class LacertaVcsImpl implements LacertaVcs {
@ -38,12 +44,17 @@ public class LacertaVcsImpl implements LacertaVcs {
@Override @Override
public void insertPage(int index, String fileName) { public void insertPage(int index, String fileName) {
logger.debug(TAG, "insertPage"); logger.debug(TAG, "insertPage");
// InsertPage
InsertPage insertPage = new InsertPage(index, fileName);
insertPage.setActionType(ActionType.INSERT_PAGE);
VcsLogEntity vcsLogEntity = new VcsLogEntity(); VcsLogEntity vcsLogEntity = new VcsLogEntity();
vcsLogEntity.id = UUID.randomUUID().toString(); vcsLogEntity.id = UUID.randomUUID().toString();
vcsLogEntity.documentId = documentId; vcsLogEntity.documentId = documentId;
vcsLogEntity.branchName = "master"; vcsLogEntity.branchName = "master";
vcsLogEntity.createdAt = new java.util.Date(); vcsLogEntity.createdAt = new java.util.Date();
vcsLogEntity.action = "placeholder"; vcsLogEntity.action = JsonUtils.toJson(insertPage);
database.vcsLogDao().insert(vcsLogEntity); database.vcsLogDao().insert(vcsLogEntity);
} }
@ -54,7 +65,61 @@ public class LacertaVcsImpl implements LacertaVcs {
@Override @Override
public void createDocument(String documentId) { public void createDocument(String documentId) {
logger.debug(TAG, "createDocument");
VcsLogEntity vcsLogEntity = new VcsLogEntity();
vcsLogEntity.id = UUID.randomUUID().toString();
vcsLogEntity.documentId = documentId;
vcsLogEntity.branchName = "master";
vcsLogEntity.createdAt = new java.util.Date();
vcsLogEntity.action = "ph-createDocument";
database.vcsLogDao().insert(vcsLogEntity);
}
// Internal
private ArrayList<VcsLogEntity> getNonIncludedVcsLogEntities() {
return new ArrayList<>(database.vcsLogDao().findByDocumentIdAndIncluded(documentId, false));
}
private ArrayList<VcsLogEntity> getNonIncludedVcsLogEntities(String branchName) {
return new ArrayList<>(database.vcsLogDao().findByDocumentIdAndBranchNameAndIncluded(documentId, branchName, false));
}
private void setIncludedVcsLogEntities(ArrayList<VcsLogEntity> vcsLogEntities) {
vcsLogEntities.forEach(vcsLogEntity -> {
vcsLogEntity.isIncluded = true;
database.vcsLogDao().update(vcsLogEntity);
});
logger.debug(TAG, "setIncludedVcsLogEntities updated: " + vcsLogEntities.size() + " entities");
}
@Override
public void generateRevisionAtCurrent(String message) {
logger.debug(TAG, "generateRevisionAtCurrent");
// TODO-rca: ブランチを考慮する
ArrayList<VcsLogEntity> vcsLogEntities = getNonIncludedVcsLogEntities();
ArrayList<String> logIds = new ArrayList<>();
vcsLogEntities.forEach(vcsLogEntity -> {
logIds.add(vcsLogEntity.id);
});
VcsRevEntity vcsRevEntity = new VcsRevEntity();
vcsRevEntity.id = UUID.randomUUID().toString();
vcsRevEntity.documentId = documentId;
vcsRevEntity.branchName = "master";
vcsRevEntity.createdAt = new java.util.Date();
vcsRevEntity.commitMessage = message;
vcsRevEntity.logIds = logIds;
database.vcsRevDao().insert(vcsRevEntity);
setIncludedVcsLogEntities(vcsLogEntities);
logger.debug(TAG, "generateRevisionAtCurrent finished");
logger.debug(TAG, "New revision inserted: " + vcsRevEntity.id);
} }
@Override @Override
@ -62,6 +127,19 @@ public class LacertaVcsImpl implements LacertaVcs {
logger.debug(TAG, "printLog"); logger.debug(TAG, "printLog");
database.vcsLogDao().findAll().forEach(vcsLog -> { database.vcsLogDao().findAll().forEach(vcsLog -> {
logger.debug(TAG, vcsLog.id); logger.debug(TAG, vcsLog.id);
logger.debug(TAG, vcsLog.documentId + " " + vcsLog.branchName);
logger.debug(TAG, vcsLog.action);
});
}
@Override
public void printRev() {
logger.debug(TAG, "printRev");
database.vcsRevDao().findAll().forEach(vcsRev -> {
logger.debug(TAG, vcsRev.id);
logger.debug(TAG, vcsRev.documentId + " " + vcsRev.branchName);
logger.debug(TAG, vcsRev.commitMessage);
logger.debug(TAG, vcsRev.logIds.toString());
}); });
} }
} }