Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
J
jh-boot
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Jan Hrabal
jh-boot
Commits
49565093
Commit
49565093
authored
Oct 22, 2019
by
Jan Hrabal
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
syncs
parent
df9deb94
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
135 additions
and
0 deletions
+135
-0
AbstractHibernateAttachmentService.java
...h/boot/attachment/AbstractHibernateAttachmentService.java
+1
-0
AttachmentService.java
src/main/java/com/jh/boot/attachment/AttachmentService.java
+44
-0
CollectionSync.java
src/main/java/com/jh/boot/jpa/CollectionSync.java
+90
-0
No files found.
src/main/java/com/jh/boot/attachment/AbstractHibernateAttachmentService.java
View file @
49565093
...
@@ -45,6 +45,7 @@ public abstract class AbstractHibernateAttachmentService implements AttachmentSe
...
@@ -45,6 +45,7 @@ public abstract class AbstractHibernateAttachmentService implements AttachmentSe
}
}
}
}
protected
abstract
void
deleteData
(
Attachment
attachment
);
protected
abstract
void
deleteData
(
Attachment
attachment
);
...
...
src/main/java/com/jh/boot/attachment/AttachmentService.java
View file @
49565093
...
@@ -3,6 +3,8 @@ package com.jh.boot.attachment;
...
@@ -3,6 +3,8 @@ package com.jh.boot.attachment;
import
java.io.InputStream
;
import
java.io.InputStream
;
import
java.util.List
;
import
java.util.List
;
import
org.springframework.web.multipart.MultipartFile
;
public
interface
AttachmentService
{
public
interface
AttachmentService
{
Attachment
saveAttachment
(
Attachment
attachment
,
InputStream
content
);
Attachment
saveAttachment
(
Attachment
attachment
,
InputStream
content
);
...
@@ -17,4 +19,46 @@ public interface AttachmentService {
...
@@ -17,4 +19,46 @@ public interface AttachmentService {
void
deleteAttachment
(
Long
attachmentId
);
void
deleteAttachment
(
Long
attachmentId
);
// default methods
public
default
Attachment
addAttachment
(
String
objectType
,
Long
objectId
,
MultipartFile
file
)
{
return
addAttachment
(
null
,
objectType
,
objectId
,
file
);
}
public
default
Attachment
addAttachment
(
Long
unitId
,
String
objectType
,
Long
objectId
,
MultipartFile
file
)
{
Attachment
a
=
new
Attachment
();
a
.
setFilename
(
file
.
getOriginalFilename
());
a
.
setSize
(
file
.
getSize
());
a
.
setObjectId
(
objectId
);
a
.
setObjectType
(
objectType
);
try
{
saveAttachment
(
a
,
file
.
getInputStream
());
}
catch
(
Exception
e
)
{
throw
new
RuntimeException
(
"Cannot upload attachment"
,
e
);
}
return
a
;
}
public
default
Attachment
syncAttachment
(
String
objectType
,
Long
objectId
,
Attachment
currentAttachment
,
Attachment
newAttachment
,
MultipartFile
file
)
{
return
syncAttachment
(
null
,
objectType
,
objectId
,
currentAttachment
,
newAttachment
,
file
);
}
public
default
Attachment
syncAttachment
(
Long
unitId
,
String
objectType
,
Long
objectId
,
Attachment
currentAttachment
,
Attachment
newAttachment
,
MultipartFile
file
)
{
Attachment
a
=
null
;
if
(
currentAttachment
!=
null
&&
newAttachment
==
null
)
{
//delete current attachment
deleteAttachment
(
currentAttachment
.
getId
());
}
if
(
file
!=
null
)
{
a
=
addAttachment
(
unitId
,
objectType
,
objectId
,
file
);
}
else
{
a
=
currentAttachment
;
}
return
a
;
}
}
}
src/main/java/com/jh/boot/jpa/CollectionSync.java
0 → 100644
View file @
49565093
package
com
.
jh
.
boot
.
jpa
;
import
java.util.*
;
//TODO move to jh-boot
public
abstract
class
CollectionSync
<
R
extends
AbstractIdEntity
,
T
extends
AbstractIdEntity
>
{
private
AbstractHibernateRepository
repo
;
private
boolean
deleteOrphans
;
protected
CollectionSync
(
AbstractHibernateRepository
repo
)
{
this
(
repo
,
false
);
}
protected
CollectionSync
(
AbstractHibernateRepository
repo
,
boolean
deleteOrphans
)
{
this
.
repo
=
repo
;
this
.
deleteOrphans
=
deleteOrphans
;
}
public
<
C
extends
Collection
<
T
>>
C
synchronize
(
R
root
,
C
existing
,
Collection
<
T
>
fresh
,
Class
<
C
>
clazz
)
{
//decide whether Set or List
if
(
existing
==
null
)
{
if
(
Set
.
class
.
isAssignableFrom
(
clazz
))
{
existing
=
(
C
)
new
HashSet
<
T
>();
}
else
if
(
List
.
class
.
isAssignableFrom
(
clazz
))
{
existing
=
(
C
)
new
ArrayList
<
T
>();
}
else
{
throw
new
IllegalArgumentException
(
"Invalid collection type"
);
}
}
List
<
T
>
toBeAdded
=
new
ArrayList
<>();
Map
<
Long
,
T
>
map
=
new
HashMap
();
if
(
fresh
!=
null
)
{
for
(
T
t
:
fresh
)
{
if
(
t
.
getId
()
==
null
)
{
toBeAdded
.
add
(
t
);
}
else
{
map
.
put
(
t
.
getId
(),
t
);
}
}
}
Iterator
<
T
>
it
=
existing
.
iterator
();
while
(
it
.
hasNext
())
{
T
e
=
it
.
next
();
T
f
=
map
.
get
(
e
.
getId
());
if
(
f
==
null
)
{
it
.
remove
();
doRemove
(
e
);
continue
;
}
doMerge
(
e
,
f
);
}
for
(
T
t
:
toBeAdded
)
{
setRootToEntity
(
root
,
t
);
doPersistNew
(
t
);
existing
.
add
(
t
);
}
return
existing
;
}
protected
void
doRemove
(
T
entity
)
{
if
(
deleteOrphans
)
{
repo
.
delete
(
entity
);
}
}
protected
void
doPersistNew
(
T
entity
)
{
repo
.
save
(
entity
);
}
protected
abstract
void
setRootToEntity
(
R
root
,
T
entity
);
protected
abstract
void
doMerge
(
T
existing
,
T
fresh
);
protected
boolean
isDeleteOrphans
()
{
return
deleteOrphans
;
}
protected
AbstractHibernateRepository
getRepo
()
{
return
repo
;
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment