Search in sources :

Example 1 with SUser

use of org.bimserver.interfaces.objects.SUser in project BIMserver by opensourceBIM.

the class BimServerImporter method start.

public void start() {
    try (BimServerClientFactory factory = new JsonBimServerClientFactory(bimServer.getMetaDataManager(), address)) {
        LOGGER.info("Importing...");
        remoteClient = factory.create(new UsernamePasswordAuthenticationInfo(username, password));
        final BimDatabase database = bimServer.getDatabase();
        DatabaseSession databaseSession = database.createSession();
        try {
            LOGGER.info("Users...");
            for (SUser user : remoteClient.getServiceInterface().getAllUsers()) {
                createUser(databaseSession, user.getOid());
            }
            LOGGER.info("Projects...");
            for (SProject project : remoteClient.getServiceInterface().getAllProjects(false, false)) {
                createProject(databaseSession, project.getOid());
            }
            LOGGER.info("Done");
            databaseSession.commit();
        } catch (BimserverDatabaseException e) {
            LOGGER.error("", e);
        } finally {
            databaseSession.close();
        }
        final BimServerClientInterface client = bimServer.getBimServerClientFactory().create(new UsernamePasswordAuthenticationInfo(username, password));
        // for (SRenderEnginePluginConfiguration renderEnginePluginConfiguration : client.getPluginInterface().getAllRenderEngines(true)) {
        // if (renderEnginePluginConfiguration.getName().equals("IFC Engine DLL")) {
        // client.getPluginInterface().setDefaultRenderEngine(renderEnginePluginConfiguration.getOid());
        // }
        // }
        Path incoming = Paths.get(path);
        final Map<GregorianCalendar, Key> comments = new TreeMap<>();
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd-hh-mm-ss");
        for (SProject project : remoteClient.getServiceInterface().getAllProjects(false, false)) {
            for (SRevision revision : remoteClient.getServiceInterface().getAllRevisionsOfProject(project.getOid())) {
                GregorianCalendar gregorianCalendar = new GregorianCalendar();
                gregorianCalendar.setTime(revision.getDate());
                if (!revision.getComment().startsWith("generated for")) {
                    User user = users.get(revision.getUserId());
                    Path userFolder = incoming.resolve(user.getUsername());
                    boolean found = false;
                    for (Path file : PathUtils.list(userFolder)) {
                        if (file.getFileName().toString().endsWith(revision.getComment())) {
                            String dateStr = file.getFileName().toString().substring(0, 19);
                            Date parse = dateFormat.parse(dateStr);
                            GregorianCalendar fileDate = new GregorianCalendar();
                            fileDate.setTime(parse);
                            long millisDiff = Math.abs(fileDate.getTimeInMillis() - revision.getDate().getTime());
                            if (millisDiff > 1000 * 60 * 120) {
                                // 120 minutes
                                continue;
                            }
                            if (revision.getOid() == project.getLastRevisionId()) {
                                comments.put(gregorianCalendar, new Key(file, project.getOid(), revision.getComment(), revision.getDate(), revision.getUserId()));
                            }
                            found = true;
                            break;
                        }
                    }
                    if (!found) {
                        LOGGER.info("Not found: " + revision.getComment());
                    }
                }
            }
        }
        ExecutorService executorService = new ThreadPoolExecutor(1, 1, 1, TimeUnit.DAYS, new ArrayBlockingQueue<Runnable>(1000));
        for (final GregorianCalendar gregorianCalendar : comments.keySet()) {
            executorService.submit(new Runnable() {

                @Override
                public void run() {
                    Key key = comments.get(gregorianCalendar);
                    LOGGER.info("Checking in: " + key.file.getFileName().toString() + " " + Formatters.bytesToString(key.file.toFile().length()));
                    Project sProject = projects.get(key.poid);
                    try {
                        SDeserializerPluginConfiguration desserializer = client.getServiceInterface().getSuggestedDeserializerForExtension("ifc", sProject.getOid());
                        client.checkin(sProject.getOid(), key.comment, desserializer.getOid(), false, Flow.SYNC, key.file);
                        SProject updatedProject = client.getServiceInterface().getProjectByPoid(sProject.getOid());
                        DatabaseSession databaseSession = database.createSession();
                        try {
                            LOGGER.info("Done");
                            Project project = databaseSession.get(updatedProject.getOid(), OldQuery.getDefault());
                            Revision revision = project.getLastRevision();
                            User user = (User) databaseSession.get(users.get(key.userId).getOid(), OldQuery.getDefault());
                            for (Revision otherRevision : revision.getConcreteRevisions().get(0).getRevisions()) {
                                otherRevision.load();
                                otherRevision.setDate(key.date);
                                otherRevision.setComment(otherRevision.getComment().replace("Administrator", user.getName()));
                                databaseSession.store(otherRevision);
                            }
                            DateFormat m = new SimpleDateFormat("dd-MM-yyyy");
                            LOGGER.info("Setting date to " + m.format(key.date));
                            revision.setUser(user);
                            revision.setDate(key.date);
                            databaseSession.store(revision);
                            databaseSession.commit();
                        } catch (BimserverDatabaseException | ServiceException e) {
                            LOGGER.error("", e);
                        } finally {
                            databaseSession.close();
                        }
                    } catch (IOException | UserException | ServerException | PublicInterfaceNotFoundException e) {
                        LOGGER.error("", e);
                    }
                }
            });
        }
        executorService.shutdown();
    } catch (ServiceException e) {
        LOGGER.error("", e);
    } catch (ChannelConnectionException e) {
        LOGGER.error("", e);
    } catch (PublicInterfaceNotFoundException e) {
        LOGGER.error("", e);
    } catch (ParseException e) {
        LOGGER.error("", e);
    } catch (IOException e) {
        LOGGER.error("", e);
    } catch (BimServerClientException e1) {
        LOGGER.error("", e1);
    } catch (Exception e2) {
        LOGGER.error("", e2);
    }
}
Also used : User(org.bimserver.models.store.User) SUser(org.bimserver.interfaces.objects.SUser) SDeserializerPluginConfiguration(org.bimserver.interfaces.objects.SDeserializerPluginConfiguration) DatabaseSession(org.bimserver.database.DatabaseSession) UsernamePasswordAuthenticationInfo(org.bimserver.shared.UsernamePasswordAuthenticationInfo) JsonBimServerClientFactory(org.bimserver.client.json.JsonBimServerClientFactory) SUser(org.bimserver.interfaces.objects.SUser) SProject(org.bimserver.interfaces.objects.SProject) JsonBimServerClientFactory(org.bimserver.client.json.JsonBimServerClientFactory) BimServerClientFactory(org.bimserver.shared.BimServerClientFactory) BimServerClientInterface(org.bimserver.plugins.services.BimServerClientInterface) Path(java.nio.file.Path) ChannelConnectionException(org.bimserver.shared.ChannelConnectionException) GregorianCalendar(java.util.GregorianCalendar) IOException(java.io.IOException) TreeMap(java.util.TreeMap) BimServerClientException(org.bimserver.shared.exceptions.BimServerClientException) Date(java.util.Date) ChannelConnectionException(org.bimserver.shared.ChannelConnectionException) ServiceException(org.bimserver.shared.exceptions.ServiceException) ParseException(java.text.ParseException) PublicInterfaceNotFoundException(org.bimserver.shared.exceptions.PublicInterfaceNotFoundException) BimServerClientException(org.bimserver.shared.exceptions.BimServerClientException) IOException(java.io.IOException) UserException(org.bimserver.shared.exceptions.UserException) ServerException(org.bimserver.shared.exceptions.ServerException) SRevision(org.bimserver.interfaces.objects.SRevision) Project(org.bimserver.models.store.Project) SProject(org.bimserver.interfaces.objects.SProject) Revision(org.bimserver.models.store.Revision) SRevision(org.bimserver.interfaces.objects.SRevision) ServiceException(org.bimserver.shared.exceptions.ServiceException) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) PublicInterfaceNotFoundException(org.bimserver.shared.exceptions.PublicInterfaceNotFoundException) ExecutorService(java.util.concurrent.ExecutorService) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) ParseException(java.text.ParseException) BimDatabase(org.bimserver.database.BimDatabase) SimpleDateFormat(java.text.SimpleDateFormat)

