Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
BudgetMaster
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Robert Goldmann
BudgetMaster
Commits
c88e5808
Commit
c88e5808
authored
4 years ago
by
Robert Goldmann
Browse files
Options
Downloads
Patches
Plain Diff
#598 - migrated DatabaseParser_v4
parent
ed59297d
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/main/java/de/deadlocker8/budgetmaster/database/DatabaseParser_v4.java
+19
-134
19 additions, 134 deletions
.../deadlocker8/budgetmaster/database/DatabaseParser_v4.java
with
19 additions
and
134 deletions
src/main/java/de/deadlocker8/budgetmaster/database/DatabaseParser_v4.java
+
19
−
134
View file @
c88e5808
package
de.deadlocker8.budgetmaster.database
;
import
com.google.gson.JsonArray
;
import
com.google.gson.JsonElement
;
import
com.google.gson.JsonObject
;
import
com.google.gson.JsonParser
;
import
de.deadlocker8.budgetmaster.templates.Template
;
import
de.deadlocker8.budgetmaster.transactions.Transaction
;
import
de.deadlocker8.budgetmaster.transactions.TransactionBase
;
import
org.joda.time.DateTime
;
import
org.joda.time.format.DateTimeFormat
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
de.deadlocker8.budgetmaster.database.model.v4.*
;
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.Optional
;
public
class
DatabaseParser_v4
extends
DatabaseParser_v3
public
class
DatabaseParser_v4
{
final
Logger
LOGGER
=
LoggerFactory
.
getLogger
(
this
.
getClass
());
private
final
String
jsonString
;
protected
List
<
Transaction
>
transactions
;
protected
List
<
Template
>
templates
;
public
DatabaseParser_v4
(
String
json
)
{
super
(
json
);
this
.
jsonString
=
json
;
}
@Override
public
Database
parseDatabaseFromJSON
()
throws
IllegalArgumentException
{
final
JsonObject
root
=
JsonParser
.
parseString
(
jsonString
).
getAsJsonObject
();
super
.
categories
=
super
.
parseCategories
(
root
);
super
.
accounts
=
super
.
parseAccounts
(
root
);
this
.
transactions
=
parseTransactions
(
root
);
this
.
templates
=
parseTemplates
(
root
);
return
new
Database
(
categories
,
accounts
,
transactions
,
templates
,
new
ArrayList
<>(),
new
ArrayList
<>());
}
@Override
protected
List
<
Transaction
>
parseTransactions
(
JsonObject
root
)
{
List
<
Transaction
>
parsedTransactions
=
new
ArrayList
<>();
JsonArray
transactionsToImport
=
root
.
get
(
"transactions"
).
getAsJsonArray
();
for
(
JsonElement
currentTransaction
:
transactionsToImport
)
{
final
JsonObject
transactionObject
=
currentTransaction
.
getAsJsonObject
();
int
amount
=
transactionObject
.
get
(
"amount"
).
getAsInt
();
String
name
=
transactionObject
.
get
(
"name"
).
getAsString
();
String
description
=
transactionObject
.
get
(
"description"
).
getAsString
();
Transaction
transaction
=
new
Transaction
();
transaction
.
setAmount
(
amount
);
transaction
.
setName
(
name
);
transaction
.
setDescription
(
description
);
transaction
.
setTags
(
parseTags
(
transactionObject
));
int
categoryID
=
transactionObject
.
get
(
"category"
).
getAsJsonObject
().
get
(
"ID"
).
getAsInt
();
transaction
.
setCategory
(
getCategoryByID
(
categoryID
));
int
accountID
=
transactionObject
.
get
(
"account"
).
getAsJsonObject
().
get
(
"ID"
).
getAsInt
();
transaction
.
setAccount
(
getAccountByID
(
accountID
));
JsonElement
transferAccount
=
transactionObject
.
get
(
"transferAccount"
);
if
(
transferAccount
!=
null
)
{
int
transferAccountID
=
transferAccount
.
getAsJsonObject
().
get
(
"ID"
).
getAsInt
();
transaction
.
setTransferAccount
(
getAccountByID
(
transferAccountID
));
}
String
date
=
transactionObject
.
get
(
"date"
).
getAsString
();
DateTime
parsedDate
=
DateTime
.
parse
(
date
,
DateTimeFormat
.
forPattern
(
"yyyy-MM-dd"
));
transaction
.
setDate
(
parsedDate
);
transaction
.
setRepeatingOption
(
super
.
parseRepeatingOption
(
transactionObject
,
parsedDate
));
handleIsExpenditure
(
transactionObject
,
transaction
);
parsedTransactions
.
add
(
transaction
);
}
return
parsedTransactions
;
}
protected
List
<
Template
>
parseTemplates
(
JsonObject
root
)
{
final
List
<
Template
>
parsedTemplates
=
new
ArrayList
<>();
final
JsonArray
templatesToImport
=
root
.
get
(
"templates"
).
getAsJsonArray
();
for
(
JsonElement
currentTemplate
:
templatesToImport
)
public
BackupDatabase_v4
parseDatabaseFromJSON
()
throws
IllegalArgumentException
{
final
JsonObject
templateObject
=
currentTemplate
.
getAsJsonObject
();
BackupDatabase_v4
database
=
new
BackupDatabase_v4
();
final
String
templateName
=
templateObject
.
get
(
"templateName"
).
getAsString
();
final
JsonObject
root
=
JsonParser
.
parseString
(
jsonString
).
getAsJsonObject
();
database
.
setAccounts
(
BackupItemParser
.
parseItems
(
root
.
get
(
"accounts"
).
getAsJsonArray
(),
BackupAccount_v4
.
class
));
database
.
setCategories
(
BackupItemParser
.
parseItems
(
root
.
get
(
"categories"
).
getAsJsonArray
(),
BackupCategory_v4
.
class
));
final
Template
template
=
new
Template
();
template
.
setTemplateName
(
templateName
);
template
.
setTags
(
super
.
parseTags
(
templateObject
));
database
.
setTransactions
(
BackupItemParser
.
parseItems
(
root
.
get
(
"transactions"
).
getAsJsonArray
(),
BackupTransaction_v4
.
class
));
fixMissingIsExpenditure
(
database
.
getTransactions
());
final
JsonElement
element
=
templateObject
.
get
(
"amount"
);
if
(
element
!=
null
)
{
template
.
setAmount
(
element
.
getAsInt
());
}
database
.
setTemplates
(
BackupItemParser
.
parseItems
(
root
.
get
(
"templates"
).
getAsJsonArray
(),
BackupTemplate_v4
.
class
));
fixMissingIsExpenditure
(
database
.
getTemplates
());
final
JsonElement
name
=
templateObject
.
get
(
"name"
);
if
(
name
!=
null
)
{
template
.
setName
(
name
.
getAsString
());
return
database
;
}
final
JsonElement
description
=
templateObject
.
get
(
"description"
);
if
(
description
!=
null
)
private
void
fixMissingIsExpenditure
(
List
<?
extends
BackupTransactionBase_v4
>
items
)
{
template
.
setDescription
(
description
.
getAsString
());
}
final
Optional
<
Integer
>
categoryOptional
=
parseIDOfElementIfExists
(
templateObject
,
"category"
);
categoryOptional
.
ifPresent
(
integer
->
template
.
setCategory
(
super
.
getCategoryByID
(
integer
)));
final
Optional
<
Integer
>
accountOptional
=
parseIDOfElementIfExists
(
templateObject
,
"account"
);
accountOptional
.
ifPresent
(
integer
->
template
.
setAccount
(
super
.
getAccountByID
(
integer
)));
final
Optional
<
Integer
>
transferAccountOptional
=
parseIDOfElementIfExists
(
templateObject
,
"transferAccount"
);
transferAccountOptional
.
ifPresent
(
integer
->
template
.
setTransferAccount
(
super
.
getAccountByID
(
integer
)));
handleIsExpenditure
(
templateObject
,
template
);
parsedTemplates
.
add
(
template
);
}
return
parsedTemplates
;
}
protected
Optional
<
Integer
>
parseIDOfElementIfExists
(
JsonObject
jsonObject
,
String
elementName
)
for
(
BackupTransactionBase_v4
item
:
items
)
{
final
JsonElement
element
=
jsonObject
.
get
(
elementName
);
if
(
element
!=
null
)
if
(
item
.
getExpenditure
()
!=
null
)
{
return
Optional
.
of
(
element
.
getAsJsonObject
().
get
(
"ID"
).
getAsInt
());
}
return
Optional
.
empty
();
continue
;
}
protected
void
handleIsExpenditure
(
JsonObject
jsonObject
,
TransactionBase
transactionBase
)
if
(
item
.
getAmount
()
==
null
)
{
final
JsonElement
isExpenditure
=
jsonObject
.
get
(
"isExpenditure"
);
if
(
isExpenditure
==
null
)
{
if
(
transactionBase
.
getAmount
()
==
null
)
{
transactionBase
.
setIsExpenditure
(
true
);
item
.
setExpenditure
(
true
);
}
else
{
transactionBase
.
setIsExpenditure
(
transactionBase
.
getAmount
()
<=
0
);
}
item
.
setExpenditure
(
item
.
getAmount
()
<=
0
);
}
else
{
transactionBase
.
setIsExpenditure
(
isExpenditure
.
getAsBoolean
());
}
}
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment