2424
2525import java .util .Collection ;
2626import java .util .HashMap ;
27+ import java .util .LinkedList ;
2728import java .util .List ;
2829import java .util .Map ;
2930import java .util .Objects ;
31+ import java .util .Optional ;
3032import java .util .Set ;
3133import java .util .UUID ;
3234import java .util .stream .Collectors ;
@@ -60,6 +62,8 @@ public class RepositoryContentFetcher {
6062
6163 private static Set <String > disabledStructuralFeatures = Set .of ("importedMembership" );
6264
65+ private final List <Issue > issues = new LinkedList <>();
66+
6367 /**
6468 * @param repositoryProject as the input
6569 * @param tracker index based on element UUIDs
@@ -76,6 +80,7 @@ public RepositoryContentFetcher(RepositoryProject repositoryProject, EObjectUUID
7680 * @return project diff. (currently just a container for the entire model downloaded from the project)
7781 */
7882 public ProjectDelta fetch () {
83+ issues .clear ();
7984 boolean success = repositoryProject .loadRemote ();
8085
8186 if (!success ) throw new RuntimeException ("Couldn't download project: " + repositoryProject .getProjectName ());
@@ -94,8 +99,8 @@ public ProjectDelta fetch() {
9499 tracker .forEachTrackedUserElement ((id , langElement ) -> {
95100 Element element = repositoryProject .getElement (id );
96101 if (element != null && isNotStandardLibraryElement (langElement )) {
97- tranformCrossreferences (langElement , element );
98- tranformAttributes (langElement , element );
102+ tranformCrossReferences (langElement , element );
103+ transformAttributes (langElement , element );
99104 //set importedMemberships for MembershipImports
100105 setImportedMembership (langElement , element );
101106 }
@@ -130,10 +135,9 @@ private void setImportedMembership(EObject langElement, Element dto) {
130135 //this is a workaround to address library element proxies where UUIDs of referenced Memberships aren't stable,
131136 //so the importedElement UUID is used instead.
132137 Object importedMembers = dto .get ("importedElement" );
133- EObject resolvedReference = resolveReference (importedMembers , false );
134- if (resolvedReference != null ) {
138+ resolveReference (importedMembers , false ).ifPresent (resolvedReference -> {
135139 membershipImport .setImportedMembership (((org .omg .sysml .lang .sysml .Element ) resolvedReference ).getOwningMembership ());
136- }
140+ });
137141 }
138142 }
139143
@@ -153,7 +157,7 @@ private boolean isNotStandardLibraryElement(EObject langElement) {
153157 }
154158
155159 private void transformContainmentReferences (EObject langElement , Element remoteElement ) {
156- for (EStructuralFeature feature : getStructuralFeatuers (langElement ))
160+ for (EStructuralFeature feature : getStructuralFeatures (langElement ))
157161 {
158162 if (!feature .isDerived () && remoteElement .get (feature .getName ()) != null ) {
159163 if (feature instanceof EReference reference && reference .isContainment ()) {
@@ -163,8 +167,8 @@ private void transformContainmentReferences(EObject langElement, Element remoteE
163167 }
164168 }
165169
166- private void tranformCrossreferences (EObject langElement , Element remoteElement ) {
167- for (EStructuralFeature feature : getStructuralFeatuers (langElement ))
170+ private void tranformCrossReferences (EObject langElement , Element remoteElement ) {
171+ for (EStructuralFeature feature : getStructuralFeatures (langElement ))
168172 {
169173 if (!feature .isDerived () && remoteElement .containsKey (feature .getName ())) {
170174 if (feature instanceof EReference reference ) {
@@ -178,8 +182,8 @@ private void tranformCrossreferences(EObject langElement, Element remoteElement)
178182 }
179183 }
180184
181- private void tranformAttributes (EObject langElement , Element remoteElement ) {
182- for (EStructuralFeature feature : getStructuralFeatuers (langElement ))
185+ private void transformAttributes (EObject langElement , Element remoteElement ) {
186+ for (EStructuralFeature feature : getStructuralFeatures (langElement ))
183187 {
184188 if (!feature .isDerived () && remoteElement .containsKey (feature .getName ())) {
185189 if (feature instanceof EAttribute reference ) {
@@ -189,7 +193,7 @@ private void tranformAttributes(EObject langElement, Element remoteElement) {
189193 }
190194 }
191195
192- private static Collection <EStructuralFeature > getStructuralFeatuers (EObject langElement ){
196+ private static Collection <EStructuralFeature > getStructuralFeatures (EObject langElement ){
193197 EClass eClass = langElement .eClass ();
194198
195199 Set <EObject > allRedefined = eClass .getEAllStructuralFeatures ().stream ()
@@ -208,33 +212,31 @@ private void transformStructuralFeature(EObject langElement, Element remoteElem
208212 Object value = remoteElement .get (feature .getName ());
209213 if (value instanceof Collection valueCollection ) {
210214 for (Object referenceValue : valueCollection ) {
211- EObject referencedLangElement = resolveReference (referenceValue , isContainment );
212- if (referencedLangElement != null ) {
213- referenceList .add (referencedLangElement );
214- }
215+ resolveReference (referenceValue , isContainment ).ifPresent (referenceList ::add );
215216 }
216217 }
217218 } else {
218219 Object value = remoteElement .get (feature .getName ());
219220 if (value != null ) {
220- langElement .eSet (feature , isReference ? resolveReference (value , isContainment ) : transformAttributeValue (value , feature ));
221+ langElement .eSet (feature , isReference ? resolveReference (value , isContainment ). orElse ( null ) : transformAttributeValue (value , feature ));
221222 }
222223 }
223224 }
224- catch (Exception e ) {
225- System .out .printf ("Unable to set structural feature %s::%s %n" , feature .getEContainingClass ().getName (), feature .getName ());
225+ catch (IllegalArgumentException e ) {
226+ //handle EObject.eGet, eSet errors
227+ issues .add (new Issue (String .format ("Unable to set structural feature %s because %s ha no such feature." , feature .getName (), feature .getEType ().getName ())));
226228 }
227229 }
228230 }
229231
230- private EObject resolveReference (Object referenceValue , boolean isContainment ) {
232+ private Optional < EObject > resolveReference (Object referenceValue , boolean isContainment ) {
231233 if (referenceValue instanceof Map referenceObjectMap ) {
232234 Object refUUID = referenceObjectMap .get ("@id" );
233235 Element referencedElement = repositoryProject .getElement (UUID .fromString (refUUID .toString ()));
234236 EObject referencedLangElement = isContainment ? transform (referencedElement ) : tracker .get (UUID .fromString (refUUID .toString ()));
235- return referencedLangElement ;
237+ return Optional . ofNullable ( referencedLangElement ) ;
236238 }
237- return null ;
239+ return Optional . empty () ;
238240 }
239241
240242
@@ -257,4 +259,20 @@ private Object transformAttributeValue(Object attributeValue, EStructuralFeature
257259
258260 return null ;
259261 }
262+
263+ public List <Issue > getIssues () {
264+ return issues ;
265+ }
266+
267+ public static class Issue {
268+ private final String message ;
269+
270+ public Issue (String message ) {
271+ this .message = message ;
272+ }
273+
274+ public String getMessage () {
275+ return message ;
276+ }
277+ }
260278}
0 commit comments