diff --git a/component/scanner/src/main/java/one/nem/lacerta/component/scanner/ScannerManagerActivity.java b/component/scanner/src/main/java/one/nem/lacerta/component/scanner/ScannerManagerActivity.java index b8ab64bf..f55484a1 100644 --- a/component/scanner/src/main/java/one/nem/lacerta/component/scanner/ScannerManagerActivity.java +++ b/component/scanner/src/main/java/one/nem/lacerta/component/scanner/ScannerManagerActivity.java @@ -26,8 +26,12 @@ import java.util.Objects; import javax.inject.Inject; 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.data.Document; +import one.nem.lacerta.vcs.factory.LacertaVcsFactory; @AndroidEntryPoint public class ScannerManagerActivity extends AppCompatActivity { @@ -40,6 +44,11 @@ public class ScannerManagerActivity extends AppCompatActivity { @Inject Document document; + @Inject + DocumentProcessorFactory documentProcessorFactory; + + @Inject + LacertaVcsFactory lacertaVcsFactory; // Variables private ArrayList croppedImages = new ArrayList<>(); @@ -143,7 +152,21 @@ public class ScannerManagerActivity extends AppCompatActivity { private void 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() { diff --git a/data/src/main/java/one/nem/lacerta/data/impl/DocumentImpl.java b/data/src/main/java/one/nem/lacerta/data/impl/DocumentImpl.java index 10eb8265..1253d0a4 100644 --- a/data/src/main/java/one/nem/lacerta/data/impl/DocumentImpl.java +++ b/data/src/main/java/one/nem/lacerta/data/impl/DocumentImpl.java @@ -24,6 +24,7 @@ import one.nem.lacerta.utils.LacertaLogger; // Lacerta/vcs import one.nem.lacerta.vcs.LacertaVcs; +import one.nem.lacerta.vcs.factory.LacertaVcsFactory; public class DocumentImpl implements Document { @@ -36,14 +37,14 @@ public class DocumentImpl implements Document { @Inject LacertaDatabase database; -// @Inject -// LacertaVcs vcs; + @Inject + LacertaVcsFactory vcsFactory; @Inject public DocumentImpl() { // Init - logger.debug(TAG, "called"); +// logger.debug(TAG, "called"); } @@ -69,7 +70,8 @@ public class DocumentImpl implements Document { database.documentDao().insert(documentEntity); // Vcs -// vcs.createDocument(meta.getId()); + LacertaVcs vcs = vcsFactory.create(meta.getId()); + vcs.createDocument(meta.getId()); return detail; } diff --git a/model/src/main/java/one/nem/lacerta/model/document/DocumentMeta.java b/model/src/main/java/one/nem/lacerta/model/document/DocumentMeta.java index 038b625e..2fc0f4bc 100644 --- a/model/src/main/java/one/nem/lacerta/model/document/DocumentMeta.java +++ b/model/src/main/java/one/nem/lacerta/model/document/DocumentMeta.java @@ -48,6 +48,17 @@ public class DocumentMeta { // TODO-rca: JavaDoc対応 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 tags, String author, String defaultBranch) { this.id = UUID.randomUUID().toString(); this.title = title; diff --git a/source/src/main/java/one/nem/lacerta/source/database/LacertaDatabase.java b/source/src/main/java/one/nem/lacerta/source/database/LacertaDatabase.java index d009d8c0..bb8667ab 100644 --- a/source/src/main/java/one/nem/lacerta/source/database/LacertaDatabase.java +++ b/source/src/main/java/one/nem/lacerta/source/database/LacertaDatabase.java @@ -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.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 TagDao tagDao(); public abstract DocumentDao documentDao(); diff --git a/source/src/main/java/one/nem/lacerta/source/database/dao/VcsLogDao.java b/source/src/main/java/one/nem/lacerta/source/database/dao/VcsLogDao.java index cd15c89a..7721555f 100644 --- a/source/src/main/java/one/nem/lacerta/source/database/dao/VcsLogDao.java +++ b/source/src/main/java/one/nem/lacerta/source/database/dao/VcsLogDao.java @@ -3,6 +3,7 @@ package one.nem.lacerta.source.database.dao; import androidx.room.Dao; import androidx.room.Insert; import androidx.room.Query; +import androidx.room.Update; import java.util.List; @@ -24,6 +25,14 @@ public interface VcsLogDao { @Query("SELECT * FROM vcs_log WHERE document_id = :documentId") List findByDocumentId(String documentId); + @Query("SELECT * FROM vcs_log WHERE document_id = :documentId AND is_included = :isIncluded ORDER BY created_at") + List findByDocumentIdAndIncluded(String documentId, boolean isIncluded); + + @Query("SELECT * FROM vcs_log WHERE document_id = :documentId AND branch_name = :branchName") + List 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 findByDocumentIdAndBranchNameAndIncluded(String documentId, String branchName, boolean isIncluded); // Insert @Insert @@ -35,5 +44,12 @@ public interface VcsLogDao { @Insert void insert(VcsLogEntity vcsLog); - // TODO-rca: Update, Deleteが必要か検討 + @Update + void update(VcsLogEntity vcsLog); + + @Update + void updateAll(VcsLogEntity... vcsLogs); + + @Update + void updateAll(List vcsLogs); } diff --git a/source/src/main/java/one/nem/lacerta/source/database/entity/VcsLogEntity.java b/source/src/main/java/one/nem/lacerta/source/database/entity/VcsLogEntity.java index de1ed13a..17628e8e 100644 --- a/source/src/main/java/one/nem/lacerta/source/database/entity/VcsLogEntity.java +++ b/source/src/main/java/one/nem/lacerta/source/database/entity/VcsLogEntity.java @@ -45,4 +45,10 @@ public class VcsLogEntity { */ @ColumnInfo(name = "action") public String action; + + /** + * Revに含まれてるかどうか + */ + @ColumnInfo(name = "is_included") + public boolean isIncluded; } diff --git a/vcs/src/main/java/one/nem/lacerta/vcs/LacertaVcs.java b/vcs/src/main/java/one/nem/lacerta/vcs/LacertaVcs.java index 46640d37..1f5d31ea 100644 --- a/vcs/src/main/java/one/nem/lacerta/vcs/LacertaVcs.java +++ b/vcs/src/main/java/one/nem/lacerta/vcs/LacertaVcs.java @@ -11,7 +11,12 @@ public interface LacertaVcs { public void createDocument(String documentId); + public void generateRevisionAtCurrent(String message); + + // debug public void printLog(); + public void printRev(); + } diff --git a/vcs/src/main/java/one/nem/lacerta/vcs/impl/LacertaVcsImpl.java b/vcs/src/main/java/one/nem/lacerta/vcs/impl/LacertaVcsImpl.java index 15e343da..89cd8d2d 100644 --- a/vcs/src/main/java/one/nem/lacerta/vcs/impl/LacertaVcsImpl.java +++ b/vcs/src/main/java/one/nem/lacerta/vcs/impl/LacertaVcsImpl.java @@ -1,5 +1,7 @@ package one.nem.lacerta.vcs.impl; +import java.util.ArrayList; +import java.util.List; import java.util.UUID; import javax.inject.Inject; @@ -8,8 +10,12 @@ import dagger.assisted.Assisted; import dagger.assisted.AssistedInject; import one.nem.lacerta.source.database.LacertaDatabase; 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.vcs.ActionType; 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 { @@ -38,12 +44,17 @@ public class LacertaVcsImpl implements LacertaVcs { @Override public void insertPage(int index, String fileName) { logger.debug(TAG, "insertPage"); + + // InsertPage + InsertPage insertPage = new InsertPage(index, fileName); + insertPage.setActionType(ActionType.INSERT_PAGE); + VcsLogEntity vcsLogEntity = new VcsLogEntity(); vcsLogEntity.id = UUID.randomUUID().toString(); vcsLogEntity.documentId = documentId; vcsLogEntity.branchName = "master"; vcsLogEntity.createdAt = new java.util.Date(); - vcsLogEntity.action = "placeholder"; + vcsLogEntity.action = JsonUtils.toJson(insertPage); database.vcsLogDao().insert(vcsLogEntity); } @@ -54,7 +65,61 @@ public class LacertaVcsImpl implements LacertaVcs { @Override 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 getNonIncludedVcsLogEntities() { + return new ArrayList<>(database.vcsLogDao().findByDocumentIdAndIncluded(documentId, false)); + } + + private ArrayList getNonIncludedVcsLogEntities(String branchName) { + return new ArrayList<>(database.vcsLogDao().findByDocumentIdAndBranchNameAndIncluded(documentId, branchName, false)); + } + + private void setIncludedVcsLogEntities(ArrayList 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 vcsLogEntities = getNonIncludedVcsLogEntities(); + + ArrayList 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 @@ -62,6 +127,19 @@ public class LacertaVcsImpl implements LacertaVcs { logger.debug(TAG, "printLog"); database.vcsLogDao().findAll().forEach(vcsLog -> { 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()); }); } }