Example 2 with SUser

use of org.bimserver.interfaces.objects.SUser in project BIMserver by opensourceBIM.

the class BimServerImporter method createUser.

public User createUser(DatabaseSession databaseSession, long remoteUoid) throws BimserverDatabaseException, ServerException, UserException, PublicInterfaceNotFoundException {
    if (users.containsKey(remoteUoid)) {
        return users.get(remoteUoid);
    }
    SUser remoteUser = remoteClient.getServiceInterface().getUserByUoid(remoteUoid);
    User newUser = databaseSession.create(User.class);
    users.put(remoteUoid, newUser);
    long createdById = remoteUser.getCreatedById();
    if (createdById != -1) {
        newUser.setCreatedBy(createUser(databaseSession, createdById));
    }
    newUser.setCreatedOn(remoteUser.getCreatedOn());
    newUser.setLastSeen(remoteUser.getLastSeen());
    newUser.setName(remoteUser.getName());
    newUser.setPasswordHash(remoteUser.getPasswordHash());
    newUser.setPasswordSalt(remoteUser.getPasswordSalt());
    newUser.setState(bimServer.getSConverter().convertFromSObject(remoteUser.getState()));
    newUser.setToken(remoteUser.getToken());
    newUser.setUsername(remoteUser.getUsername());
    newUser.setUserType(bimServer.getSConverter().convertFromSObject(remoteUser.getUserType()));
    bimServer.updateUserSettings(databaseSession, newUser);
    databaseSession.store(newUser);
    return newUser;
}
Also used : User(org.bimserver.models.store.User) SUser(org.bimserver.interfaces.objects.SUser) SUser(org.bimserver.interfaces.objects.SUser)

