Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
W
WE.SpreadsheetImport
Project overview
Project overview
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Labels
Merge Requests
0
Merge Requests
0
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Commits
Open sidebar
packages
WE.SpreadsheetImport
Commits
ed623bd8
Commit
ed623bd8
authored
Nov 03, 2016
by
Simon Gadient
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[RFT] Get identifier mapping properties function
refs KIME-4583
parent
0f755ee6
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
72 additions
and
54 deletions
+72
-54
Classes/WE/SpreadsheetImport/SpreadsheetImportService.php
Classes/WE/SpreadsheetImport/SpreadsheetImportService.php
+72
-54
No files found.
Classes/WE/SpreadsheetImport/SpreadsheetImportService.php
View file @
ed623bd8
...
...
@@ -69,18 +69,29 @@ class SpreadsheetImportService {
protected
$validatorResolver
;
/**
* Inverse SpreadsheetImport mapping array
* Inverse array of SpreadsheetDomain mapping array property. Always use the getter function, which will process the
* property in case it is not set.
*
* @var array
*/
private
$inverseSpreadsheetImportMapping
;
/**
* Identifier properties of SpreadsheetDomain mapping array. Always use the getter function, which will process the
* property in case it is not set.
*
* @var array
*/
private
$mappingIdentifierProperties
;
/**
* @param \WE\SpreadsheetImport\Domain\Model\SpreadsheetImport $spreadsheetImport
*
* @return $this
*/
public
function
init
(
SpreadsheetImport
$spreadsheetImport
)
{
$this
->
inverseSpreadsheetImportMapping
=
array
();
$this
->
mappingIdentifierProperties
=
array
();
$this
->
spreadsheetImport
=
$spreadsheetImport
;
$this
->
domain
=
$this
->
settings
[
$spreadsheetImport
->
getContext
()][
'domain'
];
...
...
@@ -154,14 +165,13 @@ class SpreadsheetImportService {
$processedObjectIds
=
array
();
$objectRepository
=
$this
->
getDomainRepository
();
$objectValidator
=
$this
->
validatorResolver
->
getBaseValidatorConjunction
(
$this
->
domain
);
$identifierProperties
=
$this
->
getDomainMappingIdentifierProperties
();
$sheet
=
$this
->
getFileActiveSheet
();
$persistRecordsChunkSize
=
intval
(
$this
->
settings
[
'persistRecordsChunkSize'
]);
$totalCount
=
0
;
/** @var \PHPExcel_Worksheet_Row $row */
foreach
(
$sheet
->
getRowIterator
(
2
)
as
$row
)
{
$totalCount
++
;
$object
=
$this
->
findObjectByIdentifierPropertiesPerRow
(
$
identifierProperties
,
$
row
);
$object
=
$this
->
findObjectByIdentifierPropertiesPerRow
(
$row
);
if
(
is_object
(
$object
))
{
$processedObjectIds
[]
=
$this
->
persistenceManager
->
getIdentifierByObject
(
$object
);
if
(
$this
->
spreadsheetImport
->
isUpdating
())
{
...
...
@@ -232,37 +242,36 @@ class SpreadsheetImportService {
/**
* @return array
*/
private
function
getDomainMappingIdentifierProperties
()
{
// TODO: Don't use the annotation properties but the SpreadsheetImport mapping since we store the Mapping object there as well
$domainMappingProperties
=
array
();
$properties
=
$this
->
reflectionService
->
getPropertyNamesByAnnotation
(
$this
->
domain
,
Mapping
::
class
);
foreach
(
$properties
as
$property
)
{
/** @var Mapping $mapping */
$mapping
=
$this
->
reflectionService
->
getPropertyAnnotation
(
$this
->
domain
,
$property
,
Mapping
::
class
);
if
(
$mapping
->
identifier
)
{
$domainMappingProperties
[
$property
]
=
$mapping
;
private
function
getMappingIdentifierProperties
()
{
if
(
empty
(
$this
->
mappingIdentifierProperties
))
{
foreach
(
$this
->
spreadsheetImport
->
getMapping
()
as
$property
=>
$columnMapping
)
{
/** @var Mapping $mapping */
$mapping
=
$columnMapping
[
'mapping'
];
if
(
$mapping
->
identifier
)
{
$this
->
mappingIdentifierProperties
[
$property
]
=
$columnMapping
;
}
}
}
return
$
domainMapping
Properties
;
return
$
this
->
mappingIdentifier
Properties
;
}
/**
* @param array $identifierProperties
* @param \PHPExcel_Worksheet_Row $row
*
* @return null|object
*/
private
function
findObjectByIdentifierPropertiesPerRow
(
array
$identifierProperties
,
\PHPExcel_Worksheet_Row
$row
)
{
private
function
findObjectByIdentifierPropertiesPerRow
(
\PHPExcel_Worksheet_Row
$row
)
{
$query
=
$this
->
getDomainRepository
()
->
createQuery
();
$constraints
=
array
();
$spreadsheetImportMapping
=
$this
->
spreadsheetImport
->
getMapping
();
/** @var Mapping $mapping */
foreach
(
$identifierProperties
as
$property
=>
$mapping
)
{
$column
=
$spreadsheetImportMapping
[
$property
][
'column'
];
$identifierProperties
=
$this
->
getMappingIdentifierProperties
();
foreach
(
$identifierProperties
as
$property
=>
$columnMapping
)
{
$column
=
$columnMapping
[
'column'
];
/** @var Mapping $mapping */
$mapping
=
$columnMapping
[
'mapping'
];
$propertyName
=
$mapping
->
queryPropertyName
?:
$property
;
/** @var \PHPExcel_Worksheet_RowCellIterator $cellIterator */
$cellIterator
=
$row
->
getCellIterator
(
$column
,
$column
);
$value
=
$cellIterator
->
current
()
->
getValue
();
$propertyName
=
$mapping
->
queryPropertyName
?:
$property
;
$constraints
[]
=
$query
->
equals
(
$propertyName
,
$value
);
}
$this
->
mergeQueryConstraintsWithArgumentIdentifiers
(
$query
,
$constraints
);
...
...
@@ -293,6 +302,18 @@ class SpreadsheetImportService {
}
}
/**
* @param array $identifiers
*
* @return \TYPO3\Flow\Persistence\QueryResultInterface
*/
private
function
findObjectsByExcludedIds
(
array
$identifiers
)
{
$query
=
$this
->
getDomainRepository
()
->
createQuery
();
$constraints
[]
=
$query
->
logicalNot
(
$query
->
in
(
'Persistence_Object_Identifier'
,
$identifiers
));
$this
->
mergeQueryConstraintsWithArguments
(
$query
,
$constraints
);
return
$query
->
matching
(
$query
->
logicalAnd
(
$constraints
))
->
execute
();
}
/**
* @param \TYPO3\Flow\Persistence\QueryInterface $query
* @param array $constraints
...
...
@@ -312,24 +333,43 @@ class SpreadsheetImportService {
}
/**
* @param array $identifiers
*
* @return \TYPO3\Flow\Persistence\QueryResultInterface
* @param object $object
* @param \PHPExcel_Worksheet_Row $row
*/
private
function
findObjectsByExcludedIds
(
array
$identifiers
)
{
$query
=
$this
->
getDomainRepository
()
->
createQuery
();
$constraints
[]
=
$query
->
logicalNot
(
$query
->
in
(
'Persistence_Object_Identifier'
,
$identifiers
));
$this
->
mergeQueryConstraintsWithArguments
(
$query
,
$constraints
);
return
$query
->
matching
(
$query
->
logicalAnd
(
$constraints
))
->
execute
();
private
function
setObjectPropertiesByRow
(
$object
,
$row
)
{
/* Set the argument properties before the mapping properties, as mapping property setters might be dependent on
argument property values */
$this
->
setObjectArgumentProperties
(
$object
);
$this
->
setObjectSpreadsheetImportMappingProperties
(
$object
,
$row
);
}
/**
* @param $object
*/
private
function
setObjectArgumentProperties
(
$object
)
{
$contextArguments
=
$this
->
settings
[
$this
->
spreadsheetImport
->
getContext
()][
'arguments'
];
if
(
is_array
(
$contextArguments
))
{
$arguments
=
$this
->
spreadsheetImport
->
getArguments
();
foreach
(
$contextArguments
as
$contextArgument
)
{
$name
=
$contextArgument
[
'name'
];
if
(
array_key_exists
(
$name
,
$arguments
))
{
if
(
array_key_exists
(
'domain'
,
$contextArgument
))
{
$value
=
$this
->
propertyMapper
->
convert
(
$arguments
[
$name
],
$contextArgument
[
'domain'
]);
}
else
{
$value
=
$arguments
[
$name
];
}
$setter
=
'set'
.
ucfirst
(
$name
);
$object
->
$setter
(
$value
);
}
}
}
}
/**
* @param object $object
* @param \PHPExcel_Worksheet_Row $row
*/
private
function
setObjectPropertiesByRow
(
$object
,
$row
)
{
// Set the arguments first as mapping property setters might be dependent on argument properties
$this
->
setObjectArgumentProperties
(
$object
);
private
function
setObjectSpreadsheetImportMappingProperties
(
$object
,
$row
)
{
$inverseSpreadsheetImportMapping
=
$this
->
getInverseSpreadsheetImportMapping
();
/** @var \PHPExcel_Cell $cell */
foreach
(
$row
->
getCellIterator
()
as
$cell
)
{
...
...
@@ -349,7 +389,7 @@ class SpreadsheetImportService {
/**
* Return an inverse SpreadsheetImport mapping array. It flips the property and column attribute and returns it as a
* 3-dim array instead of a 2-dim array. Th
e reason for that is
the case when the same column is assigned to multiple
* 3-dim array instead of a 2-dim array. Th
is is done for
the case when the same column is assigned to multiple
* properties.
*/
private
function
getInverseSpreadsheetImportMapping
()
{
...
...
@@ -363,26 +403,4 @@ class SpreadsheetImportService {
}
return
$this
->
inverseSpreadsheetImportMapping
;
}
/**
* @param $object
*/
private
function
setObjectArgumentProperties
(
$object
)
{
$contextArguments
=
$this
->
settings
[
$this
->
spreadsheetImport
->
getContext
()][
'arguments'
];
if
(
is_array
(
$contextArguments
))
{
$arguments
=
$this
->
spreadsheetImport
->
getArguments
();
foreach
(
$contextArguments
as
$contextArgument
)
{
$name
=
$contextArgument
[
'name'
];
if
(
array_key_exists
(
$name
,
$arguments
))
{
if
(
array_key_exists
(
'domain'
,
$contextArgument
))
{
$value
=
$this
->
propertyMapper
->
convert
(
$arguments
[
$name
],
$contextArgument
[
'domain'
]);
}
else
{
$value
=
$arguments
[
$name
];
}
$setter
=
'set'
.
ucfirst
(
$name
);
$object
->
$setter
(
$value
);
}
}
}
}
}
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