idx
int64
0
41.2k
question
stringlengths
74
4.04k
target
stringlengths
7
750
41,200
private void injectExistingEntityTypeAttributeIdentifiers ( List < ? extends EntityType > entityTypes ) { Map < String , EntityType > existingEntityTypeMap = dataService . findAll ( ENTITY_TYPE_META_DATA , EntityType . class ) . collect ( toMap ( EntityType :: getId , entityType -> entityType ) ) ; entityTypes . forEach ( entityType -> { EntityType existingEntityType = existingEntityTypeMap . get ( entityType . getId ( ) ) ; if ( existingEntityType != null ) { entityType . setId ( existingEntityType . getId ( ) ) ; Map < String , Attribute > existingAttrs = stream ( existingEntityType . getOwnAllAttributes ( ) ) . collect ( toMap ( Attribute :: getName , Function . identity ( ) ) ) ; entityType . getOwnAllAttributes ( ) . forEach ( attr -> { Attribute existingAttr = existingAttrs . get ( attr . getName ( ) ) ; if ( existingAttr != null ) { attr . setIdentifier ( existingAttr . getIdentifier ( ) ) ; } } ) ; } } ) ; }
Inject existing attribute identifiers in system entity types
41,201
private void addAttributeInternal ( EntityType entityType , Attribute attr ) { if ( ! isPersisted ( attr ) ) { return ; } if ( isMultipleReferenceType ( attr ) ) { createJunctionTable ( entityType , attr ) ; if ( attr . getDefaultValue ( ) != null && ! attr . isNillable ( ) ) { @ SuppressWarnings ( "unchecked" ) Iterable < Entity > defaultRefEntities = ( Iterable < Entity > ) AttributeUtils . getDefaultTypedValue ( attr ) ; if ( ! Iterables . isEmpty ( defaultRefEntities ) ) { createJunctionTableRows ( entityType , attr , defaultRefEntities ) ; } } } else { createColumn ( entityType , attr ) ; } }
Add attribute to entityType .
41,202
private static boolean isPersisted ( Attribute attr ) { return ! attr . hasExpression ( ) && attr . getDataType ( ) != COMPOUND && ! ( attr . getDataType ( ) == ONE_TO_MANY && attr . isMappedBy ( ) ) ; }
Indicates if the attribute is persisted in the database . Compound attributes computed attributes with an expression and one - to - many mappedBy attributes are not persisted .
41,203
private void updateColumn ( EntityType entityType , Attribute attr , Attribute updatedAttr ) { if ( ! Objects . equals ( attr . isNillable ( ) , updatedAttr . isNillable ( ) ) ) { updateNillable ( entityType , attr , updatedAttr ) ; } if ( ! Objects . equals ( attr . isUnique ( ) , updatedAttr . isUnique ( ) ) ) { updateUnique ( entityType , attr , updatedAttr ) ; } if ( ! Objects . equals ( attr . isReadOnly ( ) , updatedAttr . isReadOnly ( ) ) ) { updateReadonly ( entityType , attr , updatedAttr ) ; } if ( ! Objects . equals ( attr . getDataType ( ) , updatedAttr . getDataType ( ) ) ) { if ( updatedAttr . isReadOnly ( ) ) { dropTableTriggers ( entityType ) ; } updateDataType ( entityType , attr , updatedAttr ) ; if ( updatedAttr . isReadOnly ( ) ) { createTableTriggers ( entityType ) ; } } if ( attr . hasRefEntity ( ) && updatedAttr . hasRefEntity ( ) && ! attr . getRefEntity ( ) . getId ( ) . equals ( updatedAttr . getRefEntity ( ) . getId ( ) ) ) { updateRefEntity ( entityType , attr , updatedAttr ) ; } if ( ! Objects . equals ( attr . getEnumOptions ( ) , updatedAttr . getEnumOptions ( ) ) ) { updateEnumOptions ( entityType , attr , updatedAttr ) ; } }
Updates database column based on attribute changes .
41,204
private void updateRefEntity ( EntityType entityType , Attribute attr , Attribute updatedAttr ) { if ( isSingleReferenceType ( attr ) && isSingleReferenceType ( updatedAttr ) ) { dropForeignKey ( entityType , attr ) ; if ( attr . getRefEntity ( ) . getIdAttribute ( ) . getDataType ( ) != updatedAttr . getRefEntity ( ) . getIdAttribute ( ) . getDataType ( ) ) { updateColumnDataType ( entityType , updatedAttr ) ; } createForeignKey ( entityType , updatedAttr ) ; } else if ( isMultipleReferenceType ( attr ) && isMultipleReferenceType ( updatedAttr ) ) { throw new MolgenisDataException ( format ( "Updating entity [%s] attribute [%s] referenced entity from [%s] to [%s] not allowed for type [%s]" , entityType . getId ( ) , attr . getName ( ) , attr . getRefEntity ( ) . getId ( ) , updatedAttr . getRefEntity ( ) . getId ( ) , updatedAttr . getDataType ( ) . toString ( ) ) ) ; } }
Updates foreign keys based on referenced entity changes .
41,205
private void updateEnumOptions ( EntityType entityType , Attribute attr , Attribute updatedAttr ) { if ( attr . getDataType ( ) == ENUM ) { if ( updatedAttr . getDataType ( ) == ENUM ) { dropCheckConstraint ( entityType , attr ) ; createCheckConstraint ( entityType , updatedAttr ) ; } else { dropCheckConstraint ( entityType , attr ) ; } } else { if ( updatedAttr . getDataType ( ) == ENUM ) { createCheckConstraint ( entityType , updatedAttr ) ; } } }
Updates check constraint based on enum value changes .
41,206
private void updateDataType ( EntityType entityType , Attribute attr , Attribute updatedAttr ) { Attribute idAttr = entityType . getIdAttribute ( ) ; if ( idAttr != null && idAttr . getName ( ) . equals ( attr . getName ( ) ) ) { throw new MolgenisDataException ( format ( "Data type of entity [%s] attribute [%s] cannot be modified, because [%s] is an ID attribute." , entityType . getId ( ) , attr . getName ( ) , attr . getName ( ) ) ) ; } if ( isSingleReferenceType ( attr ) && isSingleReferenceType ( updatedAttr ) ) { return ; } if ( isMultipleReferenceType ( attr ) && isMultipleReferenceType ( updatedAttr ) ) { return ; } if ( isSingleReferenceType ( attr ) && ! isReferenceType ( updatedAttr ) ) { dropForeignKey ( entityType , attr ) ; } if ( isSingleReferenceType ( attr ) && isMultipleReferenceType ( updatedAttr ) ) { updateManyToOneToManyToMany ( entityType , attr , updatedAttr ) ; } else if ( isMultipleReferenceType ( attr ) && isSingleReferenceType ( updatedAttr ) ) { updateManyToManyToManyToOne ( entityType , attr , updatedAttr ) ; } else { updateColumnDataType ( entityType , updatedAttr ) ; } if ( ! isReferenceType ( attr ) && isSingleReferenceType ( updatedAttr ) ) { createForeignKey ( entityType , updatedAttr ) ; } }
Updates column data type and foreign key constraints based on data type update .
41,207
private void updateUnique ( EntityType entityType , Attribute attr , Attribute updatedAttr ) { if ( attr . isUnique ( ) && ! updatedAttr . isUnique ( ) ) { Attribute idAttr = entityType . getIdAttribute ( ) ; if ( idAttr != null && idAttr . getName ( ) . equals ( attr . getName ( ) ) ) { throw new MolgenisDataException ( format ( "ID attribute [%s] of entity [%s] must be unique" , attr . getName ( ) , entityType . getId ( ) ) ) ; } dropUniqueKey ( entityType , updatedAttr ) ; } else if ( ! attr . isUnique ( ) && updatedAttr . isUnique ( ) ) { createUniqueKey ( entityType , updatedAttr ) ; } }
Updates unique constraint based on attribute unique changes .
41,208
private void updateReadonly ( EntityType entityType , Attribute attr , Attribute updatedAttr ) { Map < String , Attribute > readonlyTableAttrs = getTableAttributesReadonly ( entityType ) . collect ( toLinkedMap ( Attribute :: getName , Function . identity ( ) ) ) ; if ( ! readonlyTableAttrs . isEmpty ( ) ) { dropTableTriggers ( entityType ) ; } if ( attr . isReadOnly ( ) && ! updatedAttr . isReadOnly ( ) ) { readonlyTableAttrs . remove ( attr . getName ( ) ) ; } else if ( ! attr . isReadOnly ( ) && updatedAttr . isReadOnly ( ) ) { readonlyTableAttrs . put ( updatedAttr . getName ( ) , updatedAttr ) ; } if ( ! readonlyTableAttrs . isEmpty ( ) ) { createTableTriggers ( entityType , readonlyTableAttrs . values ( ) ) ; } }
Updates triggers and functions based on attribute readonly changes .
41,209
private void registerRefEntityIndexActions ( ) { getEntityType ( ) . getMappedByAttributes ( ) . forEach ( mappedByAttr -> { EntityType refEntity = mappedByAttr . getRefEntity ( ) ; indexActionRegisterService . register ( refEntity , null ) ; } ) ; getEntityType ( ) . getInversedByAttributes ( ) . forEach ( inversedByAttr -> { EntityType refEntity = inversedByAttr . getRefEntity ( ) ; indexActionRegisterService . register ( refEntity , null ) ; } ) ; }
Register index actions for entity types with bidirectional attribute values .
41,210
private void registerRefEntityIndexActions ( Entity entity ) { getEntityType ( ) . getMappedByAttributes ( ) . forEach ( mappedByAttr -> { EntityType mappedByAttrRefEntity = mappedByAttr . getRefEntity ( ) ; entity . getEntities ( mappedByAttr . getName ( ) ) . forEach ( refEntity -> indexActionRegisterService . register ( mappedByAttrRefEntity , refEntity . getIdValue ( ) ) ) ; } ) ; getEntityType ( ) . getInversedByAttributes ( ) . forEach ( inversedByAttr -> { Entity refEntity = entity . getEntity ( inversedByAttr . getName ( ) ) ; if ( refEntity != null ) { EntityType inversedByAttrRefEntity = inversedByAttr . getRefEntity ( ) ; indexActionRegisterService . register ( inversedByAttrRefEntity , refEntity . getIdValue ( ) ) ; } } ) ; }
Register index actions for the given entity for entity types with bidirectional attribute values .
41,211
private Multimap < Object , Object > selectMrefIDsForAttribute ( EntityType entityType , AttributeType idAttributeDataType , Attribute mrefAttr , Set < Object > ids , AttributeType refIdDataType ) { Stopwatch stopwatch = null ; if ( LOG . isTraceEnabled ( ) ) stopwatch = createStarted ( ) ; String junctionTableSelect = getSqlJunctionTableSelect ( entityType , mrefAttr , ids . size ( ) ) ; LOG . trace ( "SQL: {}" , junctionTableSelect ) ; Multimap < Object , Object > mrefIDs = ArrayListMultimap . create ( ) ; jdbcTemplate . query ( junctionTableSelect , getJunctionTableRowCallbackHandler ( idAttributeDataType , refIdDataType , mrefIDs ) , ids . toArray ( ) ) ; if ( LOG . isTraceEnabled ( ) ) LOG . trace ( "Selected {} ID values for MREF attribute {} in {}" , mrefIDs . values ( ) . stream ( ) . collect ( counting ( ) ) , mrefAttr . getName ( ) , stopwatch ) ; return mrefIDs ; }
Selects MREF IDs for an MREF attribute from the junction table in the order of the MREF attribute value .
41,212
void appendLog ( String formattedMessage ) { if ( logTruncated ) return ; String combined = join ( getLog ( ) , formattedMessage ) ; if ( combined . length ( ) > MAX_LOG_LENGTH ) { String truncated = abbreviate ( combined , MAX_LOG_LENGTH - TRUNCATION_BANNER . length ( ) * 2 - 2 ) ; combined = join ( new String [ ] { TRUNCATION_BANNER , truncated , TRUNCATION_BANNER } , "\n" ) ; logTruncated = true ; } setLog ( combined ) ; }
Appends a log message to the execution log . The first time the log exceeds MAX_LOG_LENGTH it gets truncated and the TRUNCATION_BANNER gets added . Subsequent calls to appendLog will be ignored .
41,213
public ScriptResult runScript ( String scriptName , Map < String , Object > parameters ) { Script script = dataService . query ( SCRIPT , Script . class ) . eq ( ScriptMetadata . NAME , scriptName ) . findOne ( ) ; if ( script == null ) { throw new UnknownEntityException ( SCRIPT , scriptName ) ; } if ( script . getParameters ( ) != null ) { for ( ScriptParameter param : script . getParameters ( ) ) { if ( ! parameters . containsKey ( param . getName ( ) ) ) { throw new GenerateScriptException ( "Missing parameter [" + param + "]" ) ; } } } Map < String , Object > scriptParameters = new HashMap < > ( parameters ) ; if ( script . isGenerateToken ( ) ) { String token = tokenService . generateAndStoreToken ( SecurityUtils . getCurrentUsername ( ) , "For script " + script . getName ( ) ) ; scriptParameters . put ( "molgenisToken" , token ) ; } ScriptRunner scriptRunner = scriptRunnerFactory . getScriptRunner ( script . getScriptType ( ) . getName ( ) ) ; FileMeta fileMeta = null ; if ( scriptRunner . hasFileOutput ( script ) ) { String name = generateRandomString ( ) ; String resultFileExtension = script . getResultFileExtension ( ) ; if ( resultFileExtension != null ) { name += "." + script . getResultFileExtension ( ) ; } File file = fileStore . getFileUnchecked ( name ) ; scriptParameters . put ( "outputFile" , file . getAbsolutePath ( ) ) ; fileMeta = createFileMeta ( name , file ) ; dataService . add ( FILE_META , fileMeta ) ; } String output = scriptRunner . runScript ( script , scriptParameters ) ; return new ScriptResult ( fileMeta , output ) ; }
Run a script with parameters .
41,214
private void convertIdtoLabelLabels ( List < Object > idLabels , EntityType entityType , DataService dataService ) { final int nrLabels = idLabels . size ( ) ; if ( nrLabels > 0 ) { Stream < Object > idLabelsWithoutNull = idLabels . stream ( ) . filter ( Objects :: nonNull ) . map ( untypedIdLabel -> EntityUtils . getTypedValue ( untypedIdLabel . toString ( ) , entityType . getIdAttribute ( ) ) ) ; Map < String , Entity > idToLabelMap = new HashMap < > ( ) ; dataService . findAll ( entityType . getId ( ) , idLabelsWithoutNull ) . forEach ( entity -> idToLabelMap . put ( entity . getIdValue ( ) . toString ( ) , entity ) ) ; for ( int i = 0 ; i < nrLabels ; ++ i ) { Object id = idLabels . get ( i ) ; if ( id != null ) { idLabels . set ( i , idToLabelMap . get ( id . toString ( ) ) ) ; } } } }
Convert matrix labels that contain ids to label attribute values . Keeps in mind that the last label on a axis is Total .
41,215
public static String getCurrentUsername ( ) { Authentication authentication = SecurityContextHolder . getContext ( ) . getAuthentication ( ) ; if ( authentication == null ) { return null ; } return getUsername ( authentication ) ; }
Returns the username of the current authentication .
41,216
public static boolean currentUserHasRole ( String ... roles ) { if ( roles == null || roles . length == 0 ) return false ; Authentication authentication = SecurityContextHolder . getContext ( ) . getAuthentication ( ) ; if ( authentication != null ) { Collection < ? extends GrantedAuthority > authorities = authentication . getAuthorities ( ) ; if ( authorities == null ) throw new IllegalStateException ( "No user currently logged in" ) ; for ( String role : roles ) { for ( GrantedAuthority grantedAuthority : authorities ) { if ( role . equals ( grantedAuthority . getAuthority ( ) ) ) return true ; } } } return false ; }
Returns whether the current user has at least one of the given roles
41,217
public static boolean currentUserIsAuthenticated ( ) { Authentication authentication = SecurityContextHolder . getContext ( ) . getAuthentication ( ) ; return authentication != null && authentication . isAuthenticated ( ) && ! currentUserIsAnonymous ( ) ; }
Returns whether the current user is authenticated and not the anonymous user
41,218
public Entity findOneById ( Object id ) { if ( cacheable && ! transactionInformation . isEntireRepositoryDirty ( getEntityType ( ) ) && ! transactionInformation . isEntityDirty ( EntityKey . create ( getEntityType ( ) , id ) ) ) { return l2Cache . get ( delegate ( ) , id ) ; } return delegate ( ) . findOneById ( id ) ; }
Retrieves a single entity by id .
41,219
private List < Entity > findAllBatch ( List < Object > ids ) { String entityTypeId = getEntityType ( ) . getId ( ) ; Multimap < Boolean , Object > partitionedIds = Multimaps . index ( ids , id -> transactionInformation . isEntityDirty ( EntityKey . create ( entityTypeId , id ) ) ) ; Collection < Object > cleanIds = partitionedIds . get ( false ) ; Collection < Object > dirtyIds = partitionedIds . get ( true ) ; Map < Object , Entity > result = newHashMap ( uniqueIndex ( l2Cache . getBatch ( delegate ( ) , cleanIds ) , Entity :: getIdValue ) ) ; result . putAll ( delegate ( ) . findAll ( dirtyIds . stream ( ) ) . collect ( toMap ( Entity :: getIdValue , e -> e ) ) ) ; return ids . stream ( ) . filter ( result :: containsKey ) . map ( result :: get ) . collect ( toList ( ) ) ; }
Retrieves a batch of Entity IDs .
41,220
public void evictAll ( EntityType entityType ) { cache . asMap ( ) . keySet ( ) . stream ( ) . filter ( e -> e . getEntityTypeId ( ) . equals ( entityType . getId ( ) ) ) . forEach ( cache :: invalidate ) ; }
Evicts all entries from the cache that belong to a certain entityType .
41,221
public Optional < CacheHit < Entity > > getIfPresent ( EntityType entityType , Object id ) { EntityKey key = EntityKey . create ( entityType , id ) ; return Optional . ofNullable ( cache . getIfPresent ( key ) ) . map ( cacheHit -> hydrate ( cacheHit , entityType ) ) ; }
Retrieves an entity from the cache if present .
41,222
public void put ( Entity entity ) { EntityType entityType = entity . getEntityType ( ) ; cache . put ( EntityKey . create ( entityType , entity . getIdValue ( ) ) , CacheHit . of ( entityHydration . dehydrate ( entity ) ) ) ; }
Inserts an entity into the cache .
41,223
public static File saveToTempFile ( Part part ) throws IOException { String filename = getOriginalFileName ( part ) ; if ( filename == null ) { return null ; } File file = File . createTempFile ( "molgenis-" , "." + StringUtils . getFilenameExtension ( filename ) ) ; FileCopyUtils . copy ( part . getInputStream ( ) , new FileOutputStream ( file ) ) ; return file ; }
Saves an uploaded file to a tempfile with prefix molgenis - keeps the original file extension
41,224
public static File saveToTempFolder ( Part part ) throws IOException { String filename = getOriginalFileName ( part ) ; if ( filename == null ) { return null ; } File file = new File ( FileUtils . getTempDirectory ( ) , filename ) ; FileCopyUtils . copy ( part . getInputStream ( ) , new FileOutputStream ( file ) ) ; return file ; }
Save an Uploaded file to the temp folder keeping it original name
41,225
public static String getOriginalFileName ( Part part ) { String contentDisposition = part . getHeader ( "content-disposition" ) ; if ( contentDisposition != null ) { for ( String cd : contentDisposition . split ( ";" ) ) { if ( cd . trim ( ) . startsWith ( "filename" ) ) { String path = cd . substring ( cd . indexOf ( '=' ) + 1 ) . replaceAll ( "\"" , "" ) . trim ( ) ; Path filename = Paths . get ( path ) . getFileName ( ) ; return StringUtils . hasText ( filename . toString ( ) ) ? filename . toString ( ) : null ; } } } return null ; }
Get the filename of an uploaded file
41,226
public JobFactory < ScriptJobExecution > scriptJobFactory ( ) { return new JobFactory < ScriptJobExecution > ( ) { public Job < ScriptResult > createJob ( ScriptJobExecution scriptJobExecution ) { final String name = scriptJobExecution . getName ( ) ; final String parameterString = scriptJobExecution . getParameters ( ) ; return progress -> { Map < String , Object > params = new HashMap < > ( ) ; params . putAll ( gson . fromJson ( parameterString , MAP_TOKEN ) ) ; params . put ( "scriptJobExecutionId" , scriptJobExecution . getIdValue ( ) ) ; ScriptResult scriptResult = savedScriptRunner . runScript ( name , params ) ; if ( scriptResult . getOutputFile ( ) != null ) { scriptJobExecution . setResultUrl ( format ( "/files/%s" , scriptResult . getOutputFile ( ) . getId ( ) ) ) ; } progress . status ( format ( "Script output:%n%s" , scriptResult . getOutput ( ) ) ) ; return scriptResult ; } ; } } ; }
The Script JobFactory bean .
41,227
@ PreAuthorize ( "hasAnyRole('ROLE_SU')" ) @ PostMapping ( "/upload-logo" ) public String uploadLogo ( @ RequestParam ( "logo" ) Part part , Model model ) throws IOException { String contentType = part . getContentType ( ) ; if ( ( contentType == null ) || ! contentType . startsWith ( "image" ) ) { model . addAttribute ( "errorMessage" , ERRORMESSAGE_LOGO ) ; } else { File logoDir = new File ( fileStore . getStorageDir ( ) + "/logo" ) ; if ( ! logoDir . exists ( ) && ! logoDir . mkdir ( ) ) { throw new IOException ( "Unable to create directory [" + logoDir . getAbsolutePath ( ) + "]" ) ; } String file = "/logo/" + FileUploadUtils . getOriginalFileName ( part ) ; try ( InputStream inputStream = part . getInputStream ( ) ) { fileStore . store ( inputStream , file ) ; } appSettings . setLogoNavBarHref ( file ) ; } return init ( model ) ; }
Upload a new molgenis logo
41,228
public Stream < EntityType > getCompatibleEntityTypes ( EntityType target ) { return dataService . getMeta ( ) . getEntityTypes ( ) . filter ( candidate -> ! candidate . isAbstract ( ) ) . filter ( isCompatible ( target ) ) ; }
Public for testability
41,229
private Set < Impact > collectResult ( List < Impact > singleEntityChanges , List < Impact > wholeRepoActions , Set < String > dependentEntityIds ) { Set < String > wholeRepoIds = union ( wholeRepoActions . stream ( ) . map ( Impact :: getEntityTypeId ) . collect ( toImmutableSet ( ) ) , dependentEntityIds ) ; ImmutableSet . Builder < Impact > result = ImmutableSet . builder ( ) ; result . addAll ( wholeRepoActions ) ; dependentEntityIds . stream ( ) . map ( Impact :: createWholeRepositoryImpact ) . forEach ( result :: add ) ; singleEntityChanges . stream ( ) . filter ( action -> ! wholeRepoIds . contains ( action . getEntityTypeId ( ) ) ) . forEach ( result :: add ) ; return result . build ( ) ; }
Combines the results .