Example 3 with SUser

use of org.bimserver.interfaces.objects.SUser in project BIMserver by opensourceBIM.

the class SyndicationServlet method writeCheckoutsFeed.

private void writeCheckoutsFeed(HttpServletRequest request, HttpServletResponse response, ServiceMap serviceMap) throws ServiceException, IOException, FeedException, PublicInterfaceNotFoundException {
    long poid = Long.parseLong(request.getParameter("poid"));
    SProject sProject = serviceMap.getServiceInterface().getProjectByPoid(poid);
    SyndFeed feed = new SyndFeedImpl();
    feed.setFeedType(FEED_TYPE);
    feed.setTitle("BIMserver.org checkouts feed for project '" + sProject.getName() + "'");
    feed.setLink(request.getContextPath());
    feed.setDescription("This feed represents all the checkouts of project '" + sProject.getName() + "'");
    List<SyndEntry> entries = new ArrayList<SyndEntry>();
    try {
        List<SCheckout> allCheckoutsOfProject = serviceMap.getServiceInterface().getAllCheckoutsOfProjectAndSubProjects(poid);
        for (SCheckout sCheckout : allCheckoutsOfProject) {
            SRevision revision = serviceMap.getServiceInterface().getRevision(sCheckout.getRevision().getOid());
            SProject project = serviceMap.getServiceInterface().getProjectByPoid(sCheckout.getProjectId());
            SUser user = serviceMap.getServiceInterface().getUserByUoid(sCheckout.getUserId());
            SyndEntry entry = new SyndEntryImpl();
            entry.setTitle("Checkout on " + project.getName() + ", revision " + revision.getId());
            entry.setLink(request.getContextPath() + "/project.jsp?poid=" + sProject.getOid());
            entry.setPublishedDate(sCheckout.getDate());
            SyndContent description = new SyndContentImpl();
            description.setType("text/plain");
            description.setValue("<table><tr><td>User</td><td>" + user.getUsername() + "</td></tr><tr><td>Revision</td><td>" + sCheckout.getRevision().getOid() + "</td></tr></table>");
            entry.setDescription(description);
            entries.add(entry);
        }
    } catch (UserException e) {
        LOGGER.error("", e);
    }
    feed.setEntries(entries);
    SyndFeedOutput output = new SyndFeedOutput();
    output.output(feed, response.getWriter());
}
Also used : SyndEntry(com.rometools.rome.feed.synd.SyndEntry) SyndContentImpl(com.rometools.rome.feed.synd.SyndContentImpl) SUser(org.bimserver.interfaces.objects.SUser) ArrayList(java.util.ArrayList) SyndFeedOutput(com.rometools.rome.io.SyndFeedOutput) SProject(org.bimserver.interfaces.objects.SProject) SyndFeed(com.rometools.rome.feed.synd.SyndFeed) SRevision(org.bimserver.interfaces.objects.SRevision) SyndContent(com.rometools.rome.feed.synd.SyndContent) SyndEntryImpl(com.rometools.rome.feed.synd.SyndEntryImpl) SyndFeedImpl(com.rometools.rome.feed.synd.SyndFeedImpl) UserException(org.bimserver.shared.exceptions.UserException) SCheckout(org.bimserver.interfaces.objects.SCheckout)

Example 4 with SUser

use of org.bimserver.interfaces.objects.SUser in project BIMserver by opensourceBIM.

the class ServiceImpl method getAllUsers.

@Override
public List<SUser> getAllUsers() throws ServerException, UserException {
    if (getBimServer().getServerSettingsCache().getServerSettings().getHideUserListForNonAdmin()) {
        if (getCurrentUser().getUserType() != SUserType.ADMIN) {
            throw new UserException("Admin rights required to list users");
        }
    }
    requireRealUserAuthentication();
    DatabaseSession session = getBimServer().getDatabase().createSession();
    try {
        BimDatabaseAction<Set<User>> action = new GetAllUsersDatabaseAction(session, getInternalAccessMethod(), getAuthorization());
        List<SUser> convertToSListUser = getBimServer().getSConverter().convertToSListUser(session.executeAndCommitAction(action));
        Collections.sort(convertToSListUser, new SUserComparator());
        return convertToSListUser;
    } catch (Exception e) {
        return handleException(e);
    } finally {
        session.close();
    }
}
Also used : Set(java.util.Set) LinkedHashSet(java.util.LinkedHashSet) DatabaseSession(org.bimserver.database.DatabaseSession) SUser(org.bimserver.interfaces.objects.SUser) UserException(org.bimserver.shared.exceptions.UserException) GetAllUsersDatabaseAction(org.bimserver.database.actions.GetAllUsersDatabaseAction) SUserComparator(org.bimserver.webservices.SUserComparator) IOException(java.io.IOException) BimserverDatabaseException(org.bimserver.BimserverDatabaseException) SerializerException(org.bimserver.plugins.serializers.SerializerException) BcfException(org.opensourcebim.bcf.BcfException) UserException(org.bimserver.shared.exceptions.UserException) CannotBeScheduledException(org.bimserver.longaction.CannotBeScheduledException) DeserializeException(org.bimserver.plugins.deserializers.DeserializeException) ServerException(org.bimserver.shared.exceptions.ServerException) MessagingException(javax.mail.MessagingException) AddressException(javax.mail.internet.AddressException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) MalformedURLException(java.net.MalformedURLException)

Example 5 with SUser

use of org.bimserver.interfaces.objects.SUser in project BIMserver by opensourceBIM.

the class ServiceImpl method getAllLocalProfiles.

@Override
public List<SProfileDescriptor> getAllLocalProfiles(String serviceIdentifier) throws ServerException, UserException {
    requireRealUserAuthentication();
    DatabaseSession session = getBimServer().getDatabase().createSession();
    List<SProfileDescriptor> descriptors = new ArrayList<SProfileDescriptor>();
    try {
        SUser currentUser = getCurrentUser();
        Condition condition = new AttributeCondition(StorePackage.eINSTANCE.getUser_Token(), new StringLiteral(currentUser.getToken()));
        User user = session.querySingle(condition, User.class, OldQuery.getDefault());
        if (user != null) {
            for (InternalServicePluginConfiguration internalServicePluginConfiguration : user.getUserSettings().getServices()) {
                if (serviceIdentifier.equals("" + internalServicePluginConfiguration.getOid())) {
                    SProfileDescriptor sProfileDescriptor = new SProfileDescriptor();
                    descriptors.add(sProfileDescriptor);
                    sProfileDescriptor.setIdentifier("" + internalServicePluginConfiguration.getOid());
                    sProfileDescriptor.setName(internalServicePluginConfiguration.getName());
                    sProfileDescriptor.setDescription(internalServicePluginConfiguration.getDescription());
                    sProfileDescriptor.setPublicProfile(false);
                }
            }
        }
    } catch (Exception e) {
        handleException(e);
    } finally {
        session.close();
    }
    return descriptors;
}
Also used : AttributeCondition(org.bimserver.database.query.conditions.AttributeCondition) Condition(org.bimserver.database.query.conditions.Condition) SUser(org.bimserver.interfaces.objects.SUser) User(org.bimserver.models.store.User) StringLiteral(org.bimserver.database.query.literals.StringLiteral) DatabaseSession(org.bimserver.database.DatabaseSession) SUser(org.bimserver.interfaces.objects.SUser) InternalServicePluginConfiguration(org.bimserver.models.store.InternalServicePluginConfiguration) ArrayList(java.util.ArrayList) AttributeCondition(org.bimserver.database.query.conditions.AttributeCondition) SProfileDescriptor(org.bimserver.interfaces.objects.SProfileDescriptor) IOException(java.io.IOException) BimserverDatabaseException(org.bimserver.BimserverDatabaseException) SerializerException(org.bimserver.plugins.serializers.SerializerException) BcfException(org.opensourcebim.bcf.BcfException) UserException(org.bimserver.shared.exceptions.UserException) CannotBeScheduledException(org.bimserver.longaction.CannotBeScheduledException) DeserializeException(org.bimserver.plugins.deserializers.DeserializeException) ServerException(org.bimserver.shared.exceptions.ServerException) MessagingException(javax.mail.MessagingException) AddressException(javax.mail.internet.AddressException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) MalformedURLException(java.net.MalformedURLException)

Aggregations

SUser (org.bimserver.interfaces.objects.SUser)13 UserException (org.bimserver.shared.exceptions.UserException)9 ServerException (org.bimserver.shared.exceptions.ServerException)8 IOException (java.io.IOException)6 DatabaseSession (org.bimserver.database.DatabaseSession)6 SRevision (org.bimserver.interfaces.objects.SRevision)6 UnsupportedEncodingException (java.io.UnsupportedEncodingException)5 MalformedURLException (java.net.MalformedURLException)5 MessagingException (javax.mail.MessagingException)5 AddressException (javax.mail.internet.AddressException)5 BimserverDatabaseException (org.bimserver.BimserverDatabaseException)5 SProject (org.bimserver.interfaces.objects.SProject)5 CannotBeScheduledException (org.bimserver.longaction.CannotBeScheduledException)5 DeserializeException (org.bimserver.plugins.deserializers.DeserializeException)5 SerializerException (org.bimserver.plugins.serializers.SerializerException)5 BcfException (org.opensourcebim.bcf.BcfException)5 ArrayList (java.util.ArrayList)4 User (org.bimserver.models.store.User)3 PublicInterfaceNotFoundException (org.bimserver.shared.exceptions.PublicInterfaceNotFoundException)3 ServiceException (org.bimserver.shared.exceptions.ServiceException